Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED]Really simple C++ Q (If-else/do-while)
New Posts  All Forums:Forum Nav:

[SOLVED]Really simple C++ Q (If-else/do-while)

post #1 of 18
Thread Starter 
If I have a working If statement:


//Math goes on here like:-
cin>> X;
Y = X+7;
cout<< X;

cout<<"Do u wanna try again?";
cin>> ans;

if (ans = yes)

{
//repeat the above calculations
}

else
{
//end the program
}



Q1) How do I make answering "yes" actually loop to the above questions? Which woould make this an infinite loop if u kept entering "yes".
Q2) If you want to enter "yes" what type of variable is that? string?
Edited by MeeMoo220 - 1/27/11 at 11:41pm
post #2 of 18
Q1. Use a do-while loop. http://mathbits.com/mathbits/compsci...ng/dowhile.htm
Q2. Either a string or a character array
post #3 of 18
mrwalker got em bother right. A do while loop is the best way and a string would be easiest, or just tell them to type 1 for yes or 0 for now, something like that.
    
CPUMotherboardGraphicsRAM
Q6600 @ 3Ghz GA-P35-DS3L Rev. 2.0 Powercooler 4870 8Gb GSkill 
Hard DriveOptical DriveOSMonitor
500Gb WD+640Gb WD Lite-on 20x Vista Home Premium 64x 1920x1200 24" +1400x1050 20" 
PowerCase
650 watt OP650 CM Stacker 830 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 @ 3Ghz GA-P35-DS3L Rev. 2.0 Powercooler 4870 8Gb GSkill 
Hard DriveOptical DriveOSMonitor
500Gb WD+640Gb WD Lite-on 20x Vista Home Premium 64x 1920x1200 24" +1400x1050 20" 
PowerCase
650 watt OP650 CM Stacker 830 
  hide details  
Reply
post #4 of 18
Give me sec I can probably help.
post #5 of 18
a do while loop is the best

do
(your stuff)
while ans=yes
post #6 of 18
Quote:
Originally Posted by MeeMoo220 View Post
If I have a working If statement:


//Math goes on here like:-
cin>> X;
Y = X+7;
cout<< X;

cout<<"Do u wanna try again?";
cin>> ans;

if (ans = yes)

{
//repeat the above calculations
}

else
{
//end the program
}



Q1) How do I make answering "yes" actually loop to the above questions? Which woould make this an infinite loop if u kept entering "yes".
Q2) If you want to enter "yes" what type of variable is that? string?
Code:
boolean stop = false;

do{
     cin>> X;
     Y = X+7;
     cout<< X;

     cout<<"Do u wanna try again?\
";
     cout<<"1) Yes\
";
     cout<<"2) No\
";
     cin>> ans;

     if(ans == 2){
          stop == true;
     }

}while(!stop);
I assume you are creating the int's X, Y and ans.
Edited by cbrazeau1115 - 1/27/11 at 10:16pm
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
post #7 of 18
Thread Starter 
I changed it into a do-while loop based on the one in this website but entering Y/y/N/n doesnt do anything and entering something else loops it. X_x?

My program is attached.
post #8 of 18
Quote:
Originally Posted by MeeMoo220 View Post
I changed it into a do-while loop based on the one in this website but entering Y/y/N/n doesnt do anything and entering something else loops it. X_x?

My program is attached.
I posted you code that should work just fine. Though, why are you wanting to read in X, calculate a Y and then print an X again? Just wondering if that is the true wanted functionality.
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
post #9 of 18
Thread Starter 
Quote:
Originally Posted by cbrazeau1115 View Post
I posted you code that should work just fine. Though, why are you wanting to read in X, calculate a Y and then print an X again? Just wondering if that is the true wanted functionality.
Thanks a lot mate, I'm gonna try and implement it and see what happens.


EDIT: that isn't the actual function, the actual function is in my attached program 2-3 posts above.
Edited by MeeMoo220 - 1/27/11 at 10:22pm
post #10 of 18
Thread Starter 
Is there a library I neede to #include to use [boolean stop= false;] ?

It says 'boolean' undeclared function.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED]Really simple C++ Q (If-else/do-while)