Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:45:01+00:00 2026-06-04T01:45:01+00:00

In a C++ program I’m trying to process user input which consists of integer

  • 0

In a C++ program I’m trying to process user input which consists of integer operands interspersed with operators (+ – / *). I have the luxury of requiring users to put whitespace(s) before and after each operator. My approach is to assume that anything that’s not an int is an operator. So as soon as there’s a non eof error on the stream, I call cin.clear() and read the next value into a string.

#include <iostream>
#include <string>

//in some other .cpp i have these functions defined
void process_operand(int);
void process_operator(string);

using namespace std;

int main()
{
    int oprnd;
    string oprtr;
    for (;; )
    {
        while ( cin >> oprnd)
            process_operand(oprnd);
        if (cin.eof())
            break; 
        cin.clear();
        cin >> oprtr;
        process_operator(oprtr);
    }
}

This works fine for / and * operators but not for + – operators. The reason is that operator>> eats away the + or – before reporting the error and doesn’t put it back on the stream. So I get an invalid token read into oprtr.

Ex: 5 1 * 2 4 6 * /   works fine
    5 1 + 2 
          ^ ---> 2 becomes the oprnd here.

What would be a good C++ way of dealing with this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:45:04+00:00Added an answer on June 4, 2026 at 1:45 am

    Read in std::strings and convert them using boost::lexical_cast<> or its equivalent.

    int main()
    {
        string token;
        while ( cin >> token) {
            try {
                process_operand(boost::lexical_cast<int>(token));
            } catch (std::bad_cast& e) {
                process_operator(token);
            }
        }
    }
    

    Postscript: If you are allergic to Boost, you can use this implementation of lexical_cast:

    template <class T, class U>
    T lexical_cast(const U& u) {
      T t;
      std::stringstream s;
      s << u;
      s >> t;
      if( !s )
        throw std::bad_cast();
      if( s.get() != std::stringstream::traits_type::eof() )
        throw std::bad_cast();
      return t;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program requires an input of an arbitrary large unsigned integer which is expressed
I program in Java and have been trying to understand exactly what operator overloading
This program takes 2 numbers from user input, asks them whether they'd like to
Program A, is a c program that endlessly, receives input in stdin, process it
My program needs to know when a user ejects cd disc. Is there some
program s; type info = record name, surname: string; min, sec: integer; end; arrays
Program: program s; type info = record name, surname: string; min, sek: integer; end;
My program collects the number of bottles collected by four rooms. When the user
My program is fetching messages from a database, which contains English, German and several
program TypeCategory; {$R+} var sInt : shortint; iInt : integer; begin readln(iInt); sInt :=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.