# Thread: newb wonders if he is doing it right

1. ## 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>

using namespace std;

int main()
{
return 0;
}
{
int choice;
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<<"[7] Exit\n";
cout<<"Selection: ";
cin>> choice;
if (cin.fail()) {
cin.clear(); cin.ignore();
system("clear output");
}
if (choice == 1){
system("clear output");
}
else if (choice == 2){
system("clear output");
}
else if (choice == 3){
system("clear output");
}
else if (choice == 4){
system("clear output");
}
else if (choice == 5){
system("clear output");
}
else if (choice == 6){
system("clear output");
}
else if (choice == 7){
cout<<"You entered choice: " << choice << endl;
system("exit");
}
else {
system("clear output");
}
}
{
float volt;
float amp;
float ohm;
float watt;
int choice;
cout<<"[1] Calculate Volts (watt/amp)\n";
cout<<"[2] Calculate Volts (ohm*amp)\n";
cout<<"Selected: ";
cin >> choice;
if (cin.fail()) {
cin.clear(); cin.ignore();
system("clear output");

}
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;
}
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;
}
else if (choice == 3){
system("clear output");
}
}
{
float volt;
float amp;
float ohm;
float watt;
int choice;
cout<<"[1] Calculate Ohms (volt/amp)\n";
cout<<"[2] Calculate Ohms (ohm=volt/(watt/volt=amp)\n";
cout<<"Selected: ";
cin >> choice;
if (cin.fail()) {
cin.clear(); cin.ignore();
system("clear output");
}
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;
}
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;
}
else if (choice == 3){
system("clear output");
}
}
{
float volt;
float amp;
float ohm;
float watt;
int choice;
cout<<"[1] Calculate Watts (watt/amp)\n";
cout<<"Selected: ";
cin >> choice;
if (cin.fail()) {
cin.clear(); cin.ignore();
system("clear output");
}
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;
}
else if (choice == 2){
system("clear output");
}
}

{
float volt;
float amp;
float ohm;
float watt;
int choice;
cout<<"[1] Calculate Amps (ohm/volt)\n";
cout<<"[2] Calculate Amps (volt/watt)\n";
cout<<"Selected: ";
cin >> choice;
if (cin.fail()) {
cin.clear(); cin.ignore();
system("clear output");
}
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;
}
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;
}
else if (choice == 3){
system("clear output");
}
}
{
cout<<" Not there yet but i am working on this part :(\n";
}
{
cout<<"           *** Smother Kitten Software ***\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";
}```

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<<"[1] Copper\n";
cout<<"[2] Aluminum\n";
cout<<"Selected: ";
cin>> input1;
if (cin.fail()) {
cin.clear(); cin.ignore();
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();
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. sry for my syntax ahead of time

3. 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();
Drop();
}

else if ((input2 > 0) && (input2 <= 30))
{
kcmil = kcmildata[input2 - 1];
cout<< kcmil << endl;
}

else if (input2 == 31)
{
system("clear output");
Drop();
}```

4. 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.

5. 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. 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. been working on it a bit tried to clean it up a bit as well

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<<"[1] Copper\n";
cout<<"[2] Aluminum\n";
cout<<"Selected: ";
cin>> input;
if (cin.fail())
{
cin.clear();
cin.ignore();
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();
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<<"Selected: ";
cin>> input;
if (cin.fail())
{
cin.clear();
cin.ignore();
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. 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