### Looping infinitely?

Hey guys,

This is a homework assignment for a C++ course I'm taking, I'm fairly new to C++ but I'm familiar with other languages such as Lua, some Java, and C#.

The goal is to input how much a cereal box weighs, have the program tell you how many boxes you'd need to equal a metric ton, then allow you to repeat the calculation with a different variable for the weight.

I have everything figured out except for the part that allows you to repeat the calculation. I literally can't figure this out. I'm pretty sure I'm missing something pretty simple here. Any help?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include using namespace std; int main(int argc, char *argv[]) { float boxOunces, answer; char goAgain[50]; //I couldn't get it to work by declaring it as a string cout<<"Answer this question:\n"; int question; { cout<<"\nHow many ounces is one cereal box? "; cin>> boxOunces; cin.ignore(); // now we have the input answer = (2000/boxOunces); //now we divide... cout<<"You will need "<< answer <<" cereal boxes to make one metric ton!\n\n"; //now we have an answer! cout<<"Do you want to try again with another box of cereal? (Enter 'yes' or 'no'): "; cin>> goAgain; cin.ignore(); } if ( goAgain == "Yes" || "yes" || "y" ) { question; } else { cout<<"Uh oh, something's wrong!"; } cin.get(); system ("PAUSE"); } ``````

You know how to make a while loop where the condition is true right? This seems like experience you would already have from Lua, Java, and C# ;)
Haha yes, its been a while. I learned Lua when I was a kid playing a game called Roblox...haven't touched Lua once in about 8 years. I tried a "while true do" loop. I also tried repeating the main code within the if statement. Neither worked the way I wanted.
The syntax for loops is different in C++ than in Lua:
http://www.cplusplus.com/doc/tutorial/control/#while
 ``1234`` ``````while(true) { //code you want to loop }``````
Last edited on
You are my favorite person on the internet right now. Thanks so much!

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; int main(int argc, char *argv[]) { float boxOunces, howMany, partTon, percentTon; char goAgain[50]; //I couldn't get it to work by declaring it as a string cout<<"Answer this question:\n"; while (goAgain == "Yes" || "yes" || "y") { cout<<"\nHow many ounces does one cereal box weigh? "; cin>> boxOunces; cin.ignore(); if (boxOunces != partTon = (boxOunces/2000); percentTon = (partTon * 100); cout<<"One cereal box is "<< partTon <<" of one metric ton! That's "<< percentTon <<" percent!\n"; howMany = (2000/boxOunces); cout<<"You would need "<< howMany <<" cereal boxes to make one metric ton!\n\n"; cout<<"Do you want to try again with another box of cereal? (Enter 'yes' or 'no'): "; cin>> goAgain; cin.ignore(); if (goAgain != "Yes" || "yes" || "y") { cout<<"\nGoodbye\n\n"; system ("PAUSE"); return 0; } } }``````

Another question, how could implement a check against boxOunces to make sure it's a number? If anything other than a number is entered it crashes...
You can't compare character arrays with == to string literals like you are trying to do. I'd highly recommend just using the standard library's std::string type instead, but otherwise you should use strcmp().

- If goAgain == "Yes" (already problematic, as I mentioned above) evaluates to true
OR
- If the string literal "yes" evaluates to true
OR
- If the string literal "y" evaluates to true

See the issue?
Oh jeez. This is killing me.

I didn't know you couldn't do that with character arrays. Is this proper?

`string goAgain;`

What would you suggest doing about the conditions then? This doesn't work:

 ``123456`` ``````else { cout<<"\nGoodbye\n\n"; system ("PAUSE"); return 0; } cin.get(); ``````
Change

`while (goAgain == "Yes" || "yes" || "y") {`

to

`while (goAgain == "Yes" || goAgain == "yes" || goAgain == "y") {`

(assuming you are using std::string instead of char *)
Last edited on
Topic archived. No new replies allowed.