need help with calculator

This is a discussion on need help with calculator within the C++ Programming forums, part of the General Programming Boards category; Code: // my calculator.cpp : Defines the entry point for the console application. // #include <iostream> using namespace std; int ...

  1. #1
    I eat babies
    Join Date
    Jan 2008
    Location
    la-la-land
    Posts
    31

    need help with calculator

    Code:
    // my calculator.cpp : Defines the entry point for the console application.
    //
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int first, second, todo;
    	
    	cout << "This is a calculator for basic math functions between two integers.\n";
    	cout << "What would you like to do?\n1: Addition\n2: Subtraction\n3: multiplication\n4: Division\n";
    	cin >> todo;
    	if (todo > 4)
    		cout << "Please enter a number 1 - 4";
    	cout << endl;
    	cout << "Please insert the two integers you would like to use: ";
    	cin >> first;
    	cin >> second;
    	if (todo == 1)
    		cout << "The answer is: " << first + second << endl;
    	
    	if (todo == 2)
    		cout << "The answer is: " << first - second << endl;
    	
    	if (todo == 3)
    		cout << "The answer is: " << first * second << endl;
    	
    	if (todo == 4)
    		cout << "The answer is: " << first / second << " With a remainder of " <<  first % second << endl;
    	
    
    	system("pause");
    return 0;
    }
    when the user inputs an incorrect number for addition subtraction etc. it says "please enter a number 1 - 4" but then it just goes to the next step, how can i fix this?
    There are only 10 types of people, those who understand binary and those who don't
    C++ Beginner
    Visual C++ 2008 Express Edition!
    Windows XP
    http://img369.imageshack.us/img369/4372/1399mf9.jpg

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    What you need is a loop. A while loop works perfectly here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    I eat babies
    Join Date
    Jan 2008
    Location
    la-la-land
    Posts
    31
    ok thanks
    There are only 10 types of people, those who understand binary and those who don't
    C++ Beginner
    Visual C++ 2008 Express Edition!
    Windows XP
    http://img369.imageshack.us/img369/4372/1399mf9.jpg

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Dont use system("pause") as system calls are not safe.

    Use cin.get()
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I'd say - do not use this stuff at all

    Ctrl+F5 in VS will run the window that will not close till you press space

    And when running in debug with F5 - you always can put breakpoint on return 0; of main to prevent program from closing

    So this artificial pause has no meaning at all
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 03:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21