samedi 31 mars 2018

reading number and character one after another until end of input in c++

suppose I have input like

1 2 3 4 5 6
2 3 4 5 6 7
3 7 5 8 9 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;
}

Aucun commentaire:

Enregistrer un commentaire