C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
c++ Reading data to a struct but it is skipping one of my getlines making the element a blank
这是我的代码,我并没有真正看到这里的问题,我敢肯定它很简单,我只需要另一对新鲜的眼睛才能找到。
#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;
}
相关文章:
- 如何获取一个数字的前3位
- 获取向量C++中第一个值和最后一个值的和
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 重载运算符*以获取对另一个类的实例的引用
- push_back通过自行创建的对象获取最后一个元素的向量
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- 互斥,Windows 10,c ++,在一个线程上获取,在另一个线程上发布
- 给定一个指向对象的超类指针,如何获取对象的大小?
- 如何获取 std::filesystem::p ath 中的最后一个目录?
- 获取指针的地址会为其分配一个值
- 如何从一个容器中获取某些元素并将其转换插入到另一个容器中?
- 有没有一种标准方法可以在C++中获取第 n 个"下一个"浮点值
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 查询事件日志.如何查询才能单独获取第一个和最后一个事件?
- CPP:如何获取字符的最后一个索引
- C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
- 如何从一个 .h 获取 int 到另一个 .h