Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How do I loop my program in C++?
New Posts  All Forums:Forum Nav:

How do I loop my program in C++?

post #1 of 4
Thread Starter 
Here is a little program that asks the user for an answer to two numbers being multiplied together. the question is random each time. here is the code.
Code:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
cout << "Please enter your name: ";
string name;
cin >> name;
string answer;
cout << "\n" << name << ", are you ready to do some timetable? \n";
cin >> answer;
if (answer == "yes")
{ //A
        srand( time (0));
cout << "There we go....\n";
cout << "TIMETABLE CALCULATIONS\n";
int n1( rand() % 10);
int n2( rand() % 10);
cout << "\nWhat is " << n1 << " * " << n2 << "? ";
int product;
cin >> product;
while (product != (n1 * n2))
{
cout << "\nError: Try again! \n";
cout << "\nWhat is " << n1 << " * " << n2 << "? ";
cin >> product;
}
cout << "Correct! \n";
} //B
else
cout << "Maybe some other time...\n";
cout << "\n\n";
system("pause");
return 0;
}

I've been asked to loop the multiplication question 5 times. How would I do this without rewriting the program differently? thank you.
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #2 of 4
Code:
for (int i = 0; i < 5; ++i)
{
        Put the lines you want to repeat here
}

Edited by Bartouille - 10/19/13 at 8:17am
HELLRiDER
(14 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-4770K @ 4.6GHz GIGABYTE GA-Z87X-UD5H HIS R9 280 IceQ X² OC @ 1200/1600MHz HIS R9 280 IceQ X² OC @ 1200/1600MHz 
RAMHard DriveHard DriveCooling
G.SKILL F3-2133C9D-16GXH SAMSUNG Spinpoint F3 1TB SAMSUNG Spinpoint F3 1TB [CPU] Noctua NH-D14 w/ stock fans 
CoolingOSMonitorPower
[GPU] 2x AMD FX-Series Liquid Cooling w/ GT AP-30 Microsoft Windows 10 Pro Dell UltraSharp U2713H EVGA SuperNOVA 1300 G2 
CaseOther
CORSAIR 300R Scythe Kaze Q-12 Fan Controller 
  hide details  
Reply
HELLRiDER
(14 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-4770K @ 4.6GHz GIGABYTE GA-Z87X-UD5H HIS R9 280 IceQ X² OC @ 1200/1600MHz HIS R9 280 IceQ X² OC @ 1200/1600MHz 
RAMHard DriveHard DriveCooling
G.SKILL F3-2133C9D-16GXH SAMSUNG Spinpoint F3 1TB SAMSUNG Spinpoint F3 1TB [CPU] Noctua NH-D14 w/ stock fans 
CoolingOSMonitorPower
[GPU] 2x AMD FX-Series Liquid Cooling w/ GT AP-30 Microsoft Windows 10 Pro Dell UltraSharp U2713H EVGA SuperNOVA 1300 G2 
CaseOther
CORSAIR 300R Scythe Kaze Q-12 Fan Controller 
  hide details  
Reply
post #3 of 4
You should add a for loop around the parts you want to repeat like so:
Code:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
  cout << "Please enter your name: ";
  string name;
  cin >> name;
  string answer;
  cout << "\n" << name << ", are you ready to do some timetable? \n";
  cin >> answer;
  if (answer == "yes")
  { //A
    srand( time (0));
    cout << "There we go....\n";
    for (int i = 0; i < 5; i++)  // BEGIN FOR LOOP
    {
      cout << "TIMETABLE CALCULATIONS\n";
      int n1( rand() % 10);
      int n2( rand() % 10);
      cout << "\nWhat is " << n1 << " * " << n2 << "? ";
      int product;
      cin >> product;
      while (product != (n1 * n2))
      {
        cout << "\nError: Try again! \n";
        cout << "\nWhat is " << n1 << " * " << n2 << "? ";
        cin >> product;
      }
      cout << "Correct! \n";
    }  // END FOR LOOP
  } //B
  else
    cout << "Maybe some other time...\n";
  cout << "\n\n";
  system("pause");
  return 0;
}

This will cause the program to repeat the portion of code that creates the numbers to be asked in the question and where the user answers that question five times.
DAS RIG
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 [4Ghz @ 1.24375v] EVGA x58 FTW3 EVGA GTX 480 Corsair 6GB XMS3 
Hard DriveOSMonitorKeyboard
150GB WD Raptor Windows 7 Home Premium 64-bit LG 24" Saitek Eclipse 2 
PowerCaseMouseMouse Pad
Corsair 850HX Antec 902 Logitech G5 Steelpad 5L 
  hide details  
Reply
DAS RIG
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 [4Ghz @ 1.24375v] EVGA x58 FTW3 EVGA GTX 480 Corsair 6GB XMS3 
Hard DriveOSMonitorKeyboard
150GB WD Raptor Windows 7 Home Premium 64-bit LG 24" Saitek Eclipse 2 
PowerCaseMouseMouse Pad
Corsair 850HX Antec 902 Logitech G5 Steelpad 5L 
  hide details  
Reply
post #4 of 4
Thread Starter 
Thank you both of you. I'd written that line of code but I was not sure how or where to put it.
Rep + for both.
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How do I loop my program in C++?