Basic C++ help please

handoverfist

Golden Member
Apr 1, 2001
1,427
0
0
I am supposed to use a while and switch structure for one of my assignments. My question is after one of the cases are met, how do I get it to go back to beginning of the program to prompt for new input? Any help is appreciated.

while(Sel == 1 || Sel == 2 || Sel == 3)
{
switch(Sel)
{
case 1:
float R1, R2, R3, rsum, curr;
cout << "Three resistor series circuit selected " << endl;
cout << "Enter Value for R1 (Ohms) : " << endl;
cin >> R1;
cout << "Enter Value for R2 (Ohms) : " << endl;
cin >> R2;
cout << "Enter Value for R3 (Ohms) : " << endl;
cin >> R3;
rsum = R1 + R2 + R3;
curr = volt / rsum;

cout << endl << endl;
cout << "Three resistor parallel circuit selected " << endl << endl;
cout << "Voltage across R1 = " << curr * R1 << endl;
cout << "Voltage across R2 = " << curr * R2 << endl;
cout << "Voltage across R3 = " << curr * R3 << endl << endl;

cout << "Current through R1 = " << volt << endl;
cout << "Current through R2 = " << volt << endl;
cout << "Current through R3 = " << volt << endl;
return 0;
break;

case 2:
cout << "Enter Value for R1 (Ohms) : " << endl;
cin >> R1;
cout << "Enter Value for R2 (Ohms) : " << endl;
cin >> R2;
cout << "Enter Value for R3 (Ohms) : " << endl;
cin >> R3;

cout << endl;
cout << "Voltage across R1 = " << volt << endl;
cout << "Voltage across R2 = " << volt << endl;
cout << "Voltage across R3 = " << volt << endl << endl;

cout << "Current through R1 = " << volt / R1 << endl;
cout << "Current through R2 = " << volt / R2 << endl;
cout << "Current through R3 = " << volt / R2 << endl;
break;

case 3:
return 0;
break;
 

stevf

Senior member
Jan 26, 2005
290
0
0
is all of that under main?

try removing the return 0; statements and then see what happens - is it just running through once? about to leave work so dont have time to look closer - also how are you reprompting the user for their choice? that should be inside the while loop
 

handoverfist

Golden Member
Apr 1, 2001
1,427
0
0
Yes, that is all under main.

Right now it runs through case 1 and then case 2 gets stuck in an infinite loop. When I remove the return 0's case 1 gets stuck in an infinite loop.

After case 1 or case 2 options have been entered, and the the output shown, I need to ask user if they want to run the circuit over again. That is where I am lost.

Thanks for the help!
 

stash

Diamond Member
Jun 22, 2000
5,468
0
0
You could do an infinite loop and make one of the switch statements be a way to end the loop. Something like:

bool exit = false;
while (true)
{
int selection
// print out menu here

switch(selection)
{
// cases...
case (4):
exit = true;
break;
default:
cout << "try again: " << endl;
break;
}
if (exit == true)
break;
}
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
You're missing some code there. I can't see the bottom of the switch or while statements.

If you intend the loop to continue to run and process input then take out the return statements. Once one of those is executed you leave the current function call context, and since that is main() you exit your program.

The basic pattern you're looking for is:

// Get some input into Sel
while ( Sel == 1 || Sel == 2 || Sel == 3 )
{
switch( Sel )
{
case 1 :
// do some stuff
break;

case 2 :
// do some stuff
break;

// etc.
}
// get some more input into Sel
}
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Markbnj
You're missing some code there. I can't see the bottom of the switch or while statements.

If you intend the loop to continue to run and process input then take out the return statements. Once one of those is executed you leave the current function call context, and since that is main() you exit your program.

The basic pattern you're looking for is:

// Get some input into Sel
while ( Sel == 1 || Sel == 2 || Sel == 3 )
{
switch( Sel )
{
case 1 :
// do some stuff
break;

case 2 :
// do some stuff
break;

// etc.
}
// get some more input into Sel
}

do
{

// get some input

switch(sel)
...
...
...
} while(sel == 1 || sel == 2 || sel == 3)
 

stevf

Senior member
Jan 26, 2005
290
0
0
tfinch that looks ok for the do while - now that i have a chance to look at it option1 &2 of the switch does some calculations and 3 looks to exit. If you have to use the while loop and not the do while you would try something like this if you want option 3 to end the loop:

bool endLoop = false;
while (endLoop == false) {
int choice;
cout << "get some input - 1,2,3"
cin >> choice;
switch(choice) {
case 1:
//do stuff
break;
case 2:
//do stuff
break;
case 3:
endLoop = true;
}//end switch
}// end while
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
One of my biggest inabilities to adapt to C++ is the nature of i/o return values.

Couldn't you just test cin for a false/not false value?

It's return type is a istream object, according to msdn2, which doesn't mean much to me (hence, my inability to grasp the nuances of C++), but wouldn't it be NULL or 0 if the istream is nothing?
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: aCynic2
One of my biggest inabilities to adapt to C++ is the nature of i/o return values.

Couldn't you just test cin for a false/not false value?

It's return type is a istream object, according to msdn2, which doesn't mean much to me (hence, my inability to grasp the nuances of C++), but wouldn't it be NULL or 0 if the istream is nothing?

Cin returns a Cin that is why this type of syntax works

cin >> s >> q >> j;

http://www.cplusplus.com/reference/iostream/istream/

Provides a lot of methods to determine the current state of the istream.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Just some quick nit picks, while everyone answered you question, I think I'll just through in my 2 cents.

You should note that everytime you say
cout << "something " << endl;
cout << "something " << endl;

each of those "something " statements are stored in their own individual memory slot (hopefully I'm not going beyond what you have done). That means you essentially have char a[] = "something "; and char b[] = "something "; It is fairly wasteful. You can impress your teacher by declaring one const char[] at the begining of your function and then doing a loop for all the values you need. So,

const char msg1[] = "Voltage across R";
while (short i < 3)
{
cout << msg1 << i << "= " << curr * R << endl;
}

Looks a bit cleaner and results in slightly smaller code (more important when you are making 100 classes).
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Cogman
Just some quick nit picks, while everyone answered you question, I think I'll just through in my 2 cents.

You should note that everytime you say
cout << "something " << endl;
cout << "something " << endl;

each of those "something " statements are stored in their own individual memory slot (hopefully I'm not going beyond what you have done). That means you essentially have char a[] = "something "; and char b[] = "something "; It is fairly wasteful. You can impress your teacher by declaring one const char[] at the begining of your function and then doing a loop for all the values you need. So,

const char msg1[] = "Voltage across R";
while (short i < 3)
{
cout << msg1 << i << "= " << curr * R << endl;
}

Looks a bit cleaner and results in slightly smaller code (more important when you are making 100 classes).


Although you are correct that using const strings is good coding practice it has nothing to with memory. The C complier treat the inline string the same way it treats your const char msg1.

Unless allocated on the heap all variables/constants will always take up their extract size.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Originally posted by: smack Down
Originally posted by: Cogman
Just some quick nit picks, while everyone answered you question, I think I'll just through in my 2 cents.

You should note that everytime you say
cout << "something " << endl;
cout << "something " << endl;

each of those "something " statements are stored in their own individual memory slot (hopefully I'm not going beyond what you have done). That means you essentially have char a[] = "something "; and char b[] = "something "; It is fairly wasteful. You can impress your teacher by declaring one const char[] at the begining of your function and then doing a loop for all the values you need. So,

const char msg1[] = "Voltage across R";
while (short i < 3)
{
cout << msg1 << i << "= " << curr * R[ i ] << endl;
}

Looks a bit cleaner and results in slightly smaller code (more important when you are making 100 classes).

Although you are correct that using const strings is good coding practice it has nothing to with memory. The C complier treat the inline string the same way it treats your const char msg1.

Unless allocated on the heap all variables/constants will always take up their extract size.

It is a good coding practice, but it really does make a difference in memory as well. The compiler will treat each hello in

cout << "hello";
cout << "hello";

as two separate variables, that means it is allocating memory and space (as well, when the program is compiled it creates 2 different hellos in the code).

I agree that the impact is small, and with modern computers really a non-factor, but I'm against bloat in any form and wish other programmers would take a more active stance in reducing code size and memory foot print.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Cogman
Originally posted by: smack Down
Originally posted by: Cogman
Just some quick nit picks, while everyone answered you question, I think I'll just through in my 2 cents.

You should note that everytime you say
cout << "something " << endl;
cout << "something " << endl;

each of those "something " statements are stored in their own individual memory slot (hopefully I'm not going beyond what you have done). That means you essentially have char a[] = "something "; and char b[] = "something "; It is fairly wasteful. You can impress your teacher by declaring one const char[] at the begining of your function and then doing a loop for all the values you need. So,

const char msg1[] = "Voltage across R";
while (short i < 3)
{
cout << msg1 << i << "= " << curr * R[ i ] << endl;
}

Looks a bit cleaner and results in slightly smaller code (more important when you are making 100 classes).

Although you are correct that using const strings is good coding practice it has nothing to with memory. The C complier treat the inline string the same way it treats your const char msg1.

Unless allocated on the heap all variables/constants will always take up their extract size.

It is a good coding practice, but it really does make a difference in memory as well. The compiler will treat each hello in

cout << "hello";
cout << "hello";

as two separate variables, that means it is allocating memory and space (as well, when the program is compiled it creates 2 different hellos in the code).

I agree that the impact is small, and with modern computers really a non-factor, but I'm against bloat in any form and wish other programmers would take a more active stance in reducing code size and memory foot print.

I'm willing to bet you are wrong. Nothing in the language specification would require two identical const char* to be in two different places in memory. Of course nothing requires them to share the same space. This applies to both methods of defining the string literal. Is valid for the complier to have multiple copies of the same constant. This may happen if it is trying to reduce the working set.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: tfinch2
Originally posted by: Markbnj
You're missing some code there. I can't see the bottom of the switch or while statements.

If you intend the loop to continue to run and process input then take out the return statements. Once one of those is executed you leave the current function call context, and since that is main() you exit your program.

The basic pattern you're looking for is:

// Get some input into Sel
while ( Sel == 1 || Sel == 2 || Sel == 3 )
{
switch( Sel )
{
case 1 :
// do some stuff
break;

case 2 :
// do some stuff
break;

// etc.
}
// get some more input into Sel
}

do
{

// get some input

switch(sel)
...
...
...
} while(sel == 1 || sel == 2 || sel == 3)

That's viable as well, as long as it is okay to process the switch statement the first time without knowing whether the input meets the loop conditions or not. It's not precisely the logic the OP said he was attempting, though.

Although you are correct that using const strings is good coding practice it has nothing to with memory. The C complier treat the inline string the same way it treats your const char msg1.

Also, I believe most compilers will now collapse multiple identical string literals within a compilation unit.
 

MrPickins

Diamond Member
May 24, 2003
9,088
723
126
Originally posted by: Markbnj
Originally posted by: tfinch2

do
{

// get some input

If (input is invalid)
error statement;
else
{


switch(sel)
...
...
...

}
} while(sel == 1 || sel == 2 || sel == 3)

That's viable as well, as long as it is okay to process the switch statement the first time without knowing whether the input meets the loop conditions or not. It's not precisely the logic the OP said he was attempting, though.

Although you are correct that using const strings is good coding practice it has nothing to with memory. The C complier treat the inline string the same way it treats your const char msg1.

Also, I believe most compilers will now collapse multiple identical string literals within a compilation unit.

This is how I've been doing it in my entry level c++ classes.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Well, not sure that this proves anything, but I did a quick test with MS visual studios to see if it really did take up more space I did 2 tests. One was 2000 lines of cout << "hello"; The other was 2000 "hello"'s in a cout (IE cout << "hello" << "hello" ... and the other was a while loop.

The while loop was the smallest of the three, weighing in at 5kb. the << "hello" etc was the next smallest weighing in at 25kb and the cout << "hello"; was by far the largest, going to a whooping 119 kb. Im going to have to agree with smack Down though at say that the resulting file should have been much bigger had I been right. The only reason I believe the while loop was the smallest is because it used the fewest amount of instructions.

The lesson for this, though is when you are writing code, you should use couts sparingly for code space, and const for good coding practice (makes it easier to change messages in the future).
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
Originally posted by: Cogman
Well, not sure that this proves anything, but I did a quick test with MS visual studios to see if it really did take up more space I did 2 tests. One was 2000 lines of cout << "hello"; The other was 2000 "hello"'s in a cout (IE cout << "hello" << "hello" ... and the other was a while loop.

The while loop was the smallest of the three, weighing in at 5kb. the << "hello" etc was the next smallest weighing in at 25kb and the cout << "hello"; was by far the largest, going to a whooping 119 kb. Im going to have to agree with smack Down though at say that the resulting file should have been much bigger had I been right. The only reason I believe the while loop was the smallest is because it used the fewest amount of instructions.

The lesson for this, though is when you are writing code, you should use couts sparingly for code space, and const for good coding practice (makes it easier to change messages in the future).

All you need to do is compare the pointers:

const char *one = "hello";
const char *two = "hello";
const char three[] = "hello";

std::cout << std::boolalpha;
std::cout << (one == two) << std::endl;
std::cout << (one == three) << std::endl;
std::cout << (two == three) << std::endl;

In VC++ (and any sane compiler, I would guess), it will print "true false false" because one and two point to the same location in memory.

Notice that three is at a difference address (on the stack, instead of in read-only memory), since it's declared as an array rather than a pointer. This is inefficient, since space on the stack is allocated, then "hello" is copied from read-only memory into it. Always use pointer syntax unless you have a good reason not to.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |