Thread: newb wonders if he is doing it right

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    newb wonders if he is doing it right

    Hello all,

    I am learning C++ more and less by my self by online resources
    and a couple books i have bought. I have been working on it for around six months now and i started with no real knowledge of programming.

    This is a program i have been working on witch will calculate Volts, Ohms,
    Watts, Amps, and Voltage drop.

    The Voltagedrop section i am working on as a separate program that i will turn into a fuction of the main program as soon as i overcome some of my lack of knowledge and figuring out witch way works best.

    This Voltagedrop portion will contain:

    (2) different wire material types
    (30) different wire sizes
    (3) different voltage types
    a user inputed wire length
    a user inputed amperage

    i am planning a calculation based on only the:
    amperage
    voltage type
    distance

    This is for calculating what wire size would give no more than a 3%
    voltage drop for a given circut running user supplied information said above.

    but here is the main program witht he basic calculations.

    and below that is a snippet of the voltagedrop section...



    Code:
    #include <iostream>
    #include <cstdlib>
    void MainMenu();
    void VoltMenu();
    void OhmMenu();
    void WattMenu();
    void AmpMenu();
    void DropMenu();
    void AboutMenu();
    
    
    using namespace std;
    
    int main()
    {
      MainMenu();
      return 0;
    }
    void MainMenu()
    {
    int choice;
    cout<<"      VOWAD\n";
    cout<<"*** Main Menu ***\n";
    cout<<"[1] Calculate Volts\n";
    cout<<"[2] Calculate Ohms\n";
    cout<<"[3] Calculate Watts\n";
    cout<<"[4] Calculate Amps\n";
    cout<<"[5] Calculate Drop\n";
    cout<<"[6] About Vowad\n";
    cout<<"[7] Exit\n";
    cout<<"Selection: ";
    cin>> choice;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    MainMenu();
    }
    if (choice == 1){
    system("clear output");
    VoltMenu();
    }
    else if (choice == 2){
    system("clear output");
    OhmMenu();
    }
    else if (choice == 3){
    system("clear output");
    WattMenu();
    }
    else if (choice == 4){
    system("clear output");
    AmpMenu();
    }
    else if (choice == 5){
    system("clear output");
    DropMenu();
    }
    else if (choice == 6){
    system("clear output");
    AboutMenu();
    }
    else if (choice == 7){
    cout<<"You entered choice: " << choice << endl;
    cout<<"Exiting Vowad\n";
    system("exit");
    }
    else {
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    MainMenu();
    }
    }
    void VoltMenu()
    {
    float volt;
    float amp;
    float ohm;
    float watt;
    int choice;
    cout<<"      VOWAD\n";
    cout<<"*** VOLTS MENU ***\n";
    cout<<"[1] Calculate Volts (watt/amp)\n";
    cout<<"[2] Calculate Volts (ohm*amp)\n";
    cout<<"[3] Main Menu\n";
    cout<<"Selected: ";
    cin >> choice;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    
    VoltMenu();
    }
    if (choice == 1){
        cout<<"Enter Watts: ";
        cin>> watt;
        cout<<"Enter Amps: ";
        cin>> amp;
        volt = watt / amp;
        ohm = volt / amp;
        system("clear output");
        cout<<"Circut Running At: "<< watt << " Watts" << " -and- " << amp <<" Amps\n";
        cout<<"Has Calculated Volts: " << volt << endl;
        cout<<"Has Calculated Ohms: " << ohm << endl;
        VoltMenu();
    }
    else if (choice == 2){
        cout<<"Enter Ohms: ";
        cin>> ohm;
        cout<<"Enter Amps: ";
        cin>> amp;
        volt = amp * ohm;
        watt = volt * amp;
        system("clear output");
        cout<<"Circut Running At: "<< ohm << " Ohms" << " -and- " << amp <<" Amps\n";
        cout<<"Has Calculated Volts: " << volt << endl;
        cout<<"Has Calculated Watts: " << watt << endl;
        VoltMenu();   
    }
    else if (choice == 3){
    system("clear output");
    MainMenu();
    }
    }
    void OhmMenu()
    {
    float volt;
    float amp;
    float ohm;
    float watt;
    int choice;
    cout<<"*** OHMS MENU ***\n";
    cout<<"[1] Calculate Ohms (volt/amp)\n";
    cout<<"[2] Calculate Ohms (ohm=volt/(watt/volt=amp)\n";
    cout<<"[3] Main Menu\n";
    cout<<"Selected: ";
    cin >> choice;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    OhmMenu();
    }
    if (choice == 1){
        cout<<"Enter Volts: ";
        cin>> volt;
        cout<<"Enter Amps: ";
        cin>> amp;
        ohm = volt / amp;
        watt = volt * amp;
        system("clear output");
        cout<<"Circut Running At: "<< volt << " Volts" << " -and- " << amp <<" Amps\n";
        cout<<"Has Calculated Ohms: " << ohm << endl;
        cout<<"Has Calculated Watts: " << watt << endl;
        OhmMenu();
    }
    else if (choice == 2){
        cout<<"Enter Watts: ";
        cin>> watt;
        cout<<"Enter Volts: ";
        cin>> volt;
        amp = watt / volt;
        ohm = volt / amp;
        system("clear output");
        cout<<"Circut Running At: "<< watt << " Watts" << " -and- " << volt <<" Volts\n";
        cout<<"Has Calculated Ohms: " << ohm << endl;
        cout<<"Has Calculated Running Amps: " << amp << endl;
        OhmMenu();    
    }
    else if (choice == 3){
    system("clear output");
    MainMenu();
    }
    }
    void WattMenu()
    {
    float volt;
    float amp;
    float ohm;
    float watt;
    int choice;
    cout<<"      VOWAD\n";
    cout<<"*** WATTS MENU ***\n";
    cout<<"[1] Calculate Watts (watt/amp)\n";
    cout<<"[2] Main Menu\n";
    cout<<"Selected: ";
    cin >> choice;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    WattMenu();
    }
    if (choice == 1){
        cout<<"Enter Volts: ";
        cin>> volt;
        cout<<"Enter Amps: ";
        cin>> amp;
        watt = volt * amp;
        ohm = volt / amp;
        system("clear output");
        cout<<"Circut Running At: "<< volt << " Volts" << " -and- " << amp <<" Amps\n";
        cout<<"Has Calculated Watts: " << watt << endl;
        cout<<"Has Calculated Ohms: " << ohm << endl;
        WattMenu();
    }
    else if (choice == 2){
        system("clear output");
        MainMenu();    
    }
    }
    
    void AmpMenu()
    {
    float volt;
    float amp;
    float ohm;
    float watt;
    int choice;
    cout<<"      VOWAD\n";
    cout<<"*** AMPS MENU ***\n";
    cout<<"[1] Calculate Amps (ohm/volt)\n";
    cout<<"[2] Calculate Amps (volt/watt)\n";
    cout<<"[3] Main Menu\n";
    cout<<"Selected: ";
    cin >> choice;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    system("clear output");
    cout<<"Sorry! not a menu option\n";
    AmpMenu();
    }
    if (choice == 1){
        cout<<"Enter Volts: ";
        cin>> volt;
        cout<<"Enter Ohms: ";
        cin>> ohm;
        amp = volt / ohm;
        watt = volt * amp;
        system("clear output");
        cout<<"Circut Running At: "<< volt << " Volts" << " -and- " << ohm <<" Ohms\n";
        cout<<"Has Calculated Amps: " << amp << endl;
        cout<<"Has Calculated Watts: " << watt << endl;
        AmpMenu(); 
    }
    else if (choice == 2){
        cout<<"Enter Watts: ";
        cin>> watt;
        cout<<"Enter Volts: ";
        cin>> volt;
        amp = watt / volt;
        ohm = volt / amp;
        system("clear output");
        cout<<"Circut Running At: "<< watt << " Watts" << " -and- " << volt <<" Volts\n";
        cout<<"Has Calculated Ohms: " << ohm << endl;
        cout<<"Has Calculated Amps: " << amp << endl;
        AmpMenu();   
    }
    else if (choice == 3){
    system("clear output");
    MainMenu();
    }
    }
    void DropMenu()
    {
    cout<<" Not there yet but i am working on this part :(\n";
    MainMenu();
    }
    void AboutMenu()
    {
    cout<<"           *** Smother Kitten Software ***\n";
    cout<<"                   *** Vowad ***\n";
    cout<<"                  system: win32\n";
    cout<<"                   version: 0.2\n";
    cout<<"                by: Jacob K Mcgrath\n";
    cout<<"\n";
    cout<<"               A electrical calculator\n";
    cout<<"             written in C++ on a Linux box\n";
    MainMenu();
    }


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    void Drop();
    
    using namespace std;
    
    int main()
    {
      Drop();
      return EXIT_SUCCESS;
    }
    
    void Drop()
    {
    string Copper;
    string Aluminum;
    float materialdata[2] = {12.9, 21.2};
    float voltagedata[3] = {1, 0.5, .866};
    int kcmildata[30] = {1620, 2560, 4110, 6530, 10380, 16510,
    26240, 41740, 52620, 66360, 83690, 105600, 133100, 167800,
    211600, 250000, 300000, 350000, 400000, 500000, 600000, 700000,
    750000, 800000, 900000, 1000000, 1250000, 1500000, 1750000, 2000000};
    int lenght;
    int amperes;
    float voltdrop;
    float voltdroppercent;
    float material;
    float voltage;
    int kcmil;
    int input1;
    int input2;
    cout<<"          VOWAD\n";
    cout<<"*** VOLTAGE DROP MENU ***\n";
    cout<<"[1] Copper\n";
    cout<<"[2] Aluminum\n";
    cout<<"[3] Main Menu\n";
    cout<<"Selected: ";
    cin>> input1;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    cout<<"Please try again\n";
    Drop();
    }
    else if (input1 == 1){
    material = materialdata[0];
    cout<<" copper constant is:  " << material << endl;
    }
    else if (input1 == 2){
    material = materialdata[1];
    cout<<" aluminum constant is:  " << material << endl;
    }
    else if (input1 == 3){
    system("clear output");
    Drop();
    }
    cout<<"Select Wire\n";
    cout<<"[1] 18 AWG    [16] 250 kcmil   [31] Voltage Drop Menu\n";   
    cout<<"[2] 16 AWG    [17] 350 kcmil                     \n";
    cout<<"[3] 14 AWG    [18] 400 kcmil                     \n"; 
    cout<<"[4] 12 AWG    [19] 450 kcmil                     \n";
    cout<<"[5] 10 AWG    [20] 500 kcmil                     \n";
    cout<<"[6]  8 AWG    [21] 600 kcmil                     \n";
    cout<<"[7]  6 AWG    [22] 700 kcmil                     \n";
    cout<<"[8]  4 AWG    [23] 750 kcmi                      \n";
    cout<<"[9]  3 AWG    [24] 800 kcmil                     \n";
    cout<<"[10] 2 AWG    [25] 900 kcmil                     \n";
    cout<<"[11] 1 AWG    [26] 1000 kcmil                    \n";
    cout<<"[12] 1/0 AWG  [27] 1250 kcmil                    \n";
    cout<<"[13] 2/0 AWG  [28] 1500 kcmil                    \n";
    cout<<"[14] 3/0 AWG  [29] 1750 kcmil                    \n";
    cout<<"[15] 4/0 AWG  [30] 2000 kcmil                    \n";
    cout<<"Selected: ";
    cin>> input2;
    if (cin.fail()) {
    cin.clear(); cin.ignore();
    cout<<"Please try again\n";
    Drop();
    }
    else if (input2 == 1){ kcmil = kcmildata[0];
    cout<< kcmil << endl;
    
    }
    else if (input2 == 2){ kcmil = kcmildata[1];
    cout<< kcmil << endl;
    }
    else if (input2 == 3){ kcmil = kcmildata[2];
    cout<< kcmil << endl;
    }
    else if (input2 == 4){ kcmil = kcmildata[3];
    cout<< kcmil << endl;
    }
    else if (input2 == 5){ kcmil = kcmildata[4];
    cout<< kcmil << endl;
    }
    else if (input2 == 6){ kcmil = kcmildata[5];
    cout<< kcmil << endl;
    }
    else if (input2 == 7){ kcmil = kcmildata[6];
    }
    else if (input2 == 8){ kcmil = kcmildata[7];
    }
    else if (input2 == 9){ kcmil = kcmildata[8];
    }
    else if (input2 == 10){ kcmil = kcmildata[9];
    }
    else if (input2 == 11){ kcmil = kcmildata[10];
    }
    else if (input2 == 12){ kcmil = kcmildata[11];
    }
    else if (input2 == 13){ kcmil = kcmildata[12];
    }
    else if (input2 == 14){ kcmil = kcmildata[13];
    }
    else if (input2 == 15){ kcmil = kcmildata[14];
    }
    else if (input2 == 16){ kcmil = kcmildata[15];
    }
    else if (input2 == 17){ kcmil = kcmildata[16];
    }
    else if (input2 == 18){ kcmil = kcmildata[17];
    }
    else if (input2 == 19){ kcmil = kcmildata[18];
    }
    else if (input2 == 20){ kcmil = kcmildata[19];
    }
    else if (input2 == 21){ kcmil = kcmildata[20];
    cout<< kcmil << endl;
    }
    else if (input2 == 22){ kcmil = kcmildata[21];
    cout<< kcmil << endl;
    }
    else if (input2 == 23){ kcmil = kcmildata[22];
    cout<< kcmil << endl;
    }
    else if (input2 == 24){ kcmil = kcmildata[23];
    cout<< kcmil << endl;
    }
    else if (input2 == 25){ kcmil = kcmildata[24];
    cout<< kcmil << endl;
    }
    else if (input2 == 26){ kcmil = kcmildata[25];
    cout<< kcmil << endl;
    }
    else if (input2 == 27){ kcmil = kcmildata[26];
    cout<< kcmil << endl;
    }
    else if (input2 == 28){ kcmil = kcmildata[27];
    cout<< kcmil << endl;
    }
    else if (input2 == 29){ kcmil = kcmildata[28];
    cout<< kcmil << endl;
    }
    else if (input2 == 30){kcmil = kcmildata[29];
    cout<< kcmil << endl;
    }
    else if (input2 == 31){
    system("clear output"); 
    Drop();
    }
    cout<<"i have a poopy but\n";
    }


    as you see i am just getting started and was wondering if i could use case switches instead of the *if else if loops ?

    i used case switches before and i dont even know why i strayed from that option LOL

    another thing i was wondering is if i was using array,s somewhat correctly in relating to

    the menu input in the voltage drop program

    else if (input2 == 30){kcmil = kcmildata[29]; }
    -and-
    else if (input1 == 2){
    material = materialdata[1]; }

    it seems to work for now but i think i will run into problems later

    let me know if there is a better way other than what i can see at the moment
    not so much solve the problem but point me in a given direction so i can do the trial by fire route


    ty

    heinola

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    sry for my syntax ahead of time

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok. Take a close look at your code. The else ifs. Do you notice a pattern? Let me illustrate it for you more, so you can see it if you don't already.

    Code:
    else if (input2 == 1)
    { 
    	kcmil = kcmildata[0];
    	cout<< kcmil << endl;
    }
    else if (input2 == 2)
    {
    	kcmil = kcmildata[1];
    	cout<< kcmil << endl;
    }
    else if (input2 == 3)
    { 
    	kcmil = kcmildata[2];
    	cout<< kcmil << endl;
    }
    The array index that you're modifying is ALWAYS input2 - 1.

    Here's an easier function for you.

    Code:
    else if ((input2 > 0) && (input2 <= 30)) // 30 because that's as high as you're going with your ifs.
    { 
    	kcmil = kcmildata[input2 - 1];
    	cout<< kcmil << endl;
    }
    Replace all your else ifs up to 30 with that single if block. Keep 31, because that's a different animal. Your code will look similar to this:

    Code:
    if (cin.fail())
    {
        cin.clear();
        cin.ignore();
        cout<<"Please try again\n";
        Drop();
    }
    
    else if ((input2 > 0) && (input2 <= 30))
    { 
        kcmil = kcmildata[input2 - 1];
        cout<< kcmil << endl;
    }
    
    else if (input2 == 31)
    {
        system("clear output"); 
        Drop();
    }
    Last edited by Lithorien; 04-16-2005 at 09:51 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well I hope your code is better formatted in your text editor than the mess you posted is (ugh!)

    Few people are going to spend large amounts of time figuring out what large amounts of code which is poorly formatted is doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Salem's right, posting a buch of code isn't the way to go, especially if it's poorly formatted.

    You are right about switch statements. Somehow internally, they are more efficient than a bunch of if-else statements. So, if the number you are checking is an int type, then use a switch.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Code:
    else if ((input2 > 0) && (input2 <= 30))
    { 
        kcmil = kcmildata[input2 - 1];
        cout<< kcmil << endl;
    }
    so my kcmildata[] array has a range from 0 to 30

    and my input2 has a range from 1 to 31

    all this is doing is using input2 and subtracting 1 from its value
    to appease the offset between my menu and my array

    like

    I selected 17

    17-1 = 16

    16 point to the array value i wanted

    and menu option 31 is just my call for the main menu fuction to start again



    i am working on the syntax today

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    been working on it a bit tried to clean it up a bit as well
    at least for my reading

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    void Drop();
    
    using namespace std;
    
    int main()
    {
        Drop();
        return EXIT_SUCCESS;
    }
    
    void Drop()
    {
    float materialdata[2] = {12.9, 21.2};
    float voltagedata[10] = {1, 1, .866, .5, 1, .866, .5, 1, 1, 1};
    float voltdrop;
    float voltdroppercent;
    float material;
    float voltage;
    int kcmildata[30] = {1620, 2560, 4110, 6530, 10380, 16510,
    26240, 41740, 52620, 66360, 83690, 105600, 133100, 167800,
    211600, 250000, 300000, 350000, 400000, 500000, 600000, 700000,
    750000, 800000, 900000, 1000000, 1250000, 1500000, 1750000, 2000000};
    int length;
    int amperes;
    int kcmil;
    int input;
    
    cout<<"          VOWAD\n";
    cout<<"*** VOLTAGE DROP MENU ***\n";
    cout<<"[1] Copper\n";
    cout<<"[2] Aluminum\n";
    cout<<"[3] Main Menu\n";
    cout<<"Selected: ";
    cin>> input;
    if (cin.fail()) 
      {
        cin.clear();
        cin.ignore();
        cout<<"Please try again\n";
        Drop();
      }
    else if ((input > 0) && (input <= 2))
      { 
        material = materialdata[input - 1];
        cout<< material << endl;
      }
        
    else if (input == 3)
      {
        system("clear output"); 
        Drop();
      }
    cout<<"Select Wire\n";
    cout<<"[1] 18 AWG    [16] 250 kcmil   [31] VoltageDrop Menu\n";   
    cout<<"[2] 16 AWG    [17] 350 kcmil                     \n";
    cout<<"[3] 14 AWG    [18] 400 kcmil                     \n"; 
    cout<<"[4] 12 AWG    [19] 450 kcmil                     \n";
    cout<<"[5] 10 AWG    [20] 500 kcmil                     \n";
    cout<<"[6]  8 AWG    [21] 600 kcmil                     \n";
    cout<<"[7]  6 AWG    [22] 700 kcmil                     \n";
    cout<<"[8]  4 AWG    [23] 750 kcmi                      \n";
    cout<<"[9]  3 AWG    [24] 800 kcmil                     \n";
    cout<<"[10] 2 AWG    [25] 900 kcmil                     \n";
    cout<<"[11] 1 AWG    [26] 1000 kcmil                    \n";
    cout<<"[12] 1/0 AWG  [27] 1250 kcmil                    \n";
    cout<<"[13] 2/0 AWG  [28] 1500 kcmil                    \n";
    cout<<"[14] 3/0 AWG  [29] 1750 kcmil                    \n";
    cout<<"[15] 4/0 AWG  [30] 2000 kcmil                    \n";
    cout<<"Selected: ";
    cin>> input;
    if (cin.fail()) 
      {
        cin.clear();
        cin.ignore();
        cout<<"Please try again\n";
        Drop();
      }
    else if ((input > 0) && (input <= 30))
      {
        kcmil = kcmildata[input - 1];
        cout<< kcmil << endl;
        cout<< material << endl;
      }
    else if (input == 31)
      {
        system("clear output"); 
        Drop();
      }
     cout<<"Select voltage type\n";
    cout<<" [1] 120v 1-phase\n";   
    cout<<" [2] 240v 1phase\n";
    cout<<" [3] 208v 3-phase 3-wire\n"; 
    cout<<" [4] 120/208v 3-phase 4-wire\n";
    cout<<" [5] 277v 1-phase\n";
    cout<<" [6] 480v 3-phase\n";
    cout<<" [7] 277/480v 3-phase 4-wire\n";
    cout<<" [8] 24v dc/ac 1-phase\n";
    cout<<" [9] 48v dc/ac 1-phase\n";
    cout<<"[10] 124v dc/ac 1-phase\n";
    cout<<"[11] Voltagedrop Menu\n"; 
    cout<<"Selected: ";
    cin>> input;
    if (cin.fail()) 
      {
        cin.clear();
        cin.ignore();
        cout<<"Please try again\n";
        Drop();
      }
    else if ((input > 0) && (input <= 10))
      {
        voltage = voltagedata[input - 1];
        cout<< voltage << endl;
        cout<< material << endl;
        cout<< kcmil << endl;
      }
    else if (input == 11)
      {
        system("clear output"); 
        Drop();
      }  
    }

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by 7stud
    You are right about switch statements. Somehow internally, they are more efficient than a bunch of if-else statements. So, if the number you are checking is an int type, then use a switch.
    Your not quite right there in that you can use switch statements with char if you want, you just need to use inverted commas:

    Code:
    switch (example)
           case 'a':
                       blah blah;
                       break;
    excuse me if you already knew, but i couldnt bear to see you not knowing that, lol
    Last edited by Welshy; 04-18-2005 at 08:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  3. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  4. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  5. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM