cin.peek help equation

So far I did this but I have to use cin.peek and I'm not really sue how it works. I have to delete the line(s) I'm currently using to read in the user's input, and in its place put the following code (to ignore all initial whitespace on the input stream):

1
2
3
4
5
// ignore all initial whitespace
while (    cin.peek() == ' '  || cin.peek() == '\t'
        || cin.peek() == '\n' || cin.peek() == '\v'
        || cin.peek() == '\f' || cin.peek() == '\r' )
    cin.get();


The program should look something like this:

1
2
3
4
Please enter an expression (like `4 * 5` or `ln 5`): 4 * 5
4 * 5 = 20
Please enter an expression (like `4 * 5` or `ln 5`): ln 5
ln 5 = 1.60944


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 <cmath>
// pow()

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;


int main() {
    double  a;         // to hold the user's first real number
    double  b;         // to hold the user's second real number
    string operation;  // to hold the user's operation

    cout << "Please enter a real number: ";
    cin >> a;

    cout << "Please enter an operation (+, - , *, /, **): ";
    cin >> operation;

    cout << "Please enter another real number: ";
    cin >> b;

    cout << endl;

    cout << a << " " << operation << " " << b << " = ";

    if ( operation.compare("+") == 0 )
        cout << a + b;
    else if ( operation.compare("-") == 0 )
        cout << a - b;
    else if ( operation.compare("*") == 0 )
        cout << a * b;
    else if ( operation.compare("/") == 0 )
        cout << a / b;
    else if ( operation.compare("**") == 0 )
        cout << pow(a,b);
    else
        cout << "ERROR: Operation not supported";

    cout << endl;

    return 0;  // success
}
Last edited on
If you are trying to ignore all whitespace in the user's input, and just get the expression the user entered, then something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <typename T>
void getValue(T &v) {
    for (std::cin >> v; !std::cin; std::cin >> v) {
        std::cout << "ERROR\n";
        std::cin.clear(); // clear error flags
        std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    }
}

void getExpression(std::string &mystr) {
    double f, k;
    std::cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";
    getValue<double>(f);
    
    while (isspace(std::cin.peek()))
        std::cin.get();
    
    std::cin >> mystr;
    getValue<double>(k);
    
    mystr = std::to_string(f) + " " + mystr + " " + std::to_string(k);
}


Full example:
http://coliru.stacked-crooked.com/a/364f02da7bedf7f4
Last edited on
I'm not getting the output of 20 and not getting anything for ln 5. I'm getting an error.
Last edited on
This thread may be of interest: http://www.cplusplus.com/forum/beginner/125389/
What do I enter for the input after I debug it?
It won't find ln 5 though
Last edited on
If you are looking for the actual result you will have to do something like:

1
2
3
4
if(operation == "ln")
{
    result = std::log(first_operand);
}


Also if it is ln or log or something like that you should only need one operand(after).

*typo

*edit2 forgot that c++ log function is natural log and log10 is log by default (was thinking ln/log)
Last edited on
So where do I insert that part, I tried putting it in but it gave me errors.
Last edited on
Well for starters you are going to want to check if the next character is a letter or a number. If it is a letter you can safely say they want log or natural log. Otherwise they are doing an expression like 4 * 5. Then get the desired result based on the chosen operation.
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 <sstream>
#include <cctype>
#include <cmath>
#include <limits>

void doMath(const std::string& expression) {
    std::istringstream iss(expression);
    double f, k;
    std::string operation;
    
    std::cout << expression << " = ";
    
    if (iss >> f >>  operation  >> k) {
        
        if ( operation.compare("+") == 0 )
            std::cout << f + k;
        else if ( operation.compare("-") == 0 )
            std::cout << f - k;
        else if ( operation.compare("*") == 0 )
            std::cout << f * k;
        else if ( operation.compare("/") == 0 )
            std::cout << f / k;
        else if ( operation.compare("**") == 0 )
            std::cout << pow(f,k);
        else
            std::cout << "ERROR: Operation not supported";

        std::cout << std::endl;
    }
}    

template <typename T>
void getValue(T &v) {
    for (std::cin >> v; !std::cin; std::cin >> v) {
        std::cout << "ERROR\n";
        std::cin.clear(); // clear error flags
        std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    }
}

void getExpression(std::string &mystr) {
    double f, k;
    std::cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";
    getValue<double>(f);
    
    while (isspace(std::cin.peek()))
        std::cin.get();
    
    std::cin >> mystr;
    getValue<double>(k);
    
    mystr = std::to_string(f) + " " + mystr + " " + std::to_string(k);

	if(operation == "ln")
 {
		result = std::log(first_operand); }


}

int main() {
    std::string mystr;
    getExpression(mystr);
    doMath(mystr);
}
I tried it but don't think it worked.
You just mashed the if statement I suggested into smac's code. For that to work it would have to be in the do math function and change the first_operand to f.

Also I would suggest applying the code to your original and not just copy/paste. If you do the copy/paste method you may not understand everything.

BY the way did you look at the link JLBorges linked earlier? He helped one of your classmates with getting the equations now you just need to return the result based on the operator.
Last edited on
ya I looked at the other link but it was different, thanks anyway just turned it what I had.
Last edited on
Topic archived. No new replies allowed.