New Posts  All Forums:Forum Nav:

C++, quiting during runtime

post #1 of 8
Thread Starter 
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
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
post #2 of 8
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.
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
post #3 of 8
Thread Starter 
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
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
post #4 of 8
Thread Starter 
nevermind

i could just use a function that will test for the inputs
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
Main Baby
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 G0 3.2Ghz Asus 750i P5N-D evga 9800gtx 512mb G.Skill 2x2gb PC-8500 DDR2 
Hard DriveOSMonitorPower
320gb 16mb cache Windows 7 Home Premium 64-bit Samsung SyncMaster 932BW 19" Apevia Warlock - 750 watt 
Case
CoolerMaster 690 
  hide details  
Reply
post #5 of 8
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 works
Edited by Stormwolf - 5/28/08 at 12:55pm
post #6 of 8
you can concatenate the input strings together and seek for the bytes you want.
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
post #7 of 8
or if you prefer the if method. If Input != null
Nightshade
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7-4930k Asus Rampage IV Extreme EVGA - Titan-X Superclocked EVGA - Titan-X 
RAMHard DriveHard DriveHard Drive
64 GB Corsair Vengance DDR3 OCZ Agility 3 Intel 540 240GB 7200 RPM Platter Drive 
Hard DriveCoolingOSMonitor
1TB Mushkin SSD Watercooled Windows 10 Pro x64 ASUS ROG Swift 
KeyboardPowerCaseMouse
Razer Black Widow Chroma Corsair RM1000 Corsair 900D Razer Deathadder Chroma 
Mouse PadAudioOther
Steelseries Soundblaster Z Speakers: Logitech z5500 
  hide details  
Reply
Nightshade
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7-4930k Asus Rampage IV Extreme EVGA - Titan-X Superclocked EVGA - Titan-X 
RAMHard DriveHard DriveHard Drive
64 GB Corsair Vengance DDR3 OCZ Agility 3 Intel 540 240GB 7200 RPM Platter Drive 
Hard DriveCoolingOSMonitor
1TB Mushkin SSD Watercooled Windows 10 Pro x64 ASUS ROG Swift 
KeyboardPowerCaseMouse
Razer Black Widow Chroma Corsair RM1000 Corsair 900D Razer Deathadder Chroma 
Mouse PadAudioOther
Steelseries Soundblaster Z Speakers: Logitech z5500 
  hide details  
Reply
post #8 of 8
http://msdn.microsoft.com/en-us/libr...c4(VS.80).aspx

Its the same in C++ if i am not mistaken.
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming