Neyeli
Neyeli

Reputation: 9

c++ if(cin>>input) doesn't work properly in while loop

I'm new to c++ and I'm trying to solve the exercise 6 from chapter 4 out of Bjarne Stroustrups book "Programming Principles and Practise Using C++ and don't understand why my code doesn't work.

The exercise:

Make a vector holding the ten string values "zero", "one", ..., "nine". Use that in a program that converts a digit to its corresponding spelled-out value: e.g., the input 7 gives the output seven. Have the same program, using the same input loop, convert spelled-out numbers into their digit form; e.g., the input seven gives the output 7.

My loop only executes one time for a string and one time for an int, the loop seems to continue but it doesn't matter which input I'm giving, it doesn't do what it's supposed to do.

One time it worked for multiple int inputs, but only every second time. It's really weird and I don't know how to solve this in a different way.

It would be awesome if someone could help me out. (I'm also not a native speaker, so sorry, if there are some mistakes)

The library in this code is a library provided with the book, to make the beginning easier for us noobies I guess.

#include "std_lib_facilities.h"

int main()
{
   vector<string>s = {"zero","one","two","three","four","five","six","seven","eight","nine"};

   string input_string;
   int input_int;

   while(true)
   {
        if(cin>>input_string)
        {
             for(int i = 0; i<s.size(); i++)
             {
                  if(input_string == s[i])
                  {
                       cout<<input_string<<" = "<<i<<"\n";
                  }
             }
        }

        if(cin>>input_int)
        {
            cout<<input_int<<" = "<<s[input_int]<<"\n";
        }

    }

    return 0;
}

Upvotes: 0

Views: 1730

Answers (2)

Ziezi
Ziezi

Reputation: 6457

if(cin >> input) doesn't work properly in while loop?

A possible implementation of the input of your program would look something like:

std::string sentinel = "|";
std::string input;

// read whole line, then check if exit command
while (getline(std::cin, input) && input != sentinel)
{
    // use string stream to check whether input digit or string
    std::stringstream ss(input);

    // if string, convert to digit

    // else if digit, convert to string

    // else clause containing a check for invalid input
}

To discriminate between int and string value you could use peek(), for example. Preferably the last two actions of conversion (between int and string) are done by separate functions.

Assuming the inclusion of the headers:

#include <iostream> 
#include <sstream> 

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

When you (successfully) read input from std::cin, the input is extracted from the buffer. The input in the buffer is removed and can not be read again.

And when you first read as a string, that will read any possible integer input as a string as well.

There are two ways of solving this:

  1. Attempt to read as int first. And if that fails clear the errors and read as a string.

  2. Read as a string, and try to convert to an int. If the conversion fails you have a string.

Upvotes: 1

Related Questions