# Help with looping

• 05-07-2002
cprogstudent
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<<"1. Multiplication\n";
cout<<"2. Division\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:
cin>>a1;
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
• 05-07-2002
Denethor2000
#include <iostream.h>

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

cout<<"1. Multiplication\n";
cout<<"2. Division\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:
cin>>a1;
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;
}
• 05-07-2002
cprogstudent
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 :) .
• 05-07-2002
lostminds
use a :

do
{
}while();

loop
• 05-07-2002
cprogstudent
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?
• 05-07-2002
Unregistered
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; }```
• 05-08-2002
Vicious
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