English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

void function2() // Retail sale
{char cont1 = char();
bool flag1 = true;
while ( true )
{char cont = char();
bool flag = true;
while ( true)
{int unit = int();
double total = double();
cout << "How many unit do you want? ";
cin >> unit;
if (cin.fail())
{//cout << "Invalid data" << endl << endl;
cin.clear();
????????????????????????????????????????????
(What do I put to make the loop keep going?)
????????????????????????????????????????????
cout << endl;
}
if ( unit > 0)
{total = unit * 15;
cout << total << "$" << endl;}

cout << "Continue? ";
cin >> cont; //system("pause");
cout << endl;
if ( cont == 'y' || cont == 'Y' )
{flag = true;}
else
{flag = false;}

}//end while loop

2006-10-27 15:29:41 · 3 answers · asked by Jackie...boy 1 in Computers & Internet Programming & Design

3 answers

I'm not exactly sure what you're asking, but the 'continue' keyword will jump execution back to the beginning of the loop. I think that's what you're after here. For example:

while(true) {
cout << "A" << endl;
cout << "B" << endl;
continue;
cout << "C" << endl;
}

C would never be printed.

Alternately, if you're looking for a way to break -out- of the loop (which I can also see you wanting given the semantics of your program), use 'break'. It works like 'continue, only it automatically jumps to the first statement outside your loop.

2006-10-27 16:11:27 · answer #1 · answered by Julie 2 · 0 0

I do not think you have to put anything in where you say to place something in. The problem that I can see is, if there is an error in cin, it will go into the if statement, as normal.

What you do not want is for it to come out of that if statement and then proceed to do the unit. You would be best advised placing the code that runs when there is no error in cin in an else statement after the if.

Therefore the program will run like this
while true, infinite loop, does not stop.
get input from cin
if error in cin
inform user, clear error
else
do wahtever is needed
end if
ask if user wants to continue.
You can do one of two things here.

If you just want the whole function to end when the user does not want to continue, you can remove the flag = true; and flag = false; parts, and instead of these lines
if ( cont == 'y' || cont == 'Y' )
{flag = true;}
else
{flag = false;}

place these ones
if (cont != 'y' && cont != 'Y')
return;

What that says is, if not a little y and not a big y, in other words, the user has not types 'y' or 'Y', then stop the function.

So your code should look like this:
void function2() // Retail sale
{ //char cont1 = char(); // take this out, not the { though
// bool flag1 = true; // take this out.
// while ( true ) // take this out.
// {char cont = char(); // take this out.
// bool flag = true; // take this out.
while ( true)
{int unit = int();
double total = double(); // I do not understand why you have the total as a double, when you are only multiplying it by an integer 15 below. Anyhow.
cout << "How many unit do you want? ";
cin >> unit;
if (cin.fail())
{cout << "Invalid data" << endl << endl;
cin.clear();
// ??????????????????????????????... // nothing needs to go here :)
// (What do I put to make the loop keep going?)
// ??????????????????????????????...
cout << endl;
}
else // this is new
{ // this is new
if ( unit > 0)
{total = unit * 15;
cout << total << "$" << endl;}
}
}
cout << "Continue? ";
cin >> cont; //system("pause");
cout << endl;
if ( cont != 'y' && cont != 'Y' )
return
} //end while loop // this is the proper end of the while loop
} // this is the end of the function

2006-10-31 11:48:11 · answer #2 · answered by Mark aka jack573 7 · 0 0

seems you have 2 while loops and the argument is "true". What you need to do is change the "while(true)" to while(flag) or while(flag1). I dont see where flag1 gets changed to false though, maybe thats what you need.

2006-10-28 09:19:25 · answer #3 · answered by justme 7 · 0 0

fedest.com, questions and answers