Reputation: 5
This seems like it should be ridiculously simple, but I can't figure it out. So I'm reading in values from a file, and I need to know if the next input from that file is going to be a string or an int. Something along the lines of:
if(next command from file is an int){ do this; }
else{ do this; }
here's the code I have so far:
while (!(myFile.eof())) {
char inPeek = myFile.peek();
int input;
string command;
if (isdigit(inPeek)) {
int input;
myFile >> input;
if (command == "push") {
MyStack.push(input);
cout << "Pushed " << input << endl;
}
else {
MyQueue.append(input);
cout << "Appended " << input << endl;
}
}
else {
myFile >> command;
if (command == "pop") {
MyStack.pop();
cout << "Popped" << endl;
}
if (command == "serve") {
MyQueue.serve();
cout << "Served" << endl;
}
}
}
But if(isdigit(inPeek))
never returns true. The file that I'm reading from looks like this essentially like this:
append 10
serve
append 20
append 30
serve
push 10
push 50
push 20
push 20
pop
Upvotes: 0
Views: 66
Reputation: 86
You can't. There's no a way to know the type of a stream of bytes.
The input is read as a stream of bytes and that stream could be interpreted as a number, a character or a string (or whatever you want).
As PRP said, you should read the input as a string and check inside your program whether your commands need some other input or not.
Also, the function 'isdigit' will return true when the byte read is between 0 and 9.
Upvotes: 1