Switch inside While loop... User error!

C

Cybex

I am trying to get this to work but when ever I enter an proper
integer it just hangs. The Switch default seems to catch the improper
integers but the right ones are not triggering the way I thought they
would.

Any help would be appreciated...


#include <iostream>
#include <string>
using namespace std;

int main()
{
//declare variables
int intEmpID = 0;
string strEmpName = "";

//Gather inputs
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;

while (intEmpID >0) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
} //End switch
} // End while

//display outputs
cout << "The employee ID " << intEmpID << " belongs to: " <<
strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;

return 0;
} //end of main function
 
K

Kai-Uwe Bux

Cybex said:
I am trying to get this to work but when ever I enter an proper
integer it just hangs. The Switch default seems to catch the improper
integers but the right ones are not triggering the way I thought they
would.

Any help would be appreciated...


#include <iostream>
#include <string>
using namespace std;

int main()
{
//declare variables
int intEmpID = 0;
string strEmpName = "";

//Gather inputs
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;

while (intEmpID >0) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;

This break just exits the switch stament. It does not exit the ambient while
loop.
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
} //End switch
} // End while

//display outputs
cout << "The employee ID " << intEmpID << " belongs to: " <<
strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;

return 0;
} //end of main function
Possible fixes include:

a) Use of a boolean flag.
b) Use of goto.
c) Use of a function, e.g.:

#include <iostream>
#include <string>
using namespace std;

std::string get_name ( int & intEmpID ) {
while ( true ) {
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;
switch (intEmpID) {
case 1234:
return( "Sue Nguyen" );
case 1345:
return( "Janice Blackfeather" );
case 3456:
return( "Allen Kraus" );
case 4567:
return( "Margie O'Donnell" );
default :
cout << "Incorrect ID - Please try again" <<"\n";
}
}
}

int main()
{
//declare variables and get id:
int intEmpID = 0;
string strEmpName = get_name( intEmpID );

//display outputs
cout << "The employee ID " << intEmpID << " belongs to: " <<
strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;

return 0;
} //end of main function


Note that this is not totally satisfying since the function has too many
responsibilities; it looks up the string value and it issues requests for
changing the ID. It might be better to have a std::map<int,std::string>
that matches id-numbers and names and then the input validation could just
check whether that map has an entry with a given id-number.


Best

Kai-Uwe Bux
 
O

osmium

Cybex said:
I am trying to get this to work but when ever I enter an proper
integer it just hangs. The Switch default seems to catch the improper
integers but the right ones are not triggering the way I thought they
would.

Any help would be appreciated...


#include <iostream>
#include <string>
using namespace std;

int main()
{
//declare variables
int intEmpID = 0;
string strEmpName = "";

//Gather inputs
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;

while (intEmpID >0) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
} //End switch
}// End while
^^^^ move this down
//display outputs
cout << "The employee ID " << intEmpID << " belongs to: " <<
strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;

} // end while
to here
return 0;
} //end of main function

There may be other problems as well, but that looks plain wrong if I
understand your intent.
 
G

Grizlyk

Cybex said:
while (intEmpID >0) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;

with (intEmpID==1234) loop forever.
} //End switch
} // End while

write tail like this

default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
//add
continue;
} //End switch

//add
break;
} // End while

Are you sure that you want "while (intEmpID >0) {"?
 
C

Cybex

Are you sure that you want "while (intEmpID >0) {"?


I need a way for the user to exit the While loop... I thought if they
entered a negative number it would exit the loop.
 
C

Cybex

This is what I have now... It seems to work fro all situations.

#include <iostream>
#include <string>
using namespace std;

int main() {
//declare variables
int intEmpID = 0;
string strEmpName = "";

//Gather inputs
cout << "Enter the emplyee's ID: (Enter a negitive number to
exit.)";
cin >> intEmpID;

while (intEmpID > -1) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
continue;
} //End switch

//display outputs
cout << "The employee ID " << intEmpID << " belongs to: "
<< strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;
} // End while
return 0;
} //end of main function
 
S

shivguru

//hope this will help

#include <iostream>
#include <string>
using namespace std;

int main()
{
//declare variables
int intEmpID = 0;
string strEmpName = "";
bool correctID;

//Gather inputs
//how about cout << "Enter the emplyee's ID (-1 to stop): ";
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;

while (intEmpID > 0) // how about while (intEmpID != -1)
{
correctID = true;
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
correctID = false;
cout << "\nIncorrect ID - Please try again"
<<"\n";
//how about cout << "Re-enter the emplyee's ID (-1
to stop): ";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
} //End switch


//display outputs
if (correctID)
{
cout << "The employee ID " << intEmpID << " belongs to: "
<<strEmpName << " \n \n";
//how about cout << "Enter the emplyee's ID (-1 to stop): ";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;
}
} // End while
return 0;

}
 
J

Jerry Coffin

[ ... ]
int main()
{
//declare variables
int intEmpID = 0;
string strEmpName = "";

//Gather inputs
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;

while (intEmpID >0) {
switch (intEmpID) {
case 1234:
strEmpName = "Sue Nguyen";
break;
case 1345:
strEmpName = "Janice Blackfeather";
break;
case 3456:
strEmpName = "Allen Kraus";
break;
case 4567:
strEmpName = "Margie O'Donnell";
break;
default :
cout << "Incorrect ID - Please try again" <<"\n";
cout << "Re-enter the emplyee's ID: ";
cin >> intEmpID;
} //End switch
} // End while

//display outputs
cout << "The employee ID " << intEmpID << " belongs to: " <<
strEmpName << " \n \n";
cout << "Enter the next emplyee's ID: ";
cin >> intEmpID;

return 0;
} //end of main function

Try breaking this down to pseudo-code and see if the problem doesn't
become apparent. What we have is basically:

read employee ID
while != -1
lookup name
read another ID

Take particular note of the fact that the ID never changes inside of the
loop.

This is one reason to break your code up into smaller functions -- it
makes it _much_ easier to see the overall logic, without getting caught
up in all the details of each individual part. I realize you may be a
student who may not have used more than one function yet -- but if so,
this will (I hope) give you motivation when you do encounter them...
 
O

Old Wolf

std::string get_name ( int & intEmpID ) {
while ( true ) {
cout << "Enter the emplyee's ID: ";
cin >> intEmpID;
switch (intEmpID) {
// ....
default :
cout << "Incorrect ID - Please try again" <<"\n";
}
}
}

If std::cin enters an error state then this may loop
forever or cause undefined behaviour. You should also
return something or throw at exception if the input fails.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top