这是我的课程和我的课程。我没有得到任何错误,但是,输出文件只显示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;
};
答案 0 :(得分:0)
每次获得用户输入时,您都在创建一个新的TVShow。
while(choice!='4')
{ //new option = what comes back from menu()
choice=menu();
TVShow T;
if(choice!='4')
将TVShow T;
移到while循环之外。