472,127 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Switch inside While loop... User error!

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
9 3813
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
"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

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
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
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
//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
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
I would like to thank everyone for your all of your help...

Thanks!
Feb 12 '07 #9
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.

Similar topics

5 posts views Thread by Neil Zanella | last post: by
13 posts views Thread by webzila | last post: by
14 posts views Thread by serrand | last post: by
25 posts views Thread by v4vijayakumar | last post: by
14 posts views Thread by subramanian100in | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.