DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   How can I add a loop for this code (http://www.daniweb.com/forums/thread160285.html)

davids2004 Dec 1st, 2008 1:32 pm
How can I add a loop for this code
 
The code is pretty easy to understand what I am doing. You basically pick a number 1-4 and it will do either addition, subtraction, multiplication, or division. What I need it to do is after they do a problem it loops back and lets them do it again, until they enter 5 to end the program.

Thanks.

Here is my code

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

using namespace std;


int main ()

{
        srand((unsigned)time(0));
        int add1, add2, sub1, sub2;
        int multiply1, multiply2, div1, div2;
        int choice, input;
        add1 = (rand()%500)+1;
        add2 = (rand()%500)+1;
        sub1 = (rand()%500)+1;
        sub2 = (rand()%98)+1;
        multiply1 = (rand()%98)+1;
        multiply2 = (rand()%8)+1;
        div1 = (rand()%500)+1;
        div2 = (rand()%8)+1;
        int addAnswer = add1 + add2;
        int subAnswer = sub1- sub2;
        int multiplyAnswer = multiply1 * multiply2;
        int divAnswer = div1 / div2;
               
               

        //Display the menu and get the user's choice
                cout << "        Geometry Calculator \n\n";
                cout << "1. Addition\n";
                cout << "2. Subtraction\n";
                cout << "3. Multiplication\n";
                cout << "4. Division\n";
                cout << "5. Quit\n";
                cout << "\n";

                cout <<"Enter your choice (1-5): ";
                cin >> choice;
                cout << "\n";

                               
if (choice==1)
{
        cout << setw (6) << add1 << endl;
        cout << "+ ";
        cout << setw (4) << add2 << endl;
        cout << "------";
        cout << "\n";
        cin >> input;
        cout << "\n";
if
        (input == addAnswer)
        cout << "Congratulations\n";       
else
        cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
        cout << "\n";
return 0;
}

else if (choice==2)
{
        cout << setw (6) << sub1 << endl;
        cout << "- ";
        cout << setw (4) << sub2 << endl;
        cout << "------";
        cout << "\n";
        cin >> input;
        cout << "\n";
if
        (input == subAnswer)
        cout << "Congratulations\n";       
else
        cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
        cout << "\n";
return 0;}
   
else if (choice==3)
{
        cout << setw (6) << multiply1 << endl;
        cout << "* ";
        cout << setw (4) << multiply2 << endl;
        cout << "------";
        cout << "\n";
        cin >> input;
        cout << "\n";
if
        (input == multiplyAnswer)
        cout << "Congratulations\n";       
else
        cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
        cout << "\n";
return 0;}
   
else if (choice==4)
{
        cout << setw (6) << div1 << endl;
        cout << "/ ";
        cout << setw (4) << div2 << endl;
        cout << "------";
        cout << "\n";
        cin >> input;
        cout << "\n";
if
        (input == divAnswer)
        cout << "Congratulations\n";       
else
        cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
        cout << "\n";
return 0;}
                       
else if (choice==5)
        {cout << "Thanks for playing\n";}

else
        {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
   
   
return 0;

}

cikara21 Dec 1st, 2008 2:00 pm
Re: How can I add a loop for this code
 
Simply..
//...
char rep='n';
//display the menu and get user's choice
while(tolower(rep)=='n')
{
    //...Menu
    cout<<"repeat? [y/n] : ";
    cin>>rep;
}
return 0; //bye..

Majestics Dec 1st, 2008 2:36 pm
Re: How can I add a loop for this code
 
[code] while (choice!=5)
{
Your Programme Body

}[\code]

Remember to Intialize Choice With any Number without 5

davids2004 Dec 1st, 2008 2:38 pm
Re: How can I add a loop for this code
 
Quote:

Originally Posted by cikara21 (Post 748196)
Simply..
//...
char rep='n';
//display the menu and get user's choice
while(tolower(rep)=='n')
{
    //...Menu
    cout<<"repeat? [y/n] : ";
    cin>>rep;
}
return 0; //bye..

Thanks for the help. Would I put that before the else if part of the code? Also the char rep='n'; needs to go at the top where I clarify everything. Also what would I do if I wanted it to either be a capital or lower case y or n?

Thanks.

Majestics Dec 1st, 2008 2:38 pm
Re: How can I add a loop for this code
 
 while (choice!=5)
{
Your Programme Body

}

Remember to Intialize Choice With any Number without 5

davids2004 Dec 1st, 2008 2:39 pm
Re: How can I add a loop for this code
 
Quote:

Originally Posted by Majestics (Post 748223)
[code] while (choice!=5)
{
Your Programme Body

}[\code]

Remember to Intialize Choice With any Number without 5

What?

Majestics Dec 1st, 2008 2:45 pm
Re: How can I add a loop for this code
 
    
      #include <iostream>
 
      #include <cstdlib>
 
      #include <iomanip>
 
      #include <ctime>
 
     
 
      using namespace std;
 
     
 
     
 
      int main ()
       
 
    {
 
      srand((unsigned)time(0));
 
      int add1, add2, sub1, sub2;
 
      int multiply1, multiply2, div1, div2;
 
      int choice=0, input;
 
      add1 = (rand()%500)+1;
 
      add2 = (rand()%500)+1;
 
      sub1 = (rand()%500)+1;
 
      sub2 = (rand()%98)+1;
 
      multiply1 = (rand()%98)+1;
 
    multiply2 = (rand()%8)+1;
 
    div1 = (rand()%500)+1;
 
      div2 = (rand()%8)+1;
 
    int addAnswer = add1 + add2;
 
      int subAnswer = sub1- sub2;
 
      int multiplyAnswer = multiply1 * multiply2;
 
      int divAnswer = div1 / div2;
 
     
 
     
 
         
 
    //Display the menu and get the user's choice
 
      while(choice!=5)
          {
          cout << " Geometry Calculator \n\n";
 
      cout << "1. Addition\n";
 
      cout << "2. Subtraction\n";
 
      cout << "3. Multiplication\n";
 
      cout << "4. Division\n";
 
    cout << "5. Quit\n";
 
      cout << "\n";
 
     
 
      cout <<"Enter your choice (1-5): ";
 
      cin >> choice;
 
      cout << "\n";
 
     
 
     
 
      if (choice==1)
 
      {
 
      cout << setw (6) << add1 << endl;
 
      cout << "+ ";
 
      cout << setw (4) << add2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
      if
 
      (input == addAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
 
      cout << "\n";
 
      return 0;
 
      }
 
     
 
      else if (choice==2)
 
      {
 
      cout << setw (6) << sub1 << endl;
 
      cout << "- ";
 
      cout << setw (4) << sub2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
      if
 
      (input == subAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
    else if (choice==3)
 
      {
 
      cout << setw (6) << multiply1 << endl;
 
      cout << "* ";
 
      cout << setw (4) << multiply2 << endl;
 
    cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
    if
 
      (input == multiplyAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
      else if (choice==4)
 
      {
 
      cout << setw (6) << div1 << endl;
 
      cout << "/ ";
 
      cout << setw (4) << div2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
    cin >> input;
 
      cout << "\n";
 
      if
 
      (input == divAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
  else if (choice==5)
 
  {cout << "Thanks for playing\n";}
 
     
 
      else
 
    {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
 
      }
 
     
 
      return 0;
 
     
 
      }

cout<<"alias" Dec 1st, 2008 3:04 pm
Re: How can I add a loop for this code
 
I was doing a similair problem, and with some help from people on here found that using a do-while loop with a switch instead of four if statements worked best. Check out the last page for some pointers...
http://www.daniweb.com/forums/thread159343.html

davids2004 Dec 1st, 2008 3:43 pm
Re: How can I add a loop for this code
 
Quote:

Originally Posted by Majestics (Post 748232)
    
      #include <iostream>
 
      #include <cstdlib>
 
      #include <iomanip>
 
      #include <ctime>
 
     
 
      using namespace std;
 
     
 
     
 
      int main ()
       
 
    {
 
      srand((unsigned)time(0));
 
      int add1, add2, sub1, sub2;
 
      int multiply1, multiply2, div1, div2;
 
      int choice=0, input;
 
      add1 = (rand()%500)+1;
 
      add2 = (rand()%500)+1;
 
      sub1 = (rand()%500)+1;
 
      sub2 = (rand()%98)+1;
 
      multiply1 = (rand()%98)+1;
 
    multiply2 = (rand()%8)+1;
 
    div1 = (rand()%500)+1;
 
      div2 = (rand()%8)+1;
 
    int addAnswer = add1 + add2;
 
      int subAnswer = sub1- sub2;
 
      int multiplyAnswer = multiply1 * multiply2;
 
      int divAnswer = div1 / div2;
 
     
 
     
 
         
 
    //Display the menu and get the user's choice
 
      while(choice!=5)
          {
          cout << " Geometry Calculator \n\n";
 
      cout << "1. Addition\n";
 
      cout << "2. Subtraction\n";
 
      cout << "3. Multiplication\n";
 
      cout << "4. Division\n";
 
    cout << "5. Quit\n";
 
      cout << "\n";
 
     
 
      cout <<"Enter your choice (1-5): ";
 
      cin >> choice;
 
      cout << "\n";
 
     
 
     
 
      if (choice==1)
 
      {
 
      cout << setw (6) << add1 << endl;
 
      cout << "+ ";
 
      cout << setw (4) << add2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
      if
 
      (input == addAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
 
      cout << "\n";
 
      return 0;
 
      }
 
     
 
      else if (choice==2)
 
      {
 
      cout << setw (6) << sub1 << endl;
 
      cout << "- ";
 
      cout << setw (4) << sub2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
      if
 
      (input == subAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
    else if (choice==3)
 
      {
 
      cout << setw (6) << multiply1 << endl;
 
      cout << "* ";
 
      cout << setw (4) << multiply2 << endl;
 
    cout << "------";
 
      cout << "\n";
 
      cin >> input;
 
      cout << "\n";
 
    if
 
      (input == multiplyAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
      else if (choice==4)
 
      {
 
      cout << setw (6) << div1 << endl;
 
      cout << "/ ";
 
      cout << setw (4) << div2 << endl;
 
      cout << "------";
 
      cout << "\n";
 
    cin >> input;
 
      cout << "\n";
 
      if
 
      (input == divAnswer)
 
      cout << "Congratulations\n";
 
      else
 
      cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
 
      cout << "\n";
 
      return 0;}
 
     
 
  else if (choice==5)
 
  {cout << "Thanks for playing\n";}
 
     
 
      else
 
    {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
 
      }
 
     
 
      return 0;
 
     
 
      }

I tried to see what you did and tried it but it did not work.

davids2004 Dec 1st, 2008 4:03 pm
Re: How can I add a loop for this code
 
Ok I just can not get the loop to work. I tried what was in the second post but it does not work. Please help. I am stuck on the code I already posted.

Thanks.


All times are GMT -4. The time now is 1:54 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC