0

I'm attempting to read two bignums and an operator from a file into integer vectors (in order to do math on them) and I'm not allowed to use C++ strings. The file is in the format:

2308957235....
add
234989234786....

I'm not very familiar with the C++ file handling, so while I can read the numbers into the vector, I can't get it to recognize the end of a line in order to start the next one. After opening the file I have:

vector<int> numbers;
char inputDigit;
while(in>>inputDigit)
    numbers.push_back(inputDigit-48);

which just throws everything in the file into the vector, ignoring the spaces or linebreaks. I've been banging my head against this for a few hours, so any help would be greatly appreciated.

Farlo
  • 17
  • 7
  • 1
    Keep that `while` loop inside another loop. Have the inside loop add to a string in the outside loop until it encounters the character 'a'. – irrelephant Nov 10 '12 at 02:43
  • does this also mean you cannot use char pointers? if not you can test be testing the next char to see if it is "/n" – Syntactic Fructose Nov 10 '12 at 03:05
  • I'm not sure what char pointers are, but I think I've figured it out. Basically following what you said irrelephant, I came up with:
    while(in>>inputDigit)
        {
            if(inputDigit == 'a' || inputDigit == '+')
                break;
            else
                numbers.push_back(inputDigit-48);
        }
    
    – Farlo Nov 10 '12 at 04:02

1 Answers1

0

Better use two vectors. One for numbers and one for operators. Once you are done, use two pop_back() from number vector and one form operator vector. While reading file use sprintf. Have some logic to first accept number, operator, number format.

Vishal Kumar
  • 762
  • 1
  • 7
  • 15