String variable?

I was writing a very, very piece of simple code.
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
  #include <iostream>
using namespace 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".

Cheers!
you can use the function strcmp: http://www.cplusplus.com/reference/cstring/strcmp/

or go with the other guys said, it's easier
Last edited on
OK, first you need to include the string header. Also, you are setting repeat1 to characters, rather than strings. Here is your code fixed up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main() {
start:
    std::cout << "Enter two integers: " << std::endl;
    int num1;
    int num2;
    std::cin >> num1 >> num2;

    std::cout << num1 << " X " << num2 << " = " << num1 * num2 << std::endl;
    std::cout << "Do you wish to repeat the operation? " << std::endl;
    std::string repeat;
    std::cin >> repeat;

    if (repeat == "yes" || repeat == "y" || repeat == "Y" || repeat == "Yes")
        goto start;

    std::cout << "GOODBYE!" << std::endl;
}


Normally, I would recommend against using goto as well, but for a simple test like this is doesn't matter.
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.
Here's my 2cents:

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
#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;
}
Last edited on
Goto is bad, because it makes code look like spaghetti.


How spaghetti? ( ´∀`)
Topic archived. No new replies allowed.