Help with looping

This is a discussion on Help with looping within the C++ Programming forums, part of the General Programming Boards category; I wrote a switch program that performs 4 basic math operations: #include <iostream.h> int main(){ int ch; float m1,m2,mt,d1,d2,dt,a1,a2,at,s1,s2,st; cout<<"Please ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    12

    Help with looping

    I wrote a switch program that performs 4 basic math operations:


    #include <iostream.h>

    int main(){
    int ch;
    float m1,m2,mt,d1,d2,dt,a1,a2,at,s1,s2,st;

    cout<<"Please select a number from the menu\n";
    cout<<"1. Multiplication\n";
    cout<<"2. Division\n";
    cout<<"3. Addition\n";
    cout<<"4. Subtraction\n";
    cin>>ch;

    switch (ch)
    {
    case 1:
    cout<<"Please enter the 1st of the 2 numbers to multiply\n";
    cin>>m1;
    cout<<"Please enter 2nd of the 2 numbers to multiply\n";
    cin>>m2;
    mt = m1*m2;
    cout<<"The total is "<<mt<<"\n";
    break;
    case 2:
    cout<<"Please enter 1st of the 2 numbers to divide\n";
    cin>>d1;
    getnum:
    cout<<"Please enter 2nd of the 2 numbers to divide\n";
    cin>>d2;
    if (d2 == 0){
    cout<<"Invalid Number, Try again (don't use 0)\n";
    goto getnum;
    }
    dt = d1/d2;
    cout<<"The total is "<<dt<<"\n";
    break;
    case 3:
    cout<<"Please enter the 1st of the 2 numbers to add\n";
    cin>>a1;
    cout<<"Please enter 2nd of the 2 numbers to add\n";
    cin>>a2;
    at = a1+a2;
    cout<<"The total is "<<at<<"\n";
    break;

    case 4:
    cout<<"Please enter the 1st of the 2 numbers to subract\n";
    cin>>s1;
    cout<<"Please enter 2nd of the 2 numbers to subtract\n";
    cin>>s2;
    st = s1-s2;
    cout<<"The total is "<<st<<"\n";
    break;


    }
    return 0;
    }


    Can anybody tell me what I should do in order for the program to ask if you wish to perform any further calculations. If YES, how do I make it, so that the given answer will be taken as the first number to be used in the next calculation. and NO ends it. Any help would be greatly appreciated. THX

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    #include <iostream.h>

    int main(){
    int ch;
    float m1,m2,mt,d1,d2,dt=0,a1,a2,at,s1,s2,st,wait;

    cout<<"Please select a number from the menu\n";
    cout<<"1. Multiplication\n";
    cout<<"2. Division\n";
    cout<<"3. Addition\n";
    cout<<"4. Subtraction\n";
    cin>>ch;

    switch (ch)
    {
    case 1:
    cout<<"Please enter the 1st of the 2 numbers to multiply\n";
    cin>>m1;
    cout<<"Please enter 2nd of the 2 numbers to multiply\n";
    cin>>m2;
    mt = m1*m2;
    cout<<"The total is "<<mt<<"\n";
    break;
    case 2:
    while(dt == 0)
    {
    cout<<"Please enter 1st of the 2 numbers to divide\n";
    cin>>d1;
    \\getnum:
    cout<<"Please enter 2nd of the 2 numbers to divide\n";
    cin>>d2;
    if (d2 == 0 || d1 == 0){
    cout<<"Invalid Number, Try again (don't use 0)\n";
    \\goto getnum;
    }
    else
    {
    dt = d1/d2;
    cout<<"The total is "<<dt<<"\n";
    }
    }
    break;
    case 3:
    cout<<"Please enter the 1st of the 2 numbers to add\n";
    cin>>a1;
    cout<<"Please enter 2nd of the 2 numbers to add\n";
    cin>>a2;
    at = a1+a2;
    cout<<"The total is "<<at<<"\n";
    break;

    case 4:
    cout<<"Please enter the 1st of the 2 numbers to subract\n";
    cin>>s1;
    cout<<"Please enter 2nd of the 2 numbers to subtract\n";
    cin>>s2;
    st = s1-s2;
    cout<<"The total is "<<st<<"\n";
    break;


    }
    cin>>wait;
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    I think I mislead u and I apoligize for it. The program u wrote does not ask wether or not the user wants to continue using the program or not (If answer is YES, use the previous answer as the first number for the next calculation). Pls help. Thx anyways .

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    use a :

    do
    {
    }while();

    loop
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    I would but how exactly do I implement the loop? where do i put it? should it be after each case or can it be at the end of all cases?

  6. #6
    Unregistered
    Guest
    This is your code with a do-while loop and a your little continue thing... I made the spaces a little different (easier for me to read) so change 'em back if you don't like them. Notice the clrscr(); command after the do{ and the cont=getch(); statement. The first clears the screen and the second reads a character inputted by the user. The do-while loop is placed around the Menu and switch statement so that if the user wants to make another calculation, he can make a choice again.
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main(){ 
      int ch; 
      float m1,m2,mt,d1,d2,dt,a1,a2,at,s1,s2,st; 
      char cont;
    
      do{
      clrscr();
      cout<<"Please select a number from the menu\n"; 
      cout<<"1. Multiplication\n"; 
      cout<<"2. Division\n"; 
      cout<<"3. Addition\n"; 
      cout<<"4. Subtraction\n"; 
      cin>>ch; 
    
      switch (ch) 
      { 
        case 1: 
          cout<<"Please enter the 1st of the 2 numbers to multiply\n"; 
          cin>>m1; 
          cout<<"Please enter 2nd of the 2 numbers to multiply\n"; 
          cin>>m2; 
          mt = m1*m2; 
          cout<<"The total is "<<mt<<"\n"; 
          break; 
        case 2: 
          cout<<"Please enter 1st of the 2 numbers to divide\n"; 
          cin>>d1; 
          getnum: 
          cout<<"Please enter 2nd of the 2 numbers to divide\n"; 
          cin>>d2; 
          if (d2 == 0){ 
            cout<<"Invalid Number, Try again (don't use 0)\n"; 
            goto getnum; 
          } 
          dt = d1/d2; 
          cout<<"The total is "<<dt<<"\n"; 
          break; 
        case 3: 
          cout<<"Please enter the 1st of the 2 numbers to add\n"; 
          cin>>a1; 
          cout<<"Please enter 2nd of the 2 numbers to add\n"; 
          cin>>a2; 
          at = a1+a2; 
          cout<<"The total is "<<at<<"\n"; 
          break; 
        case 4: 
          cout<<"Please enter the 1st of the 2 numbers to subract\n"; 
          cin>>s1; 
          cout<<"Please enter 2nd of the 2 numbers to subtract\n"; 
          cin>>s2; 
          st = s1-s2; 
          cout<<"The total is "<<st<<"\n"; 
          break; 
      }
      cout<<"Would you like to make another calculation?(y/n)";
      cont=getch();
      }while(cont!='n');
    return 0; 
    }

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Thumbs up check this

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    
    float Num1;
    float Num2;
    void add();
    void sub();
    void div();
    void mult();
    void choice();
    
    float tax; //these are for the tax adder...
    float prc;
    float fttl;
    void prg();
    void ttl();
    
    void choice()
    {
         system("CLS");
         cout<<"Calculator v.1.5"<<endl;
         cout<<"________________________"<<endl;
         cout<<endl;
         cout<<"  1) Add"<<endl;
         cout<<"  2) Subtract"<<endl;
         cout<<"  3) Multiply"<<endl;
         cout<<"  4) Divide"<<endl;
         cout<<"  5) Tax Adder"<<endl;
         cout<<"  6) Quit"<<endl;
         cout<<endl;
         cout<<"Make your choice: ";
         cin>>Num1;
                   if(Num1 == 1){
                   add();
                   }
                   else if(Num1 == 2){
                   sub();
                   }
                   else if(Num1 == 3){
                   mult();
                   }
                   else if(Num1 == 4){
                   div();
                   }
                   else if(Num1 == 5){
                   prg();
                   }
                   else if(Num1 == 6){
                        system("CLS");
                        cout<<"Thank you for using Calculator v.1.5."<<endl;
                        cout<<endl;
                        cout<<"Made by (your name)";
                        getchar ();
                   }
                   else{
                        system("CLS");
                        cout<<"Please enter a number between 1 and 5..."<<endl;
                        cout<<endl;
                        system("pause");
                        choice();
                        }
    }
    
    void add()
    {
         system("CLS");
         cout<<"Addition"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your first number: ";
         cin>>Num1;
         system("CLS");
         cout<<"Addition"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your second number: ";
         cin>>Num2;
         system("CLS");
         cout<<"Addition"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<Num1<<" + "<<Num2<<" = "<<Num1+Num2<<endl;
         cout<<endl;
         system("pause");
         choice();
    }
    void sub()
    {
         system("CLS");
         cout<<"Subtraction"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your first number: ";
         cin>>Num1;
         system("CLS");
         cout<<"Subtraction"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your second number: ";
         cin>>Num2;
         system("CLS");
         cout<<"Subtraction"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<Num1<<" - "<<Num2<<" = "<<Num1-Num2<<endl;
         cout<<endl;
         system("pause");
         choice();
    }
    void mult()
    {
         system("CLS");
         cout<<"Multiplication"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your first number: ";
         cin>>Num1;
         system("CLS");
         cout<<"Multiplication"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your second number: ";
         cin>>Num2;
         system("CLS");
         cout<<"Multiplication"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<Num1<<" * "<<Num2<<" = "<<Num1*Num2<<endl;
         cout<<endl;
         system("pause");
         choice();
    }
    void div()
    {
         system("CLS");
         cout<<"Division"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your first number: ";
         cin>>Num1;
         system("CLS");
         cout<<"Division"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter your second number: ";
         cin>>Num2;
         system("CLS");
         cout<<"Division"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<Num1<<" / "<<Num2<<" = "<<Num1/Num2<<endl;
         cout<<endl;
         system("pause");
         choice();
    }
    void prg()                           //tax adder
    {
         system("CLS");
         cout<<"Tax Adder"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter the sub total: ";
         cin>>prc;
         system("CLS");
         cout<<"Tax Adder"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Please enter the sales tax in decimal form (i.e. 0.09): ";
         cin>>tax;
         system("CLS");
         ttl();
    }
    void ttl()                       //tax adder
    {
         fttl = prc * tax;
         cout<<"Tax Adder"<<endl;
         cout<<"_________________"<<endl;
         cout<<endl;
         cout<<"Subtotal:          "<<prc<<endl;
         cout<<"Tax:               "<<prc*tax<<endl;
         cout<<endl;
         cout<<"Total:             "<<prc+fttl<<endl;
         system("pause");
         choice();
    }
    
    int main (int argc, char *argv[])
    {
       cout<<"*Calculator v.1.5*"<<endl;
       cout<<endl;
       system("pause");
       choice();
       return 0;
    }
    This is my calculator. It uses void commands to go back to a menu.
    It is very simple
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21