|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C++, quiting during runtime
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
Folding Fanatic
|
hey guys.
in class right now and writing a basic C++ program. what is the syntax to quit using the letter "q" or "Q" using a while loop? thanks
|
||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Programmer
|
I assume you mean that the user should be able to stop the program by pressing the "Q" key. In that case...
read input from the command line: char input; cin >> input; check to see if it's "Q": if(input == 'Q') break our of the while loop: { break; } Or something like that.
__________________
"He attacked everything in life with a mix of extraordinary genius and naive incompetence, and it was often difficult to tell which was which." Douglas Adams
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||||
|
Folding Fanatic
|
i have multiple variables inside a for loop, and im trying to find a way of calling ANY input instead of saying
if ((input == 'q') || (input2 == 'q') || (input3 == 'q')) { break; } but with more variables and with strings ![]()
|
||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||
|
Folding Fanatic
|
nevermind
![]() i could just use a function that will test for the inputs
|
||||||||||||
|
|
|
|
#5 (permalink) |
|
New to Overclock.net
|
In a 'for' loop the stop condition can be as simple or complex as you choose... although simpler is better. What you should be asking is 'why am I using a for loop here?'
A simple rule for using loops in C++ is: If you have a known or preset number of iterations, use a 'for' loop. You might want to terminate early if you find a match, but you know what the upper bound is. If you have a task to iterate an unknown number of times, and you might wish to skip the loop entirely, use a 'while' loop (tests on entry into loop) If you have a task to iterate an unknown number of times, but you wish to execute it at least once, then use a do..while loop. To answer your original question, you can use the old C functions strchr() or strcmp() defined in string.h. To reduce the number of tests, use the tolower() function to turn upper case into lower case prior to testing. If you have access to a string class like the MFC CStringT you could do something pretty awful like concatenate all the inputs into a single string and then use the CString.Find() method to search for the occurence of a Q Check out MSDN to see how it worksLast edited by Stormwolf : 05-28-08 at 03:55 PM. |
|
|
|
|
|
#6 (permalink) | |||||||||
|
Overclocker
|
you can concatenate the input strings together and seek for the bytes you want.
|
|||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
Programmer
|
or if you prefer the if method. If Input != null
__________________
If you can read this, thank a teacher. If you can read this in English, thank a veteran.
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Programmer
|
__________________
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|