-4

I am complete starter and i cant figure out how to use the while function in a very simple calucator code.

ive already tried putting the while function in the code in different ways but nothing of it seems to work it just stop without even giving me the final result of the first "problem"

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{

    int number1;
    int number2;
    char op;
    int result;

    cout << "Give first number: ";
    cin >> number1;

    cout << "Give second number: ";
    cin >> number2;

    cout << "Chose operator(+ - / * ): ";
    cin >> op;

    if (op == '+')
    {
        result = number1 + number2;
    }
    else if (op == '-')
    {
        result = number1 - number2;
    }
    else if (op == '*')
    {
        result = number1 * number2;
    }
    else if (op == '/')
    {
        result = number1 / number2;
    }

cout << "The result is: " << result << endl;

system("pause");
return 0;
}

everything its working fine this way i just want it to loop after the first problem and ask again for another one...

2 Answers2

2

I am complete starter and i cant figure out how to use the while function in a very simple calucator code.

Its time for you to get your hands on a good C++ book...

Syntax of while loop: while (some_condition) { /* Body... */ }

Rectified code of above problem:

#include <iostream>

int main()
{
    int number1, number2, result;
    char op;
    bool is_loop = true;

    while (is_loop) {
        std::cout << "Give first number: ";
        std::cin >> number1;

        std::cout << "Give second number: ";
        std::cin >> number2;

        std::cout << "Chose operator(+ - / * ): ";
        std::cin >> op;

        switch (op)
        {
        case '+':
            result = number1 + number2;
            break;
        case '-':
            result = number1 - number2;
            break;
        case '*':
            result = number1 * number2;
            break;
        case '/':
            result = number1 / number2;
            break;
        default:
            is_loop = false;
        }
        std::cout << "The result is: " << result << std::endl;
        std::cin.get();
    }
    return 0;
}
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • for some reason i needed to add #include "pch.h" also it tells me that cout and cin arent members of std? – raphaelll Feb 02 '19 at 18:06
  • ohh it worked when i pasted in an empty project maybe the Solusion was the problem Thanks btw – raphaelll Feb 02 '19 at 18:15
  • @raphaell *PCH* stands for *Pre-compiled header*, which, in fact, is a **pre-compile** header for your program where you are *required* to include the necessary headers you need to keep the main code *as clean and as relevant as possible*... Judging by this fact, I can see that you are using an MSVC compiler, whose default precompiled-header is `stdafx.h` which can be [changed/disabled](https://learn.microsoft.com/en-us/cpp/build/reference/yu-use-precompiled-header-file?view=vs-2017)... – Ruks Feb 02 '19 at 18:27
  • 1 more thing i tried to change the default: is_loop = flase; break; to case: 'q': is_loop = flase; break; to quit but it doesnt work why is that? although it worked the first time the second didnt – raphaelll Feb 02 '19 at 18:40
  • @raphaell It is working as intended for me, did you mean `case 'q': case 'Q': is_loop = false;` because the ASCII characters are case-sensitive... – Ruks Feb 02 '19 at 18:54
  • yes it worked for me as well but only 1 time when i run it again i get an error – raphaelll Feb 02 '19 at 19:07
  • i found a way to make it work every time without crashing i just puted case 'q' : result = is_loop = flase – raphaelll Feb 02 '19 at 19:15
0

you may try something like this.. this might be the simplest way of doing this..

do{
//your calculator code
char ch;
cout<<"Do you want to continue"<<endl;
cin>>ch;
while(ch=='Y'||ch=='y');