Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-16-2006, 12:01 AM   #1 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
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";
 }    
 
         }
}
js2006 is offline   Reply With Quote
Old 10-16-2006, 06:28 AM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Wrap it in a while loop and test for y or n with another cin.

Toe
__________________
toe_cutter is offline   Reply With Quote
Old 10-16-2006, 05:21 PM   #3 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 10-17-2006, 05:40 PM   #4 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
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";
 }    
 
         }
         
}
js2006 is offline   Reply With Quote
Old 10-17-2006, 07:58 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I don't think you're allowed to run fuctons outside of your main declaration.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-17-2006, 08:05 PM   #6 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
so many errors here : after i put the function inside main ,

where to place it perfectly and where and how to call it ??
js2006 is offline   Reply With Quote
Old 10-17-2006, 10:46 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
You don't put the function definition in main, you put the function call in main.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-17-2006, 11:44 PM   #8 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Smile 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.
js2006 is offline   Reply With Quote
Old 10-18-2006, 05:45 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-18-2006, 07:27 AM   #10 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Exclamation 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;
        }
    }
}
js2006 is offline   Reply With Quote
Old 10-18-2006, 09:05 AM   #11 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
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())

}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can u please find alternate solution for this code ? Umakant Standard C, C++ 2 10-14-2006 01:07 PM
help with alternate row color php script xevean PHP 4 07-01-2005 09:37 AM


All times are GMT -8. The time now is 03:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting