Hi all.
I have an assignment for school and one of the requirements is to read in from a file of a specific format and do operations on a data structure based on that input.
The format for the input is as follows:
4
insert 1,5,10,9,7,4
search 5
end
The problem that I'm reaching is when I try to read in the number "10". I am trying to store each number in an array of integers, but I am reading in the characters one by one, meaning when I get to the number 10 I will read in 1 and then 0 as two separate numbers. I'm sure there is some function on input streams to do what I want but I couldn't find it.
Here is the snippet of my code:
//Keep reading input while there still is input
char operation;
char peek;
//This highest number of values you can insert on one line is 50
int inputArray[50];
int numOfInputs;
while(!inputFile.eof())
{
//Read in the next operation to be done
inputFile>>operation;
if(operation == 'i')
{
//Next operation is an insert
for(int i = 0; i < 5; i++)
{
//Read the rest of the word insert insert
inputFile>>operation;
}
//Next read in all the numbers to be input
bool flag = true;
numOfInputs = 1;
do
{
//Peek at the next character
peek = inputFile.peek();
//Check if the next character is a number
if(peek >= 48 && peek <= 57)
{
//Next character was a number
int i = 0;
inputFile>>inputArray[0];
numOfInputs++;
peek = inputFile.peek();
while(peek != ',')
{
////////
//As you can see here I am reading in a multi-digit number
//to multiple indexes in an array of ints when I want the
//multi digit number to be placed in one index of the array
////////
inputFile>>inputArray[i];
i++;
peek = inputFile.peek();
}
}
elseif(peek == 44)
{
//Next character is a comma, meaning there is a number afterwards
//Take out the comma
inputFile>>operation;
}
else
{
//Next character is a letter, meaning a new operation
//Break out of the do-while loop
flag = false;
}
//********************
//Do the input operation here
//********************
}while(flag);
}
elseif(operation == 's')
{
//Next operation is a search
}
elseif(operation == 'e')
{
//The list of operations is over
}
}