0

Hello everyone. I am new to c++ and I just wrote a program. I am on linux so just saying. This is the code. What keeps happening is all the commands just keep coming. here is an example.
Welcome to the Calculator. Please enter your operation.
addition
Please enter your first number.
Please enter your second number.
0Please enter your first number.
Please enter your second number.
0Please enter your first number.
Please enter your second number.
Floating point exception (core dumped)

Also need help on wait/sleep command if there is one. Thanks!
`/This is a basic calculator. V.01/

#include <iostream>
#include <unistd.h>

using namespace std;

 int main()

{
    int a, b, c, sum, pro, dif, div, end, addition, subtraction, division, multiplication, multiiplication, multiplucation, subtriction, diviision, diivision, adition, addishun, sutraction, subtrraction;


   start:

    cout << "Welcome to the Calculator. Please enter your operation. \n";
    cin >> c;
     if( c == addition)

         goto add;

     else if( c == subtraction)

        goto sub;

    else if( c == division)

        goto divis;

    else if( c == multiplication)

        goto multi;
    else if( c == multiiplication, multiplucation, subtriction, diviision, diivision, adition, addishun, sutraction, subtrraction)
        goto spell;


    /*Basic computations start here*/

    add:

          { cout << "Please enter your first number. \n";
        cin >> a;
            cout << "Please enter your second number. \n";
        cin >> b;

        sum = a + b;
        cout << sum;

          } 

     sub:


   {cout << "Please enter your first number. \n";
        cin >> a;
            cout << "Please enter your second number. \n";
        cin >> b;

        dif = a-b;
        cout << dif;}


     divis:
    {cout << "Please enter your first number. \n";
        cin >> a;
            cout << "Please enter your second number. \n";
        cin >> b;

        dif = a / b;}

     multi:
   {cout << "Please enter your first number. \n";
        cin >> a;
            cout << "Please enter your second number. \n";
        cin >> b;

        end = a * b;
        cout << end;}
      spell:
     {cout << "Spelling error. Restarting program";
      sleep(4);
      goto start;}


        return 0;
}

`

Edited by danila726: Spelling error

2
Contributors
1
Reply
13
Views
1 Year
Discussion Span
Last Post by Moschops
1

start: I look at this and I just KNOW you're planning to use a goto. There are situations in which goto is appropriate. This is NOT one of them, and this is not the year 1950. We have control statements now. http://www.cplusplus.com/doc/tutorial/control/

if( c == addition) This shows a significant misunderstanding. c and addition are int values. Numbers. You just entered a value for c at the keyboard. Now you're testing to see if it is the same number as addition. Tell me, what is the value of addition at this point in the code? Is it 0? Is it 17? Is it 34728? What is it? Where did you set the value of addition?

Also, this mess: else if( c == multiiplication, multiplucation, subtriction, diviision, diivision, adition, addishun, sutraction, subtrraction) What is this mess about? What do you think the , does? It does NOT do what you think it does.

This program is beyond your understanding right now. You need to go back and start simpler.

I would suggest starting with something much simpler. Maybe fetch two values from the keyboard, and if they're the same, output "The same" and if they're different, output "different". Once you can do that, you're ready to move on.

Edited by Moschops

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.