简体   繁体   中英

“Principles and Practice Using C++” chapter 6.3.1 code error?

I am working my way through Principles and Practice Using C++ . I've been understanding it kind of well but I've hit a wall recently with chapter 6 . You are suppose to start writing a calculator program and it gradually becomes more feature rich as you continue on to it. It eventually leads into tokens which is confusing the heck out of me.

ANYWHO! my question is that I'm following this code and it isn't working as it is explaining. I've gone over the code against the book several times and it looks similar. The code just keeps taking in lval and not doing anything with it. After 3 cin entries it will just show me whatever lval was first set to. I'm also not 100% sure on the use of cin >> op in the while loop. What makes it stop? When does it know to stop? The error function doesn't seem to work either. I keep trying to break the program but it doesn't pop up any of the error messages.

This is just frustrating because I'm learning and It's hard for me to figure my own problems out without a mentor :/ Thanks everyone for your time though! incoming code...it's what I have so far

#include "std_lib_facilities.h"

int main()
{
    cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/"")" << endl;
    int lval = 0;
    int rval;
    char op;
    /*int res;*/
    cin >> lval; //read left most number
    if (!cin) error("No first operand");

    while (cin >> op) //Repeatedly read operand and right value
    {
        cin >> rval;
        if (!cin) error("No second operand");
        switch(op)
        {
        case '+':
            lval += rval; //add: lval = lval + rval
            break;
        case '-':
            lval -=rval;//subtract: lval = lval - rval
            break;
        case '*':
            lval *= rval; //Multiply: lval = lval * rval
            break;
        case '/':
            lval /= rval; //Divide: lval = lval / rval
            break;
        default:
            cout << "Result: " << lval << endl;
            keep_window_open();
            return 0;
        }
    }
    error("Bad expression");
}

PS I tried using break points to see how to code works line by line, but it starts throwing me into the iostream files and I have no clue how to read those at this point!

It does sort of work. For instance if you introduce the following sequence:

3 <enter>
+ <enter>
3 <enter>
d <enter>
3 <enter>

It produces:

Result: 6

The reason is cin always expects an enter to finish. There is also an error in the logic and even when you want to stop the execution you have to introduce an extra dummy value. To solve that you must check the operator before asking for the rval.

EDIT:

Probably this would be close to what you want:

#include "iostream"
#include <cstdio>

 using namespace std;

 int main()
 {
     cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/    "")" << endl;
     int lval = 0;
     int rval;
     char op;
     /*int res;*/
     cin >> lval; //read left most number
     if (!cin) printf("No first operand");

     while (cin >> op) //Repeatedly read operand and right value
     {
         if(op != '+' && op != '-' && op != '*' && op != '/')
         {
             cout << "Result: " << lval << endl;
             //keep_window_open();
             getchar();
             return 0;
         }

         cin >> rval;
         if (!cin) printf("No second operand");
         switch(op)
         {
             case '+':
                 lval += rval; //add: lval = lval + rval
                 break;
             case '-':
                 lval -=rval;//subtract: lval = lval - rval
                 break;
             case '*':
                 lval *= rval; //Multiply: lval = lval * rval
                 break;
             case '/':
                 lval /= rval; //Divide: lval = lval / rval
                 break;
         }
     }
     printf("Bad expression");
 }

The problem comes with your first outputted line. Instead of two sets of quotes, you need to escape one set with a backslash, telling C++ to treat the quotes as a character rather than the start or end of a string. Also, your while loop seems to test a very odd condition. I don't know what has gone wrong, but it just continually takes in the operator and (presumably) the code in the while loop never gets executed. A better code solution is below.

#include "std_lib_facilities.h"

int main()
{
    cout << "Please enter an expression (we can handle \"+\",\"-\",\"*\",\"/\")" << endl;
    int lval = 0;
    int rval;
    int loop = 1;
    char op;
    /*int res;*/
    while (loop == 1) //Repeatedly calculate
    {
    cout<<"Please enter the first number"<<endl;
    cin >> lval; //read left most number
    if (!cin) error("No first operand");
    cout<<"Please enter operator"<<endl;
    cin >> op;
    cout<<"Please enter second number"<<endl;
    cin >> rval;
    if (!cin) error("No second operand");
    switch(op)
        {
        case '+':
            lval += rval; //add: lval = lval + rval
            break;
        case '-':
            lval -=rval;//subtract: lval = lval - rval
            break;
        case '*':
            lval *= rval; //Multiply: lval = lval * rval
            break;
        case '/':
            lval /= rval; //Divide: lval = lval / rval
            break;
        default:
            cout << "Result: " << lval << endl;
            keep_window_open();
            return 0;
        }
    cout<<"Enter 1 to calculate a new expression, or 0 to exit."<<endl;
    cin>>loop;
    }
    error("Bad expression");
}

Dismiss the default statement, maybe you should not use default for displaying the result. Try this:

#include "std_lib_facilities.h"

int main()
{
    cout << "Please enter an expression (we can handle ""+"",""-"",""*"",""/"")" << endl;
    int lval = 0;
    int rval;
    char op;
    /*int res;*/
    cin >> lval; //read left most number
    if (!cin) error("No first operand");

    while (cin >> op) //Repeatedly read operand and right value
    {
        cin >> rval;
        if (!cin) error("No second operand");
        switch(op)
        {
        case '+':
            lval += rval; //add: lval = lval + rval
            break;
        case '-':
            lval -=rval;//subtract: lval = lval - rval
            break;
        case '*':
            lval *= rval; //Multiply: lval = lval * rval
            break;
        case '/':
            lval /= rval; //Divide: lval = lval / rval
            break;
        }
        cout << "Result: " << lval << endl;
        keep_window_open();
        return 0;
    }
    error("Bad expression");
}

this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM