473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3920
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
13
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.