-2

suppose I have input like

1 2 34 4 58975 6
2 356 4 5 
3 77 5 89 1

now I want to read the input like first number 1 in a number variable and in a character variable[say. var] the space in between 1 and 2, next step it will decide whether var is equals to '\n' or not,if not then read again .this time it will read number 2 and the space in between 2 and 3.similar mannaer ,in last the number variable will read 6 and var will read '\n'.as var=='\n' ,I want my program to go next line and read again in the same manner. and when the input reading is finished I want my program to terminate!! I know how many lines are there in the input. I want the c++ code of this program. I myself made one but itz not working. here is the code

#include <bits/stdc++.h>
using namespace std;

int main()
{
char a;
int line =1,temp;
while(line<=3){
    while(1){
            cin>>temp>>a;
            if(a!='\n'){

        }else{

            break;
            }
        }
      ++line;
    }
   cout<<cnt;
  return 0;
}
Amartya Roy
  • 21
  • 1
  • 5
  • 1
    What happens if you try to build and run your code? In which way does it not satisfy your goal? – Yunnosch Mar 31 '18 at 07:52
  • 1
    First of all, please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Then continue continue to read about [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string), [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline), and [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream). – Some programmer dude Mar 31 '18 at 07:52
  • In short, you don't need to to that explicit newline check. Your code could be made much more simple. – Some programmer dude Mar 31 '18 at 07:53
  • Please format your code consistently, preferably with an automatic tool. This makes it readable, which is required for understanding, which in turn is required for finding and fixing bugs. – Ulrich Eckhardt Mar 31 '18 at 08:07
  • I have input formatted in 200 lines of row but column number is changing in every row,thats why I'm facing problem while reading – Amartya Roy Mar 31 '18 at 08:19
  • 1
    [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) solves this problem for a comma as a separator instead of a space. I'm sure you can adapt that code slightly. – Bo Persson Mar 31 '18 at 13:45

1 Answers1

0

How about something like this:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

int main()
{
    std::string line;
    while (std::getline(std::cin, line))
    {
        std::istringstream linestream(line);

        std::vector<int> all_integers_on_line(
            std::istream_iterator<int>(linestream),
            std::istream_iterator<int>());

        // Now the vector all_integers_on_line contains all integer values
        // on the current line. Do something useful with them...
    }
}

References:

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621