Ok, so I am having some trouble with my calculator code. The two issues I am having are:
1. I cant get my program to produce decimals, I have tried using "float" but that didn't work so i don't know how to fix that.
2. I don't know how to make my program go to the beginning and I am trying to find out how but i couldn't. And i know that GOTO is basically forbidden to please help. Here is my source code.

#include <iostream>

using namespace std;

int main()
{
    int choice;
    int a;
    int b;

    cout<<"Hello! Welcome to kiwimoosical's calculator\n";

    cout<<"1. Addition\n";
    cout<<"2. Subtraction\n";
    cout<<"3. Multiplication\n";
    cout<<"4. Division\n";
    cin >> choice;
    switch(choice)
        {
            case 1: cout<<"Enter two numbers, one at a time to be added together.";
                        cin >> a;
                        cin >> b;
                        return a+b;
                    break;
            case 2: cout<<"Enter two numbers, one at a time to be subtracted.";
                        cin >> a;
                        cin >> b;
                        return a-b;
                    break;
            case 3: cout<<"Enter two numbers, one at a time to be multiplied together.";
                        cin >> a;
                        cin >> b;
                        return a*b;
                    break;
            case 4: cout<<"Enter two numbers, one at a time to be divided by.";
                        cin >> a;
                        cin >> b;
                        return a/b;
                    break;

        }
}

1. I cant get my program to produce decimals, I have tried using "float" but that didn't work so i don't know how to fix that.

Because you declare a & b as integer, your result will also be integer as well.

2. I don't know how to make my program go to the beginning and I am trying to find out how but i couldn't. And i know that GOTO is basically forbidden to please help. Here is my source code.

Not sure how you want your program to be. I guess you would need a while loop and check for quitting condition. It depends on where you want your loop to be. You could have nested loops as well. A sample code inside main() is below. There is no error checking for input, so please beware of that.

// example, the program will run at least once
 int choice, a, b;
 float c;
 do {
   // display your question about choice here and wait for the input
   cout<<"1. Addition\n";
   cout<<"2. Subtraction\n";
   cout<<"3. Multiplication\n";
   cout<<"4. Division\n";
   cout<<"5. Quit\n";
   cin >> choice;

   // do something with your calculation
   switch(choice) {
     case 1: cout<<"Enter two numbers, one at a time to be added together.";
       cin >> a;
       cin >> b;
       c = a+b;  // not sure if you need to cast the type --> c = (float) a+b;
       break;
     case 2: cout<<"Enter two numbers, one at a time to be subtracted.";
       cin >> a;
       cin >> b;
       c = a-b;
       break;
     case 3: cout<<"Enter two numbers, one at a time to be multiplied together.";
       cin >> a;
       cin >> b;
       c = a*b;
       break;
     case 4: cout<<"Enter two numbers, one at a time to be divided by.";
       cin >> a;
       cin >> b;
       c = (float) a/b;
       break;
   }

   // display result, not a good way to do here but just to display how it goes
   if (choice>0 && choice<5) {
     cout << "Result is " << c << endl;
   }
 } while (choice>0 && choice<5);

Hope this would give you some idea.

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.