Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 02-16-2006, 03:05 PM   #1
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
C++ program fails to stop for input.

I have a situation in my program (C++) where I stated a point for collecting user input, but the program won't stop for it. Basically, it's like this...

int opt; //will be a number between 1 and 12
char oops;

do
{
if(opt < 1 || opt > 12) //opt is used before 'do'
{
cout << "\nYou have selected an illegal option.\n[C]ontinue or [q]uit? ";

cin >> oops; //somehow gets skipped

if(oops == 'Q' || oops == 'q')
{
cout << "\nGoodbye'\n\n";
stay = false;
}
else if(oops != 'C' && oops != 'c' && oops != 'Q' && oops != 'q')
{
cout << "Program terminated.\n\n";
stay = false;
}
}
if(stay == true)
{
// do some other stuff
}
}while(stay == true);


When 'oops' is meant to be requested that first time, the program completely skips it. So it says,

"You have selected an illegal option.
[C]ontinue or [q]uit? Program terminated."

I've tried having 'oops' generate output even just after I created the variable. The output itself is some character I can't easily generate on keyboard (dunno if even possible), but the integer equivalent comes out to be -52. How can I get the program to wait for a char input?
__________________
"Advancement is answering the questions, discovery is questioning the answers."
dataDude is offline   Reply With Quote
Old 02-16-2006, 03:10 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
The problem is that you don't initialize the variable opt to anything. The first thing your procedure does is test to see if opt is less than 1 or greater than 12. Since opt hasn't been assigned to yet, it could have any value at all; it almost certainly doesn't have a value between 1 and 12. Thus, you get the "Illegal option" prompt.

Get the input before you test the opt variable.
doctorgonzo is offline   Reply With Quote
Old 02-16-2006, 03:24 PM   #3
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
Sorry, I wasn't very clear about that. Right when I have the 'do' statement started, I had a comment that "//opt is used before 'do'". Opt is used in another place before the 'do' statement executes. It's sent to another function where the user is prompted for input.

Another thing I forgot to mention was that most of the time this whole thing does work. But say the user accidentally inputs '-' into the opt variable, that's when the program goes nuts. Any thoughts?
dataDude is offline   Reply With Quote
Old 02-16-2006, 05:40 PM   #4
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
I've almost figured everything out, though my solution is nothing simple. I decided to take that do statement and put it into a completely separate function and then changed the variable type for the input into a 2 element char array which then I have converted into an integer. It's more or less easier to check the input from the user this way, although I'm not quite sure how to figure out how to determine if an element in the array is numeric or not. Any thoughts on that?
dataDude is offline   Reply With Quote
Old 02-16-2006, 05:46 PM   #5
Its the Dark Side!
 
ComputerNut's Avatar
 
Join Date: Jan 2004
Location: Kitchener, Ontario, Canada
Posts: 1,111
Send a message via MSN to ComputerNut
in our programming class, our prof doesnt like the cin, cout functions. Instead you could use more useful. if youre collecting a certain array of integers you could do this:
Code:
char buffer[10]="";
fgets(buffer,10,stdin);
opt=atoi(buffer);
Buffer is an array of char, so essentially a 10 character string with a null terminator. fgets will input the characters from the keyboard (thats the job of the stdin) into the buffer. the atoi converts the string to an integer and sends it to opt. sounds a little grand, but ive known it to be more foolproof, because you can do error checking with those lines.

HTH
__________________
CN
My Rig: "Dark Lord"
Asus P5B - Intel Core 2 Duo E6400 - 1GB DDR2 667 RAM - Seagate SATAII 80GB HDD - Seagate SATAII 250GB HDD - Lite-On DVD -/+ RW Drive - nVidia GeForce 7600 GS - ATi TV Wonder VE

ComputerNut is offline   Reply With Quote
Old 02-16-2006, 05:51 PM   #6
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
More or less that's what I did. I use cin liberally. I mostly try other methods when I get an error from something that cin did or did not do. Right now I'm trying to find a function that I can use on the input string that will determine if an element is numeric or not. Got any ideas?
dataDude is offline   Reply With Quote
Old 02-16-2006, 06:57 PM   #7
Its the Dark Side!
 
ComputerNut's Avatar
 
Join Date: Jan 2004
Location: Kitchener, Ontario, Canada
Posts: 1,111
Send a message via MSN to ComputerNut
There is a function included in wchar.h or ctype.h called "isalpha". essentially it checks if an integer represents an alpha character. Heres an example (going on the assumption that your keyboard input goes into a char variable):

Code:
int a = 0;
char input = 0;

a = isalpha( (int) input );

if(a != 0){
   printf("Is a character\n");
}else{
   printf("Is not a character\n");
}
Now if you are dealing with the input variable being of type char, that is the reason why that (int) cast is there. I used this function in a project awhile ago, and I think i needed the use of that cast.

HTH
ComputerNut is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:05 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2