My Break not Working

This is a program that reports current 'state' location and then moves to different state if user input is correct. Before I enter the input/state loop, I wanted to do an input validation loop that would check for a number and then loop, and if not a number, program terminates or breaks. But the program seems to ignore the break in the else statement and then just runs an infinite loop. I am very much a beginner and would appreciate any input.

// homework_assign_2.cpp : main project file.

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

int input; //assign integer input
int state=0; //assign integer state

int function (int state, int input) //the function 'function' and the parameters state and input
{
switch (input) //switch loop to check the value of input
{
case 0:
if (state==0) //if input equals 0, and if the state is 0,
cout << "System is exiting from state_exit. "; //output exiting from state_exit
else if (state==1) //if input is 0 and the state is 1
{
cout << "System is moving from state_" << state; //output the system is moving from where it is
cout << " to state_" << input << " ."; //to where it is going
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 1:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 2:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 3:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
default:
cout << "The system is currently in state_" << state << " .";
cout <<endl;
}
return state;
cout << endl;
}
int main()
{
string str="Y";

while (str=="Y")
{
cout << "The current state is: " << state <<endl;
cout << "Please input the number of the state you would like to go to. "<<endl;
cout << "Enter 0 for state_exit."<<endl;
cout << "Enter 1 for state_1."<<endl;
cout << "Enter 2 for state_2."<<endl;
cout << "Enter 3 for state_3. "<<endl;
cout << "Please enter the your desired state: "<<endl;
cin >> input;

if (input>=0&&input<=9)

function(state,input);

else
break;

if (input<=3)
state=input;
else
{
state=state;
}
cout <<endl;
cout << "Would you like to enter another state input? " <<endl;
cout <<endl;
cout << "Y to continue or N to exit. ";
cin >> str;
if (str=="y")
str="Y";
if (str=="n")
str="N";
cout <<endl;

}
system("Pause");
return 0;
}
Please use code tags to make your code readable.
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

int input; //assign integer input
int state=0; //assign integer state

int function (int state, int input) //the function 'function' and the parameters state and input
{
switch (input) //switch loop to check the value of input
{
case 0: 
if (state==0) //if input equals 0, and if the state is 0, 
cout << "System is exiting from state_exit. "; //output exiting from state_exit
else if (state==1) //if input is 0 and the state is 1
{
cout << "System is moving from state_" << state; //output the system is moving from where it is
cout << " to state_" << input << " ."; //to where it is going
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 1:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 2:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
case 3:
if (state==0)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
else if (state==3)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
break;
default:
cout << "The system is currently in state_" << state << " .";
cout <<endl;
}
return state;
cout << endl;
}
int main()
{
string str="Y";

while (str=="Y")
{
cout << "The current state is: " << state <<endl;
cout << "Please input the number of the state you would like to go to. "<<endl;
cout << "Enter 0 for state_exit."<<endl;
cout << "Enter 1 for state_1."<<endl;
cout << "Enter 2 for state_2."<<endl;
cout << "Enter 3 for state_3. "<<endl;
cout << "Please enter the your desired state: "<<endl;
cin >> input;

if (input>=0&&input<=9)

function(state,input);

else 
break;

if (input<=3)
state=input;
else
{
state=state;
}
cout <<endl;
cout << "Would you like to enter another state input? " <<endl;
cout <<endl;
cout << "Y to continue or N to exit. ";
cin >> str;
if (str=="y")
str="Y";
if (str=="n")
str="N";
cout <<endl;

}
system("Pause");
return 0;
}


line 34, try putting the break before you close the bracket.

as with line 56, 78, 100 and 128
I fixed the problem by checking the input with the following after line 121

if (!cin)
break;

This line checks for if there was not an integer input, and if not, the next line breaks the program.

I also refined some of the logic after that line to better refine the acceptable input, but problem solved. Thanks for all the input.
Topic archived. No new replies allowed.