|
 |
|
 |
10-16-2006, 12:01 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
alternate of that :
hello guys i made a simple switch calculator , i want to add / improve it's output by taking a respone from user's after the program sucessfully shows calculation , the program asks users to :
Do another (y/n)? if y program re runs
if n the program exits.
please help me around , here it's code.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
main()
{
float a,b,ans;
char op;
cout << "Please enter number , Operator , 2nd Number to calculate\n";
cin >> a >> op >> b;
switch (op)
{
case '+':
ans = a + b ;
cout << "Sum of your inputted integers are \n";
cout << ans;
break;
case '-':
ans = a - b ;
cout << "Substraction of your inputted integers are \n";
cout << ans;
break;
case '*':
ans = a * b ;
cout << "Multiplication of your inputted integers are \n";
cout << ans;
break;
case '/':
ans = a / b ;
cout << "Division of your inputted integers are \n";
cout << ans;
default:
{
cout << "you enter somethink un-expected";
}
}
}
|
|
|
10-16-2006, 06:28 AM
|
#2 (permalink)
|
|
Code Monkey
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
|
Wrap it in a while loop and test for y or n with another cin.
Toe
__________________
|
|
|
10-16-2006, 05:21 PM
|
#3 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
You could abstract that into a function:
Code:
bool y_or_n_p( void )
{
//predicate function to prompt the user for a y or n character
// and translate that into a boolean value
char c; //user input
cout << "(y or n) ";
//loop until one of the return conditions is met
while ( 1 )
{
cin >> c;
cin.ignore();
if ( c == 'y' || c == 'Y' )
{
return true;
}
else if ( c == 'n' || c == 'N' )
{
return false;
}
else
{
//Prompt user more specifically
cout << "Please enter y or n" << endl;
}
}
}
bool continue = y_or_n_p();
__________________
Stop intellectual property from infringing on me
|
|
|
10-17-2006, 05:40 PM
|
#4 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
some problems:
there is a some problem , here plz help me up
Code:
bool continue = y_or_n_p();
compiler is giving error on the c++ statment :
33 C:\Dev-Cpp\bin\check.cpp syntax error before `continue'
here is my code , plz improve it , may be my logic is wrong.
Code:
The output should around be that :
Enter first number, operator, second number: 10/3
Answer: 3.33333
Do another (y/n)?y
Enter first number, operator, second number: 12+100
Answer: 112
Do another (y/n)?n
my code to be improve :
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
bool y_or_n_p( void )
{
//predicate function to prompt the user for a y or n character
// and translate that into a boolean value
char c; //user input
cout << "(y or n) ";
//loop until one of the return conditions is met
while ( 1 )
{
cin >> c;
cin.ignore();
if ( c == 'y' || c == 'Y' )
{
return true;
}
else if ( c == 'n' || c == 'N' )
{
return false;
}
else
{
//Prompt user more specifically
cout << "Please enter y or n" << endl;
}
}
}
bool continue = y_or_n_p();
main()
{
float a,b,ans;
char op;
cout << "Please enter number , Operator , 2nd Number to calculate\n";
cin >> a >> op >> b;
switch (op)
{
case '+':
ans = a + b ;
cout << "Sum of your inputted integers are \n";
cout << ans;
break;
case '-':
ans = a - b ;
cout << "Substraction of your inputted integers are \n";
cout << ans;
break;
case '*':
ans = a * b ;
cout << "Multiplication of your inputted integers are \n";
cout << ans;
break;
case '/':
ans = a / b ;
cout << "Division of your inputted integers are \n";
cout << ans;
break;
default:
{
cout << "you enter somethink un-expected";
}
}
}
|
|
|
10-17-2006, 07:58 PM
|
#5 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
I don't think you're allowed to run fuctons outside of your main declaration.
__________________
Stop intellectual property from infringing on me
|
|
|
10-17-2006, 08:05 PM
|
#6 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
so many errors here : after i put the function inside main ,
where to place it perfectly and where and how to call it ??
|
|
|
10-17-2006, 10:46 PM
|
#7 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
You don't put the function definition in main, you put the function call in main.
__________________
Stop intellectual property from infringing on me
|
|
|
10-17-2006, 11:44 PM
|
#8 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
thanks,
i have put it , but it working not fine
the no n , N part works ok but
the Y,y part doesnot return true
it doesnot re-run the program.
thanks for your help.
|
|
|
10-18-2006, 05:45 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
The compile error is because the word "continue" is a key word, in any loops if you for some reason want to let it skip some parts add the continue instruction.
Thus you cant declare it as a variable.
|
|
|
10-18-2006, 07:27 AM
|
#10 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
here
Here is the code , but the function doesnot return y ,Y and rerun Program after it successfully display calculation results.
so plz improve it.
iam learning it , tried so many times
but still stucked.
Thanks , Here is the code
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
bool y_or_n_p( void );
main()
{
float a,b,ans;
char op;
cout << "Please enter number , Operator , 2nd Number to calculate\n";
cin >> a >> op >> b;
switch (op)
{
case '+':
ans = a + b ;
cout << "Sum of your inputted integers are \n";
cout << ans;
y_or_n_p();
break;
case '-':
ans = a - b ;
cout << "Substraction of your inputted integers are \n";
cout << ans;
y_or_n_p();
break;
case '*':
ans = a * b ;
cout << "Multiplication of your inputted integers are \n";
cout << ans;
y_or_n_p();
break;
case '/':
ans = a / b ;
cout << "Division of your inputted integers are \n";
cout << ans;
y_or_n_p();
break;
default:
{
cout << "you enter somethink un-expected";
}
}
}
bool y_or_n_p( void )
{
//predicate function to prompt the user for a y or n character
// and translate that into a boolean value
char c; //user input
cout << "(y or n) ";
//loop until one of the return conditions is met
while ( 1 )
{
cin >> c;
cin.ignore();
if ( c == 'y' || c == 'Y' )
{
return true;
}
else if ( c == 'n' || c == 'N' )
{
return false;
}
else
{
//Prompt user more specifically
cout << "Please enter y or n" << endl;
}
}
}
|
|
|
10-18-2006, 09:05 AM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
like Toe said
Quote:
|
Wrap it in a while loop and test for y or n with another cin.
|
so do it like this:
Code:
main()
{
float a,b,ans;
char op;
do{
cout << "Please enter number , Operator , 2nd Number to calculate\n";
cin >> a >> op >> b;
switch (op)
{
case '+':
ans = a + b ;
cout << "Sum of your inputted integers are \n";
cout << ans;
break;
case '-':
ans = a - b ;
cout << "Substraction of your inputted integers are \n";
cout << ans;
break;
case '*':
ans = a * b ;
cout << "Multiplication of your inputted integers are \n";
cout << ans;
break;
case '/':
ans = a / b ;
cout << "Division of your inputted integers are \n";
cout << ans;
break;
default:
cout << "you enter somethink un-expected";
}
}while(y_or_n_p())
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:48 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|