Thursday, July 30, 2009

C++ help please?

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 %26lt;%26lt; "How many unit do you want? ";


cin %26gt;%26gt; unit;


if (cin.fail())


{//cout %26lt;%26lt; "Invalid data" %26lt;%26lt; endl %26lt;%26lt; endl;


cin.clear();


??????????????????????????????????????...


(What do I put to make the loop keep going?)


??????????????????????????????????????...


cout %26lt;%26lt; endl;


}


if ( unit %26gt; 0)


{total = unit * 15;


cout %26lt;%26lt; total %26lt;%26lt; "$" %26lt;%26lt; endl;}





cout %26lt;%26lt; "Continue? ";


cin %26gt;%26gt; cont; //system("pause");


cout %26lt;%26lt; endl;


if ( cont == 'y' || cont == 'Y' )


{flag = true;}


else


{flag = false;}





}//end while loop

C++ help please?
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 %26lt;%26lt; "A" %26lt;%26lt; endl;


cout %26lt;%26lt; "B" %26lt;%26lt; endl;


continue;


cout %26lt;%26lt; "C" %26lt;%26lt; 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.
Reply: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.
Reply: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' %26amp;%26amp; 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 %26lt;%26lt; "How many unit do you want? ";


cin %26gt;%26gt; unit;


if (cin.fail())


{cout %26lt;%26lt; "Invalid data" %26lt;%26lt; endl %26lt;%26lt; endl;


cin.clear();


// ??????????????????????????????... // nothing needs to go here :)


// (What do I put to make the loop keep going?)


// ??????????????????????????????...


cout %26lt;%26lt; endl;


}


else // this is new


{ // this is new


if ( unit %26gt; 0)


{total = unit * 15;


cout %26lt;%26lt; total %26lt;%26lt; "$" %26lt;%26lt; endl;}


}


}


cout %26lt;%26lt; "Continue? ";


cin %26gt;%26gt; cont; //system("pause");


cout %26lt;%26lt; endl;


if ( cont != 'y' %26amp;%26amp; cont != 'Y' )


return


} //end while loop // this is the proper end of the while loop


} // this is the end of the function

augustifolia

No comments:

Post a Comment