Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read both integers and characters from command prompt

Status
Not open for further replies.

Wendyj2

Instructor
Nov 5, 2004
8
CA
This is my problem:

I want the user to input a sentence or line of both integers and characters, read in the whole line,spaces and all, then parse through the line, if it is an integer push it into an vector of integers, if it is a character, push it into a vector of characters.

This is what I have so far, but it errors out on the "while(getline(line, s)" line of code


vector<int> operands;
vector<char> actionOperator;

cout << "Please enter an arithmetic equation: " ;
char str[50];
string line;
string s;
char c;

cin.getline(str, 50);

line = str;
cout << str << endl;

while(getline(line, s))
{
if(s.empty())
break;
// make an istream object
istringstream iss(s);
int i = 0;
if(iss >> i)
operands.push_back(i);
else if (iss >> c)
actionOperator.push_back[c]
}
 
There's a couple issues that can be improved.

First, you call cin.getline to read in a line or 50 characters, whichever is shorter. There's no reason to use that version unless you specifically want to cut the line off at 50 characters. If you don't, then use the string version of getline.

Second, you try to use the string version of getline in the while loop after calling cin.getline just before it. You only need one version, and that version should be in the while control.

Next, you try to use the string version of getline in the while loop, but are using the wrong parameters. It should be:
Code:
while (getline(cin, line))
If you make that change you can get rid of str and s, they aren't necessary.

Once those parts are fixed I think it might work better.
 
Thanks for your help. I have made the changes and of course they do work. My problem is that I wanted to be able to tell if it was an int or a character being entered. This way it comes in as a string. I will now have to figure out a way to test for data type so I can carry on with it.

I think I just wanted all the steps at once. Always a bad plan.

On to the next challenge,thanks again

 
Your current code already tests for the data type. Is it not working as you expect?

I just realized one problem with this part of the code:
Code:
        if(iss >> i)
            operands.push_back(i);
        else if (iss >> c)
            actionOperator.push_back[c]
It's trying to read in an integer. If there is an integer there, it works. If there isn't, it tries to read in a character. I think this idea is sound and is a good way to determine the data type.

The problem, though is that when the attempt to read the integer fails, it puts the stringstream into a fail state. You have to clear that before reading again. So I would change the else if to an else, then inside that else block call iss.clear() first. After you clear the failbit you can attempt to read a character and push it back onto the vector.
 
I have tried to do what you are suggesting but it still doesn't read in the string (changed it from char to string). If I put the "string catcher" first it works but of course everything is a string then.

This is what I have now:

vector<int> operands;
vector<string> actionOperator;

cout << "Please enter an arithmetic equation: " ;
string line;

// read in the WHOLE line
while(getline(cin, line))
{
if(line.empty())
break;
// make an istream object
istringstream iss(line);
string str2 = "";
// break the line up wherever there are spaces
while(getline(iss, str2, ' '))
{
// make another istream object
istringstream iss2(str2);
int i = 0;
string c;
if(iss2 >> i)
{
operands.push_back(i);
}
else
{
// clear iss2
iss2.clear();
if(iss2 >> c)
actionOperator.push_back(c);
}
}
}
 
You made some other changes, can you explain why you made those? What does your input look like?

When reading in a string the operator>> stops at whitespace, not integers. So if your input is 2+3-1 then first it will read in the 2 as an integer, but then it will read in "+3-1" as a string. If you were reading in characters, it would read in '+' and then the next time through it would read the 3.

Also, since operator>> skips whitespace, I don't see why you needed to add the second stringstream to separate by space.
 
Actually the reason that I did it this way is because each integer, float and string will have a whitespace between them, so they will indeed need to be delimited.

I am trying to do the famous infix to postfix and have found all kinds of useful stuff, but none of what I am finding is white space delimited.

Should have pointed that out right from the beginning.
 
Wendyj2 said:
Actually the reason that I did it this way is because each integer, float and string will have a whitespace between them, so they will indeed need to be delimited.

But your original code was already separating by space because that's what operator>> does automatically. Why did you change it? Did it not work right? What made you think that the problem was the spaces?
 
You may be right about that... I will relook at it in the morning.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top