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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdlib>
#include <iostream>

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
1
2
3
4
while(true)
{
    //code you want to loop
}
Last edited on
You are my favorite person on the internet right now. Thanks so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>

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().

Additionally, your conditions for your loops are incorrect; you are current checking three conditions:
- 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:

1
2
3
4
5
6
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.