0
votes

I need to input n, an integer, which is the number of sentences i need to input afterwards which are separated by enter, each one being on a new line.

Then i need to output the sentence which had the most words.

When i run the following program, i can only read n and then the program terminates.

EDIT 1: I put a cin.ignore() after the cin >> n and it fixed the issue, but now when i output text2 it only shows the first word of the sentence it should show.

EDIT 2: i copied text into aux before splitting the sentence and then in the if() i copied aux into text2.

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

//ifstream fin("X.TXT");
/**
ifstream gin("Y.TXT");
**/
//ofstream fout("BAC.TXT");

int main()
{
    int n, i, cnt1=0, cnt2=0;
    char text[256], text2[256]="";
    char *p;
    cin >> n;
    cin.ignore;
    for(i=0; i<n; i++)
    {
        cnt1 = 0;
        cin.get(text, 256);
        p = strtok(text, " ");
        while(p != NULL)
        {
            cnt1++;
            p = strtok(NULL, " ");
        }
        if(cnt1 > cnt2)
        {
            cnt2 = cnt1;
            strcpy(text2, text);
        }
    }
    cout << text2;
    return 0;
}