Thread: need help with calculator

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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,688
    Dont use system("pause") as system calls are not safe.

    Use cin.get()
    Double Helix STL

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 04: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, 10: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, 09:31 AM