Hi,

I don't know where i when wrong... But my program doesn't want to loop properly.
I was ask to make a Simple calculator that will loop the ans till giving a exit.

here is what i got...

#include <iostream>
#define NEWLINE "\n\n";
using namespace std;

int main()
{    
     char operators, /*counter*/;
     float num1, num2, ans;
     int  cal;



    //counter = 0;

   // while (cal == counter )
    {


    cout << "** MY OWN CALCULATOR **";
    cout << NEWLINE;
    cout << "Select + , - , * , / ";
    cout << NEWLINE;
    cout << "Start calculating!";
    cout << NEWLINE;
    cin >> num1;
    cout << NEWLINE;



    cin >> operators;
    cout << NEWLINE;


    cin >> num2;
    cout << NEWLINE;

    switch(operators)
    {                   

                     case '+':

                          cout << "=";
                          cout << NEWLINE;
                          cout<< num1 + num2<< endl;     
                          cout << NEWLINE;
                          num1 = ans;
                         // counter = counter + 1;

                          break;

                          case '*':

                              cout << "=";
                              cout << NEWLINE;
                              cout<< num1 * num2<< endl;     
                              cout << NEWLINE;
                              num1 = ans;
                            //  counter = counter + 1;
                              break;

                               case '/':

                                  cout << "=";
                                  cout << NEWLINE;
                                  cout<< num1 / num2<< endl;     
                                  cout << NEWLINE;
                                  num1 = ans;
                                 // counter = counter + 1;
                                  break;

                                    case '-':

                                         cout << "=";
                                         cout << NEWLINE;
                                         cout<< num1 - num2<< endl;     
                                         cout << NEWLINE;
                                         num1 = ans;
                                        // counter = counter + 1;
                                         break;

                                  case 'x':
                                  exit(EXIT_FAILURE);
                                  break;
                                         default:
                                         cout << "ERROR!";
                                         cout << NEWLINE;
                                         cout << "Continue";
                                         cout << NEWLINE;
                                         break;
                                         }
                                         {
                                     /* cout << "To exit press x.";
                                      exit(x);*/

                                     }
}
system ("pause");
return 0;   
}

Edited 3 Years Ago by Dani: Formatting fixed

and update coding..

#include <iostream>
#define NEWLINE "\n\n";
using namespace std;

int main()
{    
     char operators, /*counter*/;
     float num1, num2, ans;
     int  cal;



    //counter = 0;

   // while (cal == counter )
    {

    system("cls");
    cout << "** MY OWN CALCULATOR **"; // intial title
    cout << NEWLINE;
    cout << "Select + , - , * , / "; // operators
    cout << NEWLINE;
    cout << "Start calculating!"; // asking user to start calculating
    cout << NEWLINE;
    cin >> num1;                   // print out the 1st number that was enter
    cout << NEWLINE;



    cin >> operators;                 // asking user to enter the operator
    cout << NEWLINE;


    cin >> num2;                      // print out the 2nd number that was enter
    cout << NEWLINE;

    switch(operators)
    {                   

                     case '+':


                          cout << "=";
                          system("cls");
                          cout << NEWLINE;
                          cout<< num1 + num2<< endl;     
                          cout << NEWLINE;
                          num1 = ans;
                         // counter = counter + 1;

                          break;

                          case '*':

                              cout << "=";
                              system("cls");
                              cout << NEWLINE;
                              cout<< num1 * num2<< endl;     
                              cout << NEWLINE;
                              num1 = ans;
                            //  counter = counter + 1;
                              break;

                               case '/':

                                  cout << "=";
                                  system("cls");
                                  cout << NEWLINE;
                                  cout<< num1 / num2<< endl;     
                                  cout << NEWLINE;
                                  num1 = ans;
                                 // counter = counter + 1;
                                  break;

                                    case '-':

                                         cout << "=";
                                         system("cls");
                                         cout << NEWLINE;
                                         cout<< num1 - num2<< endl;     
                                         cout << NEWLINE;
                                         num1 = ans;
                                        // counter = counter + 1;
                                         break;

                                  case 'x':
                                  exit(EXIT_FAILURE);
                                  break;
                                         default:
                                         cout << "ERROR!";
                                         cout << NEWLINE;
                                         cout << "Continue";
                                         cout << NEWLINE;
                                         break;
                                         }
                                         {
                                     /* cout << "To exit press x.";
                                      exit(x);*/

                                     }
}
system ("pause");
return 0;   
}

