Getting correct output

This is a discussion on Getting correct output within the C++ Programming forums, part of the General Programming Boards category; I'm fairly new to programming in general and have just started to learn C++. So as a small project to ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Getting correct output

    I'm fairly new to programming in general and have just started to learn C++. So as a small project to test what I know and just see what I can accomplish on my own I decided to make a simple calculator program that will compute simple addition, subtraction, multiplication and division and output the result. However, I can't figure out a way to make this program output a remainder for division problems that would have one.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Operate(int numerouno, char operation, int numerodos); //Declares function 'Operate'
    
    
    int main()
    {
    
        //VARIABLES
        int num1;
        char affirm;
        int num2;
        char affirm2;
        char operation;
        int result;
        char restart;
    
        int loop;
        loop = 1;
    
    
        //INTRODUCTION
        cout<<"Welcome to the Basic Calculator. In this program you will be able to add, \nsubtract, multiply or divide two numbers.\n" << endl;
        //PROMPT FOR FIRST VALUE
    
        beginagain://Restart point if user wants to re-use program at the end.
        cout<<"What is your first number going to be?\n" << endl;
        /*goto command from Line 32*/ onenum:cin >> num1;
    
        //Checking for accuracy on First Number
        cout<<"\nAre you sure you want to use " << num1 << " as your first number?\nPress 'y' for yes and 'n' for no.\n" << endl;
        cin >> affirm;
        cout<<endl;
    
        //If User says number is not correct prompts them to re-enter it by using goto.
            while (affirm == 'n')
            {
                cout<<"Well then re-enter your first number, please.\n" << endl;
                goto onenum; //On line 17
            }
    
       //After "affirm" is 'y' - Program continues and user is informed.
        cout<<"Ok! First number registered.\n"<<endl;
    
    
        //Prompts for second number.
        cout<<"What do you want your second number to be?\n" << endl;
        twonum:cin >> num2;
    
        //Checks for accuracy of second number.
        cout<<"\nAre you sure you want to use " << num2 << " as your second number?\nPress 'y' for yes and 'n' for no.\n" << endl;
        cin >> affirm2;
    
            //User can revise their second number if answer to above question is "n"
            while (affirm2 == 'n')
            {
                cout<<"\nOh, then please re-enter your second number.\n" << endl;
                goto twonum; //On Line 41
            }
    
        //After affirm2 = y, program continues.
        cout<<"\nOk! Now that we have both of your chosen numbers, what would you like to do withthem?\n"<<endl;
        //Gives instructions on what to input for desired operation.
        cout<<"+ for Addition\n- for Subtraction\n* for Multiplication\n/ for Division\n" << endl;
        cin>>operation;
    
        //Gets result by calling function Operate
        result = Operate(num1, operation, num2);
    
    
    
        //prints out the expression and the answer
        cout<<"\n"<<num1<<operation<<num2<<"="<<result<<endl;
        cout<<"WARNING: If dividing and there is a remainder, the remainder will not show."<<endl;
    
    
        cout<<"\n\n\nWould you like to go back to the beginning of the Calculator?\n'Y' for yes or 'N' for no."<<endl;
        cin>>restart;
            if (restart=='Y')
                goto beginagain;
            else
                loop=0;
    }
    
    
    //FUNCTIONS
    
    int Operate(int num1, char operation, int num2)
    {
        if (operation=='+')
            return num1+num2;
        else if (operation=='-')
            return num1-num2;
        else if (operation=='*')
            return num1*num2;
        else if (operation=='/')
            return num1/num2;
        else cout<<"You suck."<<endl;
    }
    That's the code for a working program that does all the right functions. I'm just not sure about how to make it output a remainder value for division. I don't care if it outputs a '0' remainder for addition subtraction and multiplication as well, but I would like it to output a correct remainder for the division.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The integer divide functionality in C++ (and most other languages, such as C, Pascal, Fortran, etc) gives you the integer part of the result. If you want to see what the remainder is, you can (cheat a bit [1] and) use the % operator.


    [1] To correctly calculate the remainder, you do
    Code:
    r = x - (x / y) * y;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Since you've managed to use one while loop, there's no reason to fill the code with goto statements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    Thank you matsp. I got an idea after you posted that and was able to fix my remainder problem. Salem, thanks for the idea, I'm still a little shaky on while loops (a problem that hopefully gets solved with a little practice. Thanks to both of you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  4. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 07:08 AM
  5. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM

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