Loop in a selection problem.

Pages: 12
I couldn't find any goto equivalent, no loops could work here.

Eh??
Any kind of loop could work here. Using goto is not good.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string nm, gm, q;
	string age;
    int myAge, stars, c=0;
    cout << "What's your name? ";
    getline (cin,nm);
    cout << "Hello, " << nm << "!" << endl;

while (true) {
      cout << endl << "What is your age? ";
    getline(cin, age);
	 // This code converts from string to number safely.
   stringstream myStream(age);
   if (myStream >> myAge) {
     break;
   }
   cout << "Enter a number, dumbass." << endl;
 }//while true
   
	
	return 0;
}


This code does ok, but if you type in for 'age' "45 ducks" it converts the string to 45, instead of telling you that you are quackers.
Last edited on
Thank you, all of you for helping me. It really improved my coding skills. I completely refrained from using any gotos in the whole code, sure, it took me some time to use loops in place of it but it worked in the end. However, there's still one thing remaining that I want to fix. It's the when the program goes into an infinite loop when I enter a number in the variable stars. When I use your method of using a string, another problem comes up. Anyway to fix it?
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string nm, gm, q, age;
    int myAge, stars, c=0;
    cout << "What's your name? ";
    getline (cin,nm);
    cout << "Hello, " << nm << "!" << endl;
    while (true) 
    {
        cout << endl << "What is your age? ";
        getline(cin, age);
        stringstream myStream(age);
        if (myStream >> myAge) 
        {
            break;
        }
    cout << "Enter a number, dumbass." << endl;
    } 
    if (myAge <= 13) 
    {
        cout << "Uh... Seriously?\nNevermind."; 
    }
    else if (myAge >= 30) 
    {
        cout << "You're too old for this!\n"; 
    }
    else 
    { 
        cout << "You're just about right!\n"; 
    }
        cout << endl << "Anyway, you want to play a game? (Y/N)" << endl;
    //lol:
    cin >> gm;
    while (true)
    {
    	if (gm == "y" || gm == "Y") 	
    	{ 		
            cout << "How many stars do you want?" << endl; 
            cin >> stars; 		
            for (c=0;c<=stars;++c) 		
                { 			
                    cout << "*"; 		
                } 	
            cout << "\nDo you want more stars?\n";
            cin >> gm;
            //goto lol;
    	}	
    	else if (gm == "n" || gm == "N") 
    	{ 		
    	    cout << endl << "OK."; 
    	    break;
  		}
    	else 
    	{
    	    cout << "Invalid input, please enter 'Y' for yes or 'N' for no. ";
    	    cin >> gm;
    	    //goto lol;
    	}
    }
    cout << endl << "Thanks for talking with me. Have a nice day!"; 
}
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string nm, gm, q, age;
    int myAge, stars, c=0;
    cout << "What's your name? ";
    getline (cin,nm);
    cout << "Hello, " << nm << "!" << endl;
    while (true) 
    {
        cout << endl << "What is your age? ";
        getline(cin, age);
        stringstream myStream(age);
        if (myStream >> myAge) 
        {
            break;
        }
    cout << "Enter a number, dumbass." << endl;
    } 
    if (myAge <= 13) 
    {
        cout << "Uh... Seriously?\nNevermind."; 
    }
    else if (myAge >= 30) 
    {
        cout << "You're too old for this!\n"; 
    }
    else 
    { 
        cout << "You're just about right!\n"; 
    }
        cout << endl << "Anyway, you want to play a game? (Y/N)" << endl;
    //lol:
    cin >> gm;
    while (true)
    {
    	if (gm == "y" || gm == "Y") 	
    	{ 		
            cout << "How many stars do you want? "; 
            cin >> stars; 		
            for (c=0;c<stars;++c) 		
                { 			
                    cout << "*"; 		
                } 	
            cout << endl << "\nDo you want more stars? (Y/N) ";
            cin >> gm;
            //goto lol;
    	}	
    	else if (gm == "n" || gm == "N") 
    	{ 		
    	    cout << endl << "OK."; 
    	    break;
  		}
    	else 
    	{
    	    cout << "Invalid input, please enter 'Y' for yes or 'N' for no. ";
    	    cin >> gm;
    	    //goto lol;
    	}
    }
    cout << endl << "Thanks for talking with me. Have a nice day!"; 

	return 0;
}

Mostly just some cosmetic changes, but as for the number of stars... I took out the '=' sign.
Last edited on
Topic archived. No new replies allowed.
Pages: 12