Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 12th, 2007, 03:17 PM
Registered User
 
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default can't get my program to work, help please

Ok I want a program to do basic calculator functions (mainly to show off to my friends), but I keep f*ing up the program. Here is what I have so far.

#include <iostream>

using namespace std;

int main()
{
    int answer;
    char function;
    int a;
    int b;
    int c;
    int d;
    cout<<"***Please enter first number in equation.";
    cin>> a;
    cin.ignore();
    cout<<"Please enter second number.";
    cin>> b;
    cin.ignore();
    cout<<"Please enter a function.";
    cin>> function;
    cin>> a == *
    cin>> b == -
    cin>> c == +
    cin>> d == /
    if ( function == a ) {
         cout<< a-b;
    }
    else if ( function == b ) {
         cout<< a+b;
    }
    else if ( function == c ) {
         cout<< a*b;
    }
    else if ( function == d ) {
         cout<< a/b;
    }
}

Any suggestions would be great.

I will make C++ my bitch.
Reply With Quote
  #2 (permalink)  
Old April 13th, 2007, 12:08 PM
Authorized User
 
Join Date: Mar 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

good idea taking the bull by the horns!

one question... what is
    cin>> a == *
    cin>> b == -
    cin>> c == +
    cin>> d == /

And I think you should be testing function against the characters '-' '+' '*' and '/'

Reply With Quote
  #3 (permalink)  
Old April 15th, 2007, 08:03 PM
Registered User
 
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yah I just realized I f*ed up the whole thing anyway so here's the revised version
one quick question before starting- what integer can I use to represent a function (+ - * /)? Every time I don't know what integer to use is put (?)

here you go:


#include <iostream>

using namespace std;

int main()
{
    int answer;
    char function;
    int a;
    int b;
    (need integer type here)
    cout<<"Hello and welcome to A+ Super Calculator, letting you run functions that normal calculators can't!";
    cout<<"***Please enter first number in equation.";
    cin>> a;
    cin.ignore();
    cout<<"Please enter second number.";
    cin>> b;
    cin.ignore();
    cout<<"Please enter a function.";
    cin>> function;
    cin>> (?)
    if ( (?) == + ) {
         cout<< a+b
    }
    else if ( (?) == - ) {
         cout<< a-b;
    }
    else if ( (?) == * ) {
         cout<< a*b;
    }
    else if ( (?) == / ) {
         cout<< a/b;
    }
}


I will make C++ my bitch.
Reply With Quote
  #4 (permalink)  
Old April 16th, 2007, 01:53 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Congratulations on your goal =)

I was happy when I made it my bitch =) lol

Here are the integer values for your symbols

+ = 43
- = 45
/ = 47
* = 42

Its very simple to know this...

C++ uses ASCII (American Standard Coding for Information Interchange)

which assigns every character a numeric value...

Now you know...

Keep Up the good work =)

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #5 (permalink)  
Old May 19th, 2007, 05:06 AM
Authorized User
 
Join Date: May 2007
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Peter_APIIT
Default

if (function == +)
{
   a + b;
}
else if (function == -)
{
   a -b;
}

and so on.

I hope this may help you.

Linux is the best OS in the world.
Reply With Quote
  #6 (permalink)  
Old May 19th, 2007, 05:11 AM
Authorized User
 
Join Date: May 2007
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Peter_APIIT
Default

What is the purpose if cin.ignore().

Thanks for your explanations.

Linux is the best OS in the world.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
C program Raj264264 BOOK: Professional C++ 3 March 29th, 2010 08:55 PM
Chapter 1 Ctrl+F5 don't work, F5 does work? jimboak BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 June 29th, 2008 03:46 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
Start a program inside another program Silje Classic ASP Professional 1 November 16th, 2004 02:08 AM
downloaded php program doesn't work lanita BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 June 16th, 2004 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.