|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#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." |
|
|
|
|
|
#2 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#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? |
|
|
|
|
|
#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?
|
|
|
|
|
|
#5 | ||
|
Its the Dark Side!
|
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); HTH
__________________
CN ![]()
|
||
|
|
|
|
|
#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?
|
|
|
|
|
|
#7 |
|
Its the Dark Side!
|
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");
}
HTH |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|