1. ## 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

2. #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;
}

3. 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. use a :

do
{
}while();

loop

5. 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. 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<<"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;
}
cout<<"Would you like to make another calculation?(y/n)";
cont=getch();
}while(cont!='n');
return 0;
}```

7. ## check this

Code:
```#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>

float Num1;
float Num2;
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<<"  2) Subtract"<<endl;
cout<<"  3) Multiply"<<endl;
cout<<"  4) Divide"<<endl;
cout<<"  6) Quit"<<endl;
cout<<endl;
cin>>Num1;
if(Num1 == 1){
}
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;
getchar ();
}
else{
system("CLS");
cout<<"Please enter a number between 1 and 5..."<<endl;
cout<<endl;
system("pause");
choice();
}
}

{
system("CLS");
cout<<"_________________"<<endl;
cout<<endl;
cin>>Num1;
system("CLS");
cout<<"_________________"<<endl;
cout<<endl;
cin>>Num2;
system("CLS");
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;
cin>>Num1;
system("CLS");
cout<<"Subtraction"<<endl;
cout<<"_________________"<<endl;
cout<<endl;
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;
cin>>Num1;
system("CLS");
cout<<"Multiplication"<<endl;
cout<<"_________________"<<endl;
cout<<endl;
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;
cin>>Num1;
system("CLS");
cout<<"Division"<<endl;
cout<<"_________________"<<endl;
cout<<endl;
cin>>Num2;
system("CLS");
cout<<"Division"<<endl;
cout<<"_________________"<<endl;
cout<<endl;
cout<<Num1<<" / "<<Num2<<" = "<<Num1/Num2<<endl;
cout<<endl;
system("pause");
choice();
}
{
system("CLS");
cout<<"_________________"<<endl;
cout<<endl;
cout<<"Please enter the sub total: ";
cin>>prc;
system("CLS");
cout<<"_________________"<<endl;
cout<<endl;
cout<<"Please enter the sales tax in decimal form (i.e. 0.09): ";
cin>>tax;
system("CLS");
ttl();
}
{
fttl = prc * tax;
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