Calculator Program

So I have a small project where we have to construct a calculator program

However, the problem I have is:

ask for the user to enter their equation all at once (i.e., as "4 * 5", with each piece separated by whitespace) instead of prompting for each piece separately

My question is, what variable do I store the whole equation under? A string?

Also, my professor is asking us to use cin.peek to predetermine what the binary operator is being used (ie +, -. *), how do I include this?
You don't need to store the whole input at once, though if you did it would have to be as a string. As for the rest of it, just use the stream extraction operator (std::cin >> ...) to input the first value. I don't really know why std::cin.peek is required, but you could just test for the operator after taking in the first number and using std::cin.ignore to get rid of the useless whitespace.
my professor just emailed me the following code

1
2
3
4
5
6
7
8
9
10
11
    cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";
    while (    cin.peek() == ' '  || cin.peek() == '\t'
        || cin.peek() == '\n' || cin.peek() == '\v'
        || cin.peek() == '\f' || cin.peek() == '\r' )
    cin.get();
    
if (cin.peek() >= 'a' && cin.peek() <= 'z') {
cin >> op >> a;
} else if (cin.peek() >= '0' && cin.peek() <= '9') {
cin >> a >> op >> b; 
}



Can someone please explain to me what is exactly going on in this code?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int a, b;
std::string op;
std::cout << "Please enter an expression: ";
std::cin >> a;

// pointless
while (std::cin.peek() == ' ')
    std::cin.ignore();

std::cin >> op;
std::cin >> b;

// process operator and values... 


Of course, you would need to modify this to work for things like 'ln 5', but you get the basic idea. Maybe you can think of a way to make std::cin.peek necessary, too...

EDIT:
Maybe you can use std::cin.peek to test to see if the first character is a number, and process the equation differently otherwise.
Last edited on
yes but my professor wants me to input the following code

1
2
3
4
5
6
7
8
9
10
11
   cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";
    while (    cin.peek() == ' '  || cin.peek() == '\t'
        || cin.peek() == '\n' || cin.peek() == '\v'
        || cin.peek() == '\f' || cin.peek() == '\r' )
    cin.get();
    
if (cin.peek() >= 'a' && cin.peek() <= 'z') {
cin >> op >> a;
} else if (cin.peek() >= '0' && cin.peek() <= '9') {
cin >> a >> op >> b; 
}


I am at a loss here, what does this following exactly do?

I understand that cin.peek peeks a certain set of characters to determine what they are, so how does this work when trying to figure out what kind of equation the user input?
The code is like this:

Ask the user to enter an equation

lines 2->5 pretty much ignore any whitespaces at the start of an input so if you have something like
   \r\n    12
it becomes
12
since you are using std::cin >> after all and it stops at the next whitespace.

line 7 checks if the next character is a letter if so read in the operator and 1 variable.

line 9 is otherwise if the next character is an integer then read in a variable, operator, and another variable.

You figure out the equal based on what the operator is. I would assume the operator is a string and not a character so you can input ln otherwise you'd probably only be able to do +,-,*,/
> yes but my professor wants me to input the following code

This is what your professor is attempting to do:
1
2
3
4
a. skip over leading white-space
b. peek at the next (first non-white-space) character
      if it is alphabetic  => read operation, number: eg. 'ln 5'
      else if it is a digit => read number, operation, second number: eg. '4 * 5'


This is one of the sane ways of doing it:
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
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    double number, second_number ;
    std::string operation ;

    std::cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";

    // skip over leading white-space
    char c ;
    std::cin >> c ; // read the first non-whitespace character
    std::cin.putback(c) ; // and put it back

    // look at the first non-whitespace character
    if( std::isdigit(c) ) // if the first char is a digit
    {
        // input is of the form 'number operator number';
        // read it into number, operation, second_number
        std::cin >> number >> operation >> second_number ;

        std::cout << "number==" << number << " operation=='" << operation
                   << "' second_number==" << second_number << '\n' ;
    }

    else if( std::isalpha(c) ) // if the input starts with an alphabet
    {
        // input is of the form 'operator number';
        // read it into operation, number
        std::cin >> operation >> number ;

        std::cout << "operation=='" << operation << "' number==" << number << '\n' ;
    }
    
    // else // error in input: first non-whitespace character is neither a digit nor a letter
}

http://coliru.stacked-crooked.com/a/cc9f207dfdaa96d9
use this
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
#include <iostream>

//    writing this line here would be better
//instead of inside main().
using namespace std;

int main()
{
    //    for beautiful coding
    //always add your declarations
    //to the beginning.
    char o;
    int x;
    int y;

    //    it will keep looping as long
    //as x isnt equal to -1.
    while(x != -1)
    {
        cout << "enter a number: \n";
        cin >> x;
        cout << "\n";
        cout << "\n";
        cout << "1 = multiplication  ";
        cout << "2 = division  ";
        cout << "3 = addition  ";
        cout << "4 = subtraction  ";
        cout << "-----------------------------------------------------------------------------------------\n";
        cout << "enter your choice: \n";
        cin >> o;
        cout << "enter your last number: \n";
        cin >> y;
        //it should be == instead of =
        //haha sorry i didnt realized it :D
        if ((o == 1))
            cout << x * y << endl;
        else if ((o == 2))
            cout << x / y << endl;
        else if ((o == 3))
            cout << x + y << endl;
        else if ((o == 4))
            cout << x - y << endl;
        else
            cout << "you entered an invalid operator\a";
    }

    return 0;
}
There's a few problems wrong with that jdogsis.
1) it does not meet his requirements
2)your operator is a character and not a string
3) all your if statements are wrong characters have single quotes around them(' ')
4) they can enter just an operator and 1 number example ln 5 or log 10
5) What if they want to do something like -1 * 100 (anyways you never explicitly told the user what the exit key was)
Topic archived. No new replies allowed.