Edited 3 Years Ago by Dani: Formatting fixed

Well, you've commented out your "while()" expression. {}s don't create a loop by themselves, they just define a "scope" -- a conceptual container where (a) anything declared within it, exists and is visible only within it, and (b) anything declared in an outer scope which contains this one is also visible and accessible.

If you want to loop forever, you can use "while (true) { ... }". The best route to basic programming is: slow down, and think through step by step what it is you want your program to do. In this case "I want a loop. Each time through the loop, the user will be prompted to enter two numbers and an operation. If the operation isn't supported, the loop should exit. Otherwise the operation should be performed, and the answer displayed." Then you can start with a loop, that prompts for an operation, and test that it exits when the user inputs an invalid operation. After that you can add prompts for the numbers, and print them out, so you can see that you got what you expected. Then you can actually perform the operations. And so on.

In the future, if you enclose your source code within a

...

block, it will number your lines and preserve your indentation. After pasting your code, select it all and click the "

" button above the editor.[code]
" button above the editor.

Lol, I'm an idiot. That last bit did exactly what I said. So I'll modify it to say:

... if you enclose your source code within a [-CODE-]...[-/CODE-] block (with the dashes removed), it will ...

I wrote it without the dashes last time, so it made a code-block, just like it was supposed to! :)

it not a forever loop..... it't more like that after num1 + num 2 = ans. then ans + newnum = newans + new

#include <iostream>
#define NEWLINE "\n\n";
using namespace std;

int main()
{    
     char operators, /*counter*/;
     float num1, num2, ans;
     int  cal;
     
     
   
    //counter = 0;
    
   // while (cal == counter )
    {
    
    system("cls");
    cout << "** MY OWN CALCULATOR **"; // intial title
    cout << NEWLINE;
    cout << "Step 1 - Enter 1st number.";
    cout << NEWLINE;
    cout << "Step 2 - Select + , - , * , / "; // operators
    cout << NEWLINE;
    cout << "Step 3 - Enter 2nd number.";
    cout << NEWLINE;
    cout << "To exit program select x.";
    cout << NEWLINE;
    cout << "Start calculating!"; // asking user to start calculating
    cout << NEWLINE;
    cin >> num1;                   // print out the 1st number that was enter
    
    
    

    cin >> operators;                 // asking user to enter the operator
    
    

    cin >> num2;                      // print out the 2nd number that was enter
    
    
    switch(operators)
    {                   
                   
                     case '+':

                          cout << NEWLINE;
                          cout << "=";
                        //  system("cls");
                          cout << NEWLINE;
                          cout<< num1 + num2<< endl;     
                          cout << NEWLINE;
                          num1 = ans;
                         // counter = counter + 1;

                          break;
                          
                          case '*':
                              cout << NEWLINE;
                              cout << "=";
                            //  system("cls");
                              cout << NEWLINE;
                              cout<< num1 * num2<< endl;     
                              cout << NEWLINE;
                              num1 = ans;
                            //  counter = counter + 1;
                              break;
                               
                               case '/':
                                  cout << NEWLINE;
                                  cout << "=";
                               //   system("cls");
                                  cout << NEWLINE;
                                  cout<< num1 / num2<< endl;     
                                  cout << NEWLINE;
                                  num1 = ans;
                                 // counter = counter + 1;
                                  break;
                                    
                                    case '-':
                                         cout << NEWLINE;
                                         cout << "=";
                                      //   system("cls");
                                         cout << NEWLINE;
                                         cout<< num1 - num2<< endl;     
                                         cout << NEWLINE;
                                         num1 = ans;
                                        // counter = counter + 1;
                                         break;
                                  
                                  case 'x':
                                  exit(EXIT_FAILURE);
                                  break;
                                         default:
                                         cout << "ERROR!";
                                         cout << NEWLINE;
                                         cout << "Continue";
                                         cout << NEWLINE;
                                         break;
                                         }
                                         {
                                     /* cout << "To exit press x.";
                                      exit(x);*/
                                     
                                     }
}
system ("pause");
return 0;   
}

Edited 5 Years Ago by dina154: n/a

ok.... i see what you mean.... well this is my first time here at the website.... Plus i haven't done coding for 3year.... so i'm rusty

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