exampleInput.txt
1 2 3
0 2 3 4
0 1 3 5
0 1 2 6
1 5 6 8
2 4 6 7
3 4 5 9 10
5 8 9
4 7 9
6 7 8
6
How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
Also, it will be executed like so, "./a.out < sampleInput.txt".
Thanks for the question, here is how to count the numbers in each line with command line argument
================================================================
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main(int argc, char* argv[]){
if(argc!=2){
cout<<"No command line
arguments provided. Program will terminate ...";
return 1;
}
ifstream infile(argv[1]);
if(!infile.is_open() || infile.bad()){
cout<<"Error: could not open
file: "<<argv[1]<<endl;
cout<<"Program will terminate
now ...\n";
return 1;
}
string line;
int lineNum = 1;
while(getline(infile,line,'\n')){
stringstream ss(line);
cout<<"Line
>\""<<line<<lineNum++;
int numCount = 0;
int num;
while(ss>>num){
numCount++;
}
cout<<"\" contains
"<<numCount<<" number(s).\n";
}
infile.close();
return 0;
}
===================================================================
Get Answers For Free
Most questions answered within 1 hours.