这是我的代码,我并没有真正看到这里的问题,我敢肯定它很简单,我只需要另一对新鲜的眼睛才能找到。
#include <iostream>
#include <string>
using namespace std;
//Struct
struct MovieData
{
string title;
string director;
int year;
int runTime;
double productionCost;
double fYRevenue;
};
//Prototypes
MovieData getMovieData();
void printMovieData(MovieData );
int main()
{
//Variables
MovieData m1, m2;
cout << "Enter data for movie 1:";
m1 = getMovieData(); //call get data
cout << "Enter data for movie 2:";
m2 = getMovieData(); // Call get data
printMovieData(m1); //Call print
printMovieData(m2); //Call print
return 0;
}
/*
Passes a struct of type movie data and
collects data needed to print out
later on
*/
MovieData getMovieData()
{
MovieData m;
char line;
cout << "nnWhat is the title of the movie?: ";
getline(cin, m.title);
cout << "nWho was the director of the movie?: ";
getline(cin, m.director);
cout << "nWhat year was the movie made?: ";
cin >> m.year;
cout << "nHow long is the movie in minutes?: ";
cin >> m.runTime;
cout << "nHow much did it cost to produce this movie?: ";
cin >> m.productionCost;
cout << "nHow much did the movie make in its first year?: ";
cin >> m.fYRevenue;
cout << "n";
return m;
}
/*
Passes a variable of type movie data
then prints out the information inside
*/
void printMovieData(MovieData m)
{
cout << "nnThe movie data for " << m.title << " is as follows.n";
cout << "Title: " << m.title;
cout << "nDirector: " << m.director;
cout << "nYear Made: " << m.year;
cout << "nRunning Time: " << m.runTime;
cout << "nProduction cost: " << m.productionCost;
cout << "nRevenue: " << m.fYRevenue ;
}
,如果你们能找到问题,我非常感谢它弄乱了一会儿,我在这里无法理解这个问题。第二次GetMoviedata被称为第一个标题是空白行。但是,当我把它们当作cin&lt;&lt;M.Title;当我输入两个部分时,它跳到了整个功能,与董事名称Carl Simons一样,洪水也会跳过整个过程。
不要忘记清除您的缓冲区!
它不是完美的,但它是您与...
一起工作的开始MovieData getMovieData()
{
MovieData m;
char line;
//you have to clear out your buffer here
cin.ignore();
cin.sync();
cout << "nnWhat is the title of the movie?: ";
getline(cin, m.title);
cout << "nWho was the director of the movie?: ";
getline(cin, m.director);
cout << "nWhat year was the movie made?: ";
cin >> m.year;
cout << "nHow long is the movie in minutes?: ";
cin >> m.runTime;
cout << "nHow much did it cost to produce this movie?: ";
cin >> m.productionCost;
cout << "nHow much did the movie make in its first year?: ";
cin >> m.fYRevenue;
cout << "n";
return m;
}
您在getMovieData
函数中混合了cin >>
和getline
的使用。这导致了令人困惑的行为,因为cin >>
不会从输入缓冲区消耗尾随的新线(当您阅读第一部电影的收入时),而getline
并不希望看到缓冲区中的尾随newline(当您阅读标题时下一部电影)。
将getline
用于所有用户输入,此问题将消失。如果您需要读取一个数字,请与getline
一起使用CC_7或其他数字转换功能。例如:
std::string input;
getline(cin, input);
m.fYRevenue = stoi(input);
如果您阅读了几个整数,则可以将上述行包裹到其自己的函数中。
尝试:
MovieData getMovieData()
{
MovieData m;
char line;
cout << "nnWhat is the title of the movie?: ";
cin>> m.title;
cout << "nWho was the director of the movie?: ";
cin >> m.director;
cout << "nWhat year was the movie made?: ";
cin >> m.year;
cout << "nHow long is the movie in minutes?: ";
cin >> m.runTime;
cout << "nHow much did it cost to produce this movie?: ";
cin >> m.productionCost;
cout << "nHow much did the movie make in its first year?: ";
cin >> m.fYRevenue;
cout << "n";
return m;
}