[Solved]Program endlessly loops after input.

Hey, I am new to the programming world and I need some help with a program that I am writing. The program is a game where the user inputs a number between 1 and 10 and the program generates a random number within that same criteria. The problem arises when the user inputs a character such as a letter or a symbol. It will loop endlessly until the command line is closed. I apologize in advance if my code is very amateurish. I am just now learning how to code at a community college and we've only learned flowcharting and pseudocode so far. I'll begin learning actual code starting this week. Also this code is C++. Thanks for the help and I am open to any tips.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>   
#include <stdlib.h>  
#include <time.h> 
using namespace std;

int _tmain()
{
	cout<< "\t\t\t\t ******************" <<endl;
	cout<< "\t\t\t\t *    WELCOME!    *" <<endl;
	cout<< "\t\t\t\t *  LET'S PLAY!   *" <<endl;
	cout<< "\t\t\t\t ******************" <<endl;
	
	int num1 = 0, ram1 = 0;
	char answer;
	srand (time(NULL)); //Initialize randomization
	
	LOOP: // GOTO LOOP function leads here
	
	cout<< "Please enter a number from 1 to 10: ";
	cin>>num1;
	if (num1>10)
	{
		cout << "Number is too big!" <<endl; 
		goto LOOP;
	}
	else if (num1<1)
	{
		cout << "Number is too small!" <<endl;
		goto LOOP;
	}
	ram1 = rand() % 10+1; //Generates the random number between 1 and 10
	cout<< "Your number: " <<num1<<endl;
	cout<< "Random Number: " <<ram1<<endl;
	if (num1 == ram1)
	{
		cout << "You Win!" <<endl;
	} 
	else 
	{
		cout << "You Lose!" <<endl;
	}
	
	END: //GOTO END function leads here
	
	cout << "Do you want to play again?(Y/N) ";
	cin >> answer;
	if (answer == 'Y' || answer == 'y')
	{
		goto LOOP;
	}
	else if (answer == 'N' || answer == 'n')
	{
		return 0;
	}
	else
	{
		cout << "Invalid input!" <<endl;
		goto END;
	}
}
Last edited on
closed account (SECMoG1T)
when you read a character into a variable expecting an integral value, an error state is set on the stream immediately, the looping behavior you're getting is because the read will not be successful if an error state have been set on a stream, so your program will always be trying to read from the stream with no success, to solve that you'll need to clear the error state by calling the stream clear function cin.clear()

Advice : gotos will never be as friendly as they might seem, learn to avoid them as soon as
possible, the language offers a variety of loops that will achieve what you intend.

Also std c++ have no
1
2
3
#include <stdio.h>   
#include <stdlib.h>  
#include <time.h>  
such headers instead use this
1
2
#include <cstdlib>
#include <ctime> 


link to a problem similar to yours
http://www.cplusplus.com/forum/beginner/146149/
Last edited on
Are you asking how to validate user input before setting the character. If so you can do something as easy as setting the character then testing its value using a condition statement and if after testing the character it is set to something other than the requested values you could reset the character.
I have fixed the problem. I did some asking around and a little more research have come upon the fix. Also got rid of the spaghetti code that is caused by the goto functions. Not sure if generated more but the program has a few more fancy features.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>   
#include <sstream>
using namespace std;

int _tmain()
{	
	char answer;
	srand (time(NULL));
	
	system ("color f0");

	do
	{
		
		do
		{

			string name;
			int ram1 = 0, max = 10, min = 1;
			float num1 = 0;

			cout<< "\t\t\t\t ******************" <<endl;
			cout<< "\t\t\t\t *    WELCOME!    *" <<endl;
			cout<< "\t\t\t\t *  LET'S PLAY!   *" <<endl;
			cout<< "\t\t\t\t ******************" <<endl;

			do
			{
					cout << "Please enter a number from 1 to 10: ";
					cin >> name;
					stringstream ss;
					ss << name;
					ss >> num1;

					if(ss.good()) 
					{
						cerr << "No Valid Number" << endl;
					}
					else if(num1 == 0 && name[0] != '0') 
					{
						cerr << "No Valid Number" << endl;
					}
					
					if (num1 < 1)
					{
						cout << "Number is too small!" << endl;
						system ("pause");
						system ("cls");
						cout<< "\t\t\t\t ******************" <<endl;
						cout<< "\t\t\t\t *    WELCOME!    *" <<endl;
						cout<< "\t\t\t\t *  LET'S PLAY!   *" <<endl;
						cout<< "\t\t\t\t ******************" <<endl;
					}
					else if ( num1 > 10)
					{
						cout << "Number is too big!" << endl;
						system ("pause");
						system ("cls");
						cout<< "\t\t\t\t ******************" <<endl;
						cout<< "\t\t\t\t *    WELCOME!    *" <<endl;
						cout<< "\t\t\t\t *  LET'S PLAY!   *" <<endl;
						cout<< "\t\t\t\t ******************" <<endl;
					}
			}
			while (num1<min || max<num1);
			
			ram1 = rand() % 10+1;
			cout<< "Your number: " <<num1<<endl;
			cout<< "Random Number: " <<ram1<<endl;
	
			if (num1 == ram1)
			{
				cout << "You Win!" <<endl;
			} 
			else 
			{
				cout << "You Lose!" <<endl;
			}
			

			do
			{
				cout << "Do you want to play again?(Y/N) ";
				cin >> answer;
				
				if (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n')
				{
					cout << "Wrong Input!" << endl;
				}
				
				system ("pause");
				system ("cls");
				cout<< "\t\t\t\t ******************" <<endl;
				cout<< "\t\t\t\t *    WELCOME!    *" <<endl;
				cout<< "\t\t\t\t *  LET'S PLAY!   *" <<endl;
				cout<< "\t\t\t\t ******************" <<endl;
			}
			while (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n');
			
			system ("cls");
		}
		while (answer == 'Y' || answer == 'y');
		
		return 0;
	}
	while (answer == 'N' || answer == 'n');

}


Thanks for your help and insight!
Thanks for the solution. I had a similar problem but your solution allowed me to actually have an idea how to fix my issue as well.
by the way why #include "stdafx.h" log say fatal error in stdafx.h no such file or directory
Not sure, could be related to the compiler you're using? I'm no expert sorry.
Topic archived. No new replies allowed.