-1

what's wrong if I use the get line function only once in the character modifier function the compiler will ignore it unless I call the function twice why cant I use it only once? I tried using other ways, it worked but I wanna understand this one I'm now just writing random things so the add more details error messages go away

#include <iostream>
#include<string>
using namespace std;
class egybest
{
    string link,m;
    char sys, type, restart;
    int s = 1, e = 1, date;
public:
    string charmodifier()
    {
       //here
        getline(cin, m);
        getline(cin, m);
        for (int x = 0; x <= m.size(); x++)
        {
            if (m[x] == ' ')
                m[x] = '-';
        }
        return m;
    }
    ~egybest()
    {
        system("cls");
        cout << "do you want to restart the program? y:n;" << endl;
        cin >> restart;
        system("cls");
        if (restart == 'y' || restart == 'Y')
            egybest();
        else if (restart == 'n' || restart == 'N')
        {
            system("exit");
        }
    }
    egybest()
    {
        cout << "do you want to watch a movie or a series? 1:2;" << endl;
        cin >> type;
        system("cls");
        if (type == '1')
            linkmovie();
        else if (type == '2')
            series();
        else
            cout << "wrong input!" << endl;
    }
    void linkmovie()
    {
        cout << "enter the name of the movie:" << endl;
        charmodifier();
        cout << "enter the release date: " << endl;
        cin >> date;
        link = "start https://cape.egybest.cool/movie/" + m + "-" + to_string(date);
        cout << endl;
        system(link.c_str());
    }
    void series()
    {
        cout << "do you want it to open links for a particular season, particular episode or all seasons? s:e:a;"
            << endl;
        cin >> sys;
        system("cls");
        if (sys == 'S' || sys == 's')
            linkseason();
        else if (sys == 'A' || sys == 'a')
            linkall();
        else if (sys == 'E' || sys == 'e')
            linkepisode();
        else
            cout << "wrong input!" << endl;
    }
    void linkall()
    {
        cout << "season No." << endl;
        cin >> s;
        cout << "episode No." << endl;
        cin >> e;
        cout << "enter the name of the show:" << endl;
        charmodifier();
        for (int j = 1; j <= s; j++)
        {
            for (int i = 1; i <= e; i++)
            {
                link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(j) + "-ep-" + to_string(i);
                system(link.c_str());
            }
        }
        cout << endl;
    }
    void linkepisode()
    {
        cout << "season No." << endl;
        cin >> s;
        cout << "episode No." << endl;
        cin >> e;
        cout << "enter the name of the show:" << endl;
        charmodifier();
        link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(s) + "-ep-" + to_string(e);
        cout << endl;
        system(link.c_str());
    }
    void linkseason()
    {
        cout << "season No." << endl;
        cin >> s;
        cout << "episodes No." << endl;
        cin >> e;
        cout << "enter the name of the show:" << endl;
        charmodifier();
        for (int i = 1; i <= e; i++)
        {
            link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(s) + "-ep-" + to_string(i);
            cout << endl;
            system(link.c_str());
        }
    }
};
int main()
{
    egybest egy;
    return 0;
}```

  • [why does std::getline fails after using operator>> twice?](https://stackoverflow.com/a/15662808) and [Why must type getline(cin, string) twice?](https://stackoverflow.com/a/12723648) and many others.... – 001 May 22 '22 at 20:26

1 Answers1

1

The problem is that after entering an integer or a character as for example

cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
charmodifier();
//...

the input buffer contains the new line character '\n' that corresponds to the pressed Enter key.

So the following call of getline reads an empty string until the new line character is encountered.

In such a case before calling getline you need to remove the new line character from the input buffer like for example

#include <limits>

//...

cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
charmodifier();
//... 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335