Question

exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...

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".

Homework Answers

Answer #1

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;
}
===================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8,...
Consider the following array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8, 9, 4 }; What are the contents of the array a after the following loops complete? (show how you arrive at the answer) a) for (int i = 1; i < 10; i++) { a[i] = a[i - 1]; b) for (int i = 9; i > 0; i--) { a[i] = a[i - 1]; }
Stem Leaf 2 6 3 0, 7 4 0, 2, 6 5 1, 5, 7, 9,...
Stem Leaf 2 6 3 0, 7 4 0, 2, 6 5 1, 5, 7, 9, 9 6 0, 2, 5, 8 1) standard deviation: 2)Find Five-number summary of the data set 3). Draw a box plot that represents the data set
Let p = (8, 10, 3, 11, 4, 0, 5, 1, 6, 2, 7, 9) and...
Let p = (8, 10, 3, 11, 4, 0, 5, 1, 6, 2, 7, 9) and let q = (2, 4, 9, 5, 10, 6, 11, 7, 0, 8, 1, 3) be tone rows. Verify that p = Tk(R(I(q))) for some k, and find this value of k.
Consider the following definitions int[ ][ ] numbers = {{1, 2, 3},                                &
Consider the following definitions int[ ][ ] numbers = {{1, 2, 3},                                 {4, 5, 6} }; The following code below produces for (int row = numbers.length - 1; row >= 0; row -- ) { for (int col = 0; col < numbers[0].length; col ++) { System.out.print (numbers [row][col]); } }
Consider the following definitions int[ ][ ] numbers = {{1, 2, 3},                                &
Consider the following definitions int[ ][ ] numbers = {{1, 2, 3},                                 {4, 5, 6} }; The following code below produces for (int row = numbers.length - 1; row >= 0; row -- ) { for (int col = 0; col < numbers[0].length; col ++) { System.out.print (numbers [row][col]); } }
Let A = {0, 3, 6, 9, 12}, B = {−2, 0, 2, 4, 6, 8,...
Let A = {0, 3, 6, 9, 12}, B = {−2, 0, 2, 4, 6, 8, 10, 12}, and C = {4, 5, 6, 7, 8, 9, 10}. Determine the following sets: i. (A ∩ B) − C ii. (A − B) ⋃ (B − C)
price quantity marginal revenue $8 0 $7 1 $6 2 $5 3 $4 4 $3 5...
price quantity marginal revenue $8 0 $7 1 $6 2 $5 3 $4 4 $3 5 $2 6 $1 7 a monopolist firm sees the following demand, find the marginal revenue. Here's the same firm cost schedule find the marginal cost and based off of the data in these 2 find the monopolist equilibrium quantity and price. quantity total variable cost marginal cost 1 $5 2 $9 3 $12 4 $14 5 $18 6 $24 7 $32 8 $42
Consider the following data: 6, 8, 2, 3, 4, 4, 5, 5, 9, 6, 6, 7,...
Consider the following data: 6, 8, 2, 3, 4, 4, 5, 5, 9, 6, 6, 7, 7 ,7, 7, 8, 8, 8, 8, 9, 9. What type of distribution is this?
Consider the sample space of single-digit integers S= 0, 1, 2, 3, 4, 5, 6, 7,...
Consider the sample space of single-digit integers S= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 And let A= 2, 4, 6, 8 B= 1, 3, 5, 7, 9 C= 0, 1, 2, 3 A) Are events A and B mutually exclusive? B) Are events A and C mutually exclusive?
=Using the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 only once,...
=Using the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 only once, find all the possible 3 digit number plus another 3 digit number to equal a 4 digit number. (No repetition of numbers is allowed.) One example is 589+437=1026. We are asked to find ALL the possibilities. I know it has to do with combinations, but I'm not quite sure if I'm using it the proper way.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT