mardi 28 juillet 2015

Is there any particular way to use getline for string data type in C++ for reading in a string of say 540 characters?

I have to read in this string : efBZw WH EDC EVOh qjfzJ oXkXL QUHXWMl kXRSIyGHb TxQkBWhPI yPTJEZ KyYfFQR ZQvYBPUmZ bkuKbRBWW mjJ WRgIBFNM ojGP XbkJNUhXF SeZVBZ SooFP XXE VIebym gOXP pDXRQ

But using getline(cin>>ws, pisong[i]); in my below program, I can't take the above string as input. Any help?

    #include <iostream>
    #include <string>
    #include <ctype.h>
    #include <deque>

    using namespace std;

    int isPiSong(string str,deque<int> numbers){
        int noOfwords = 0, noOfletters = 0, ctr = 0;
        int flag = 1;
        for(std::string::iterator it = str.begin(); it != str.end(); ++it) {

            if(*it != ' ' && flag){
                if(isalpha(*it))
                noOfletters++;
            }
            else{
                if(noOfletters != numbers[ctr++])
                    flag = 0;
                noOfletters = 0;
            }

        }
        return flag;
    }

    int main()
    {
        deque<int> numbers;
              // 31415926535897932384626433833
        numbers.push_back(3);
        numbers.push_back(1);
        numbers.push_back(4);
        numbers.push_back(1);
        numbers.push_back(5);
        numbers.push_back(9);
        numbers.push_back(2);
        numbers.push_back(6);
        numbers.push_back(5);
        numbers.push_back(3);
        numbers.push_back(5);
        numbers.push_back(8);
        numbers.push_back(9);
        numbers.push_back(7);
        numbers.push_back(9);
        numbers.push_back(3);
        numbers.push_back(2);
        numbers.push_back(3);
        numbers.push_back(8);
        numbers.push_back(4);
        numbers.push_back(6);
        numbers.push_back(2);
        numbers.push_back(6);
        numbers.push_back(4);
        numbers.push_back(3);
        numbers.push_back(3);
        numbers.push_back(8);
        numbers.push_back(3);
        numbers.push_back(3);

        int T, flag;
        cin>>T;

        string* pisong = new string[T];

        for(int i = 0; i < T; i++){
           getline(cin>>ws, pisong[i]); //cin >> ws gets rid of leading whitespace 
                                       //first so that getline won't think that it's already                         //reached the end of the line
                                      //It doesn't seem to take the above input string
        }

        for(int i = 0; i < T; i++){
            flag = isPiSong(pisong[i],numbers);
            if(flag){
                cout<<"It's a pi song."<<endl;
            }else{
                cout<<"It's not a pi song."<<endl;
            }
        }
        delete [] pisong;
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire