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?
#include <cstdlib>
#include <iostream>
usingnamespace 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");
}
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.
#include <cstdlib>
#include <iostream>
usingnamespace 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