Can you continue calculate on result?

closed account (N67L8A7f)
Hi!

I'm pretty new at all this coding and trying to figure things out. I got this homework to do a calculator, and I have. With some issues (like forever loop if you put a letter instead of number, don't try it). Now I want to continue calculate with the result for those two numbers given. But I don't realy now where to start or if it's possible with the code I written. The task was just do do a simple calculator but I wan't to take it a step further, mostly for my own experience and learning.

Can anyone point me in the right direction?

Thanks. (Despite the username, I do learn well ;P )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>  

using namespace std;

int main(){

float nr1, nr2;     
char operat;
char choise;

 cout << "***'''**Welcome to calculator***'''***" << endl;
 cout << " " << endl;

 while(true){
 cout << "Please enter a number you want to use: ";   
 cin >> nr1;
 cout << " " << endl;
 cout << "Now enter an Operator from +,-,*,/ you want to use: ";  
 cin >> operat;
 cout << " " << endl;
 cout << "And the other number you want to perform the action with: ";  
 cin >> nr2;
 cout << " " << endl;


  switch(operat){
   case '+':
    cout << nr1 << " + " << nr2 << " = " << nr1+nr2 << endl; 
    cout << " " << endl;
     break;
   case '-':
    cout << nr1 << " - " << nr2 << " = " << nr1-nr2 << endl;  
    cout << " " << endl;
     break;
   case '*':
    cout << nr1 << " * " << nr2 << " = " << nr1*nr2 << endl; 
    cout << " " << endl;
     break;
   case '/':
    cout << nr1 << " / " << nr2 << " = " << nr1/nr2 << endl;   
    cout << " " << endl;
     break;
   }

   cout << "Do you want to continue? Y/N" << endl;
   cin >> choise;
   cout << " " << endl;                          

   if(choise == 'N' || choise == 'n'){
    cout << "Thanks for using Calculator!" << endl;
     return 0;}

 }

}
its possible to keep the result, but you didn't!
each one of your 'case' statements might have:
result = nr1+nr2 //example for +
so that at the end you can say
nr1 = result; //move the answer into the first number
the get nr2 and loop back.

basically, a quick fix, move lines 15 and 16 above the while loop so it only happens once.
then stuff result into nr1 on line 44ish. Next time it loops it reads nr2 only and so on.
is this what you wanted?

most code uses double and rarely float. Use double unless you have a reason to use float instead, unlike integers, where you use the machine word sized (or straight up 'int') by default unless a reason to use another size.
Last edited on
As a first refactor, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

int main() {
    char choice {};

    std::cout << "******Welcome to the calculator******\n";

    do {
        double nr1 {}, nr2 {}, result {};
        char operat {};
        bool bad {};

        std::cout << "\nPlease enter a number you want to use: ";
        std::cin >> nr1;

        std::cout << "Now enter an Operator from +,-,*,/ you want to use: ";
        std::cin >> operat;

        std::cout << "And the other number you want to perform the action with: ";
        std::cin >> nr2;

        switch (operat) {
            case '+':
                result = nr1 + nr2;
                break;

            case '-':
                result = nr1 - nr2;
                break;

            case '*':
                result = nr1 * nr2;
                break;

            case '/':
                result = nr1 / nr2;
                break;

            default:
                std::cout << "Invalid operator\n";
                bad = true;
        }

        if (!bad)
            std::cout << nr1 << ' ' << operat << ' ' << nr2 << " = " << result << '\n';

        std::cout << "\nDo you want to continue? Y/N: ";
        std::cin >> choice;
    } while (choice == 'Y' || choice == 'y');

    std::cout << "\nThanks for using Calculator!\n";
}


put a letter instead of number


The issue here is that >> for a number expects a number and if it doesn't see one the stream extraction fails and the stream is set to fail mode. No more extraction can be done until the stream state has been reset to good and the 'bad' input removed from the input.

As a second refactor with error detection, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <limits>
#include <cstring>

template<typename T = int>
T getInp(const std::string& prm) {
    const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
    T n {};

    while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
        std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return n;
}

int main() {
    const char opers[] {"+-*/"};
    char choice {};

    std::cout << "******Welcome to the calculator******\n\n";

    do {
        double result {};
        char operat {};

        const double nr1 {getInp<double>("Please enter a number you want to use : ")};

        do {
            std::cout << "Now enter an Operator from +,-,*,/ you want to use: ";
            std::cin >> operat;
        } while (std::strchr(opers, operat) == nullptr && (std::cout << "Invalid operator\n"));

        const double nr2 {getInp<double>("And the other number you want to perform the action with: ")};

        switch (operat) {
            case '+':
                result = nr1 + nr2;
                break;

            case '-':
                result = nr1 - nr2;
                break;

            case '*':
                result = nr1 * nr2;
                break;

            case '/':
                result = nr1 / nr2;
                break;
        }

        std::cout << nr1 << ' ' << operat << ' ' << nr2 << " = " << result << '\n';

        do {
            std::cout << "\nDo you want to continue? Y/N: ";
            std::cin >> choice;
        } while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N' && (std::cout << "Invalid input\n"));
    } while (choice == 'Y' || choice == 'y');

    std::cout << "\nThanks for using Calculator!\n";
}


This will check for non-numeric input for numbers, invalid operation etc.
And for a rolling calculator, then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <limits>
#include <cstring>

template<typename T = int>
T getInp(const std::string& prm) {
    const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
    T n {};

    while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
        std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return n;
}

int main() {
    const char opers[] {"+-*/"};
    char choice {};

    std::cout << "******Welcome to the calculator******\n\n";

    double nr1 {getInp<double>("Please enter a number you want to use : ")};

    do {
        double result {};
        char operat {};

        do {
            std::cout << "Now enter an Operator from +,-,*,/ you want to use: ";
            std::cin >> operat;
        } while (std::strchr(opers, operat) == nullptr && (std::cout << "Invalid operator\n"));

        const double nr2 {getInp<double>("And the other number you want to perform the action with: ")};

        switch (operat) {
            case '+':
                result = nr1 + nr2;
                break;

            case '-':
                result = nr1 - nr2;
                break;

            case '*':
                result = nr1 * nr2;
                break;

            case '/':
                result = nr1 / nr2;
                break;
        }

        std::cout << nr1 << ' ' << operat << ' ' << nr2 << " = " << result << '\n';
        nr1 = result;

        do {
            std::cout << "\nDo you want to continue? Y/N: ";
            std::cin >> choice;
        } while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N' && (std::cout << "Invalid input\n"));
    } while ((choice == 'Y' || choice == 'y') && (std::cout << "\nFirst number is " << nr1 << '\n'));

    std::cout << "\nThanks for using the Calculator!\n";
}

Last edited on
closed account (N67L8A7f)
Thanks guys! You are genius. I have been out for a couple of days with Covid, horrible disease, but now I have taken my time to look over your answers and understand what you are writing. Those examples are badass, the last one especially. It is a way over my head but as I read I will learn more. And you made me understand a lot! This is fun and funnier it gets. Thanks again!
Topic archived. No new replies allowed.