By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Switch inside While loop... User error!

P: n/a
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

Feb 12 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Cybex wrote:
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

Feb 12 '07 #2

P: n/a
"Cybex" writes:
>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.
Feb 12 '07 #3

P: n/a

Cybex wrote:
>
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) {"?

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/

Feb 12 '07 #4

P: n/a
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.

Feb 12 '07 #5

P: n/a
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

Feb 12 '07 #6

P: n/a
//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;

}
Cybex wrote:
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
Feb 12 '07 #7

P: n/a
In article <11**********************@a75g2000cwd.googlegroups .com>,
js******@gmail.com says...

[ ... ]
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...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 12 '07 #8

P: n/a
I would like to thank everyone for your all of your help...

Thanks!
Feb 12 '07 #9

P: n/a
On Feb 12, 2:35 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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.

Feb 12 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.