473,665 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3943
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
responsibilitie s; it looks up the string value and it issues requests for
changing the ID. It might be better to have a std::map<int,st d::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 << "\nIncorrec t 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************ **********@a75g 2000cwd.googleg roups.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
10140
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 seems to reveal that there are still places where this is illegal. For instance, consider the following code snippet. int main(void) { int i = 0;
13
6207
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 microcontroller should display in the LCD the value of the voltage applied to the input of the ADC. The above procedure should only execute once the user has entered "1234" using a keypad that is attached to the 8051 microprocessor.
13
11239
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 statement? The break keyword will only come out of the switch, not the for-next loop.
14
6247
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; while (1) { res = 0; if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1) { aff_erreurs ("msgrcv", "Error when recieving message : %d", errno);
25
18376
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
3019
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 way I'm going about doing this might be a little off. I'd appreciate some help. Below is the code I have thus far but I'm not sure how to reference the user control within the foreach loop. <asp:Panel ID="pnlRosterProfile" runat="Server" />
14
13048
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
7
3356
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 parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
0
8438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.