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;
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace 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
elseif (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
}
elseif (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (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 << " .";
}
elseif (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (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 << " .";
}
elseif (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (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 << " .";
}
elseif (state==1)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (state==2)
{
cout << "System is moving from state_" << state;
cout << " to state_" << input << " .";
}
elseif (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);
elsebreak;
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.