#include <iostream>
usingnamespace std;
int main()
{
repeat:
cout << "Enter two integers: " << endl;
int num1;
int num2;
cin >> num1;
cin >> num2;
cout << num1 << " X " << num2 << " = " << num1 * num2 << endl;
cout << "Do you wish to repeat the operation? " << endl;
string repeat1 = 'y';
cin >> repeat1;
if (repeat1 == 'y')
goto repeat;
cout << "GOODBYE!" << endl;
return 0;
}
When I use char repeat1 = 'y';, then it works perfectly. However, I want that 'y' to become 'yes'. I used string, but it gives a whole bunch of errors. Char doesn't work because it brings a random string of letters. What is wrong with this code?
First, gotos. Goto is bad, because it makes code look like spaghetti. Use loops instead of gotos.
Secondly - there are two types of ' signs: 'x' - it's a SINGLE character "something" - it's a STRING
so you can't check for 'yes'. You should check for "yes".
Thanks guys. I am following a book and they were explaining loops and they started with goto. I tried to modify it, but to no avail. My main problem was string, which was explained wonderfully.
#include <iostream>
#include <string> // You're missing this.
#include <limits> // For my addition at the bottom.
using std::cout; // do this instead of "using namespace".
using std::endl;
using std::cin;
using std::string;
int main()
{
// seriously never use 'goto'. Please use loops.
while (true)
{
cout << "Enter two integers: " << endl;
int num1(0); // Initialize your variables...
int num2(0); // ...and use constructor in doing so.
string repeat1("");
cin >> num1;
cin >> num2;
cout << num1 << " X " << num2 << " = " << num1 * num2 << endl;
cout << "Do you wish to repeat the operation? " << endl;
cin >> repeat1;
if (repeat1 != "y") break;
}
cout << "GOODBYE!" << endl;
// This is just to keep the console open.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}