R
Rohan Seth
Hi
I am teaching myself C++ and have written a small program. Its a
menu driven program that accepts two numbers and gives U the option of
either adding,subtracting or multiplying the two numbers. The problem
that I am facing is that when I RUN the program the result are as follows:-
Enter M for MENU or Q to QUIT
M //I enter m or M as the menu wishes
Enter Two Numbers
The FIRST Number is: 3 // enter the number 3
The SECOND Number is: 2 // enter a number 2
Enter Your Choice:
1: ADD
2: SUBTRACT
3: MULTIPLY
You Entered: 1 // choose the option to add
The answer is: 5 // it gives me the answer
--->Enter M for MENU or Q to QUIT // this is the PROBLEM (prints twice)
PLS re-enter your data
--->Enter M for MENU or Q to QUIT
"Enter M for MENU or Q to QUIT" this statement is printed twice after
each operation. It should be printed only once. I have a feeling its
taking in some junk value....I have scratched my head for sometime with
this and now I turn for HELP .... so if any one out there has the time
and effort to compile the code(which I have pasted below) and find a
solution ...... UR EFFORTS WILL BE DEEPLY APPRECIATED !!!!
Cheers
Rohan
--------SOURCE CODE----------
#include <iostream>
#include <stdlib>
#include <string>
#define TRUE 1;
#define FALSE 0;
/*:: scope resolution operator informing the compiler that cout,endl and
cin are global identifiers and not local identifiers*/
using std::cout;
using std::cin;
using std::endl;
//Function Prototypes
int menu();
int sum(int ,int );
int subtract(int , int );
int multiply(int , int );
//function main wakes up
int main()
{
//declaring and initialising variables
int choice;
char val;
int numOne = 0;
int numTwo = 0;
//function menu wakes up and assigns the option to the variable choice
while(choice != 0)
{
//output statements
cout << "Enter M for MENU or Q to QUIT" << endl;
//accepting the value of val from the user
cin.get(val);
if(val == 'M' || val == 'm') //compares input
{
cout << "Enter Two Numbers" << endl;
cout << endl << "The FIRST Number is: ";
cin >> numOne;
cout << endl << "The SECOND Number is: ";
cin >> numTwo;
choice = menu();
switch(choice)
{
case 1: sum(numOne,numTwo);
break;
case 2: subtract(numOne,numTwo);
break;
case 3: multiply(numOne,numTwo);
break;
}
}
else if(val == 'Q' || val == 'q') //comapares input
{
cout << endl << "THX for using this program " << endl;
exit(0);
}
else
cout << "PLS re-enter your data" << endl;
}
return TRUE;
}
//function menu wakes up to displays the menu
int menu()
{
int option = 0;
cout << "Enter Your Choice:";
cout << endl << "1: ADD";
cout << endl << "2: SUBTRACT";
cout << endl << "3: MULTIPLY";
cout << endl << "You Entered: ";
cin >> option;
return option;
}
//function sum wakes up to add the two numbers
int sum(int nOne,int nTwo)
{
int add;
add=nOne+nTwo;
cout << endl << "The answer is: " << add << endl;
return TRUE;
}
//function subtract wakes up to subtract the two numbers
int subtract(int nOne,int nTwo)
{
int minus;
if(nOne > nTwo)
minus=nOne-nTwo;
else
{
cout << endl << "Value One is less than Value Two" << endl;
}
cout << endl << "The answer is: " << minus << endl;
return TRUE;
}
//function multiply wakes up to multiply the two numbers
int multiply(int nOne,int nTwo)
{
int product;
product=nOne*nTwo;
cout << endl << "The answer is: " << product << endl;
return TRUE;
}
I am teaching myself C++ and have written a small program. Its a
menu driven program that accepts two numbers and gives U the option of
either adding,subtracting or multiplying the two numbers. The problem
that I am facing is that when I RUN the program the result are as follows:-
Enter M for MENU or Q to QUIT
M //I enter m or M as the menu wishes
Enter Two Numbers
The FIRST Number is: 3 // enter the number 3
The SECOND Number is: 2 // enter a number 2
Enter Your Choice:
1: ADD
2: SUBTRACT
3: MULTIPLY
You Entered: 1 // choose the option to add
The answer is: 5 // it gives me the answer
--->Enter M for MENU or Q to QUIT // this is the PROBLEM (prints twice)
PLS re-enter your data
--->Enter M for MENU or Q to QUIT
"Enter M for MENU or Q to QUIT" this statement is printed twice after
each operation. It should be printed only once. I have a feeling its
taking in some junk value....I have scratched my head for sometime with
this and now I turn for HELP .... so if any one out there has the time
and effort to compile the code(which I have pasted below) and find a
solution ...... UR EFFORTS WILL BE DEEPLY APPRECIATED !!!!
Cheers
Rohan
--------SOURCE CODE----------
#include <iostream>
#include <stdlib>
#include <string>
#define TRUE 1;
#define FALSE 0;
/*:: scope resolution operator informing the compiler that cout,endl and
cin are global identifiers and not local identifiers*/
using std::cout;
using std::cin;
using std::endl;
//Function Prototypes
int menu();
int sum(int ,int );
int subtract(int , int );
int multiply(int , int );
//function main wakes up
int main()
{
//declaring and initialising variables
int choice;
char val;
int numOne = 0;
int numTwo = 0;
//function menu wakes up and assigns the option to the variable choice
while(choice != 0)
{
//output statements
cout << "Enter M for MENU or Q to QUIT" << endl;
//accepting the value of val from the user
cin.get(val);
if(val == 'M' || val == 'm') //compares input
{
cout << "Enter Two Numbers" << endl;
cout << endl << "The FIRST Number is: ";
cin >> numOne;
cout << endl << "The SECOND Number is: ";
cin >> numTwo;
choice = menu();
switch(choice)
{
case 1: sum(numOne,numTwo);
break;
case 2: subtract(numOne,numTwo);
break;
case 3: multiply(numOne,numTwo);
break;
}
}
else if(val == 'Q' || val == 'q') //comapares input
{
cout << endl << "THX for using this program " << endl;
exit(0);
}
else
cout << "PLS re-enter your data" << endl;
}
return TRUE;
}
//function menu wakes up to displays the menu
int menu()
{
int option = 0;
cout << "Enter Your Choice:";
cout << endl << "1: ADD";
cout << endl << "2: SUBTRACT";
cout << endl << "3: MULTIPLY";
cout << endl << "You Entered: ";
cin >> option;
return option;
}
//function sum wakes up to add the two numbers
int sum(int nOne,int nTwo)
{
int add;
add=nOne+nTwo;
cout << endl << "The answer is: " << add << endl;
return TRUE;
}
//function subtract wakes up to subtract the two numbers
int subtract(int nOne,int nTwo)
{
int minus;
if(nOne > nTwo)
minus=nOne-nTwo;
else
{
cout << endl << "Value One is less than Value Two" << endl;
}
cout << endl << "The answer is: " << minus << endl;
return TRUE;
}
//function multiply wakes up to multiply the two numbers
int multiply(int nOne,int nTwo)
{
int product;
product=nOne*nTwo;
cout << endl << "The answer is: " << product << endl;
return TRUE;
}