getline не работает с выводом fstream - PullRequest
0 голосов
/ 26 апреля 2018

Вот моя программа и мой класс. Я не получаю никаких ошибок, однако, выходной файл показывает только значения int, а не строковые значения. Я подозреваю, что это может быть из-за того, как я запрашиваю ввод с помощью getline, но я не могу понять, что я делаю неправильно. Помогите! Это пример выходных данных, когда ввод «Гарри Поттер», «что-то хорошее», 4, 1111:

"

4 1111 «

Программа

#include <iostream>
#include <fstream>
#include <string>
#include"TVShow.h"

using namespace std;

char menu();

int main()
{
    string show;
    string des;
    int num;
    int time;

    char choice = '7';

    while(choice!='4')
    { 
        //new option = what comes back from menu()
        choice=menu();
        TVShow T;

        if(choice!='4')
        {
            switch(choice)
            {
                case '1': {cout << "What is the name of the TVShow? " << endl; cin.ignore();
                           getline(cin,show); T.setTitle(show);} break;
                case '2': {cout << "What is the description of the show?" << endl; cin.ignore();
                           getline(cin,des); T.setDescription(des);
                           cout << "How many episodes are there? " << endl;
                           cin >> num;T.setEpisodes(num);
                           cout << "What year was the show made? " << endl;
                           cin >> time; T.setYear(time);} break;
                case '3': {char option = '0';
                           while(option!='5')
                           {
                               cout << "Which would you like to modify? 1) TVShow\n 2) description\n"
                                    << "3) number of episodes\n 4) year made\n 5) main menu" << endl;
                               cin >> option;

                               switch(option)
                               {
                                   case '1': { cout << "What is the new TVShow name? " << endl; cin.ignore();
                                               getline(cin,show); T.setTitle(show);} break;
                                   case '2': { cout << "What is the new description? " << endl; cin.ignore();
                                               getline(cin,des); T.setDescription(des);} break;
                                   case '3': { cout << "How many episodes? " << endl;
                                               cin >> num;T.setEpisodes(num);} break;
                                   case '4': { cout << "What is the new year? " << endl;
                                               cin >> time; T.setYear(time);} break;
                                   default: {cout << "That is not a valid option. Please try again. " << endl;} break;
                               }//EOS
                           }//EOW
                        } break; //EOCase 3
                default:  {cout << "That is not a valid option. Please try again. " << endl;} break;
            }//EOS
        }//EOI

        T.display();

    }//EOW

    return 0;
}//EOM

char menu()
{
    char choice;

    cout << "Please pick from the following selections: \n"
         << "1. Create a TVShow \n"
         << "2. Input the desription, episodes, and year of the TVShow. \n"
         << "3. Modify TVShow information \n"
         << "4. Quit \n" << endl;
    cin >> choice;
    return choice;
}

Заголовок

#include <string>
#include <fstream>

using namespace std;

class TVShow
{
public:
TVShow(){}

string getTitle(){return title;}
void setTitle(string t){title=t;}

string getDescription(){return description;}
void setDescription(string d){description=d;}

int getEpisodes(){return episodes;}
void setEpisodes(int e){episodes=e;}

int getYear(){return year;}
void setYear(int y){year=y;}

void display()
{
    ofstream outFile;
    outFile.open("TVShow.txt");
    outFile << title << endl
         << description << endl
         << episodes << endl
         << year << endl;
    outFile.close();
}
private:
string title;
string description;
int episodes;
int year;
};

1 Ответ

0 голосов
/ 26 апреля 2018

Вы создаете новое ТВ-шоу каждый раз, когда получаете пользовательский ввод.

 while(choice!='4')
    { //new option = what comes back from menu()
        choice=menu();
        TVShow T;
            if(choice!='4')

Переместить TVShow T; вне цикла while.

...