1

I need to make a program that reads n numbers in a row. For example, the user first puts in a list of 2 numbers like this:

P n

I managed to read those with scanf but now I need to read the n following numbers, these numbers are given in a row as follows.

1 6 3 99 ... n times These numbers must be read all at once (or give the impression of).

I already tried using

while(getline(cin,str)){
// do something with str
} 

As explained in this thread but I need the actions inside the while loop to stop when I hit the intro key. With my current implementation they don't stop, it just keeps waiting for more lines to read.

In summary: First, user must be able to input two numbers (P and n)(done!) then hit enter and start typing a list of n numbers (not done!).

Here is my code.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main(void){
    int P,n;
    scanf("%d %d",&P,&n);
    string val;
    int score[P];
    int i;
    for(i=0;i<P;i++){
        score[i]=0;
    }
    while(getline(cin,val)){
        printf("Val=%s\n",val.c_str());
        stringstream stream(val);
        int local;
        while(stream >> local){
            score[local]=score[local]+1;
            printf("%d-%d\n",local,score[local]);
        }

    }
    for(i=0;i<P;i++){
        printf("%d-%d\n",i,score[i]);
    }
    return 0;
}
Community
  • 1
  • 1
David Merinos
  • 1,195
  • 1
  • 14
  • 36
  • do you need to exit the reading mode explicitly? – victor Sep 29 '15 at 04:58
  • I don't understand what you're trying to say but I only need to print the score array in the end. – David Merinos Sep 29 '15 at 04:59
  • The user types P n and hits enter. Then he types 1 2 3 4 5 and press enter. At this point, do you want the user to be able to type other numbers or do you want the reading to stop? – victor Sep 29 '15 at 05:00
  • @victor I want the reading to stop – David Merinos Sep 29 '15 at 05:07
  • I'm sorry, I'm trying to understand your question but I'm still confused. while(getline(...)) will keep on trying yo read lines until you break out of the loop. If you want to read only one line, why the while loop? – victor Sep 29 '15 at 05:11

3 Answers3

1

Use scanf() inside the n times loop instead of getline().

for(int i = 0; i < n; i ++){
   scanf("%d",&variable);
}
Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37
1

Not sure I understood your question, but this code will read 2 numbers, then a line and then finish.

using namespace std;

int main(){
    int P,n;

    cin >> P;
    cin >> n;

    int *score = new int[P];

    for(int i=0;i<P;i++){
        score[i]=0;
    }

    int num;

    string val;

    cin.ignore();
    getline(cin, val);

    istringstream stream(val);

    while (stream >> num) {

        printf("Val = %d\n", num);

        score[num]=score[num]+1; // potential segmentation fault here in your code

        printf("%d-%d\n",num,score[num]);
    }

    delete [] score;

    return 0;

}

The fault would occur because you are assuming that the number on the line is smaller than P, which is the size of the array. However, the following input would cause error:

1 2 5

victor
  • 1,532
  • 1
  • 13
  • 32
0

This question is almost a classic. The classic question has this code:

cin >> n;
getline(cin, s);

and then the author is puzzled why s is empty. Your variation does the same, although it also uses C stdio function to make matters more confusing. The problem is that the first call is a field-based input, which will read a single value n and leave any other input in the buffer! If the user entered 42 and hit enter, the remaining input is the newline. The second getline() call then reads an empty string and discards the newline.

For interaction with the user, only use getline() and then try to parse each line. Using stringstreams or sscanf(), since you seem familiar with it, are both valid options. However, if you only want to read the input an not really interact, David Weston's suggestion is also a good one and probably the easiest one, too. However, since you're using C++, I'd suggest using cin >> variable instead.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55