0

I've just started with C++ a few weeks ago and need some help for my "homework".

I need to read:

row1: int int
row2: int char

from a text file. My code looks like this:

int main() {
  [...]
  ifstream fin(argv[1]);
  int i{0}, tmp;
  while (fin >> tmp) {
    cout << pos << "zahl" << tmp << endl;
    if (pos == 0) {
      wagen = tmp;
    }  // needed for an array
    if (pos == 1) {
      reihen = tmp;
    }
    i++;
    [...] return 0;
  }

my problem is how do you work around row2+? I tried typecasting and googled for over a hour but didn't found anything useful.

David G
  • 94,763
  • 41
  • 167
  • 253

1 Answers1

0

There is nothing preventing you from having such a structure. You simply have to read outside the loop:

int x, y;
std::cin >> x >> y;

int val; 
char c;


while (!std::cin.eof())  /* while the end of the stream has 
                          * not been reached yet */
{
  int val;
  std::cin >> val;

  /* getting characters from a stream is tricky; consider a line such
   * as "123    c\n"; you read the int, 123, but then when you read the
   * character, you'll want the 'c', not a whitespace character, so
   * "std::cin >> std::ws" consumes the "leading" whitespace before the
   * character. */
  char c = (std::cin >> std::ws).get();

  /* next step is to ignore the trailing whitespace on the current line */
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Edit: some explanation is probably needed, so I added it in code comments.

P.S. I haven't tested the code myself.

Victor
  • 13,914
  • 19
  • 78
  • 147
  • ty for your help, but i guess im just to dump for that kind of task. – maybeAboomerBoi Nov 15 '19 at 20:42
  • @maybeAboomerBoi nobody is. you aren't. Just ask about what you do not understand. If you a beginner in terms of C++, I recommend checking [this](https://stackoverflow.com/q/388242/1673776) out. If you're a beginner in terms of programming, that's fine. You should pick some courses or books if you want to learn! – Victor Nov 16 '19 at 09:11