英文:
How to stop my setter function from filling all the variables when I press enter in C++?
问题
我创建了一个类,在这个类中,我定义了字符串和整数数据类型的成员变量。
当调用该函数创建一个新对象时,我输入字符串值,按<kbd>Enter</kbd>,然后所有剩下的变量都被赋予一些默认值,然后我的程序结束。
我不知道如何阻止它只是结束,并让我输入每个字符串值。
这是我的代码副本:
#include <iostream> // 用于输入和输出的头文件
#include <iomanip> // 用于使用 setw 的头文件
#include <math.h> // 用于使用 power 函数的头文件
using namespace std;
class MovieData
{
private:
string title, director;
int year, runTime;
public:
void setInfo()
{
cout << "电影标题:";
cin >> title;
cout << "导演:";
cin >> director;
cout << "年份:";
cin >> year;
cout << "播放时间:";
cin >> runTime;
}
void displayInfo ()
{
cout << "电影标题 " << title << endl;
cout << "导演 " << director << endl;
cout << "年份 " << year << endl;
cout << "播放时间 " << runTime << endl;
}
};
int main() {
MovieData movie1, movie2;
movie1.setInfo();
movie2.setInfo();
movie1.displayInfo();
movie2.displayInfo();
return 0;
}
我尝试将字符串变量定义为C字符串,即字符数组,以查看是否会有帮助,但结果是一样的。
英文:
I created a class where I am defining member variables of string and integer data types.
When the function is called for a new object, I type in the string value, press <kbd>Enter</kbd>, and all the variables left are given some default value, and my program ends.
I do not know how to stop it from just ending, and letting me type each string value.
Here is a copy of my code:
#include <iostream> //Header file needed to do inputs and outputs
#include <iomanip> //Header file needed to use setw
#include <math.h> //Header file needed to use power function
using namespace std;
class MovieData
{
private:
string title, director;
int year, runTime;
public:
void setInfo()
{
cout << "Movie title: ";
cin >> title;
cout << "Diector: ";
cin >> director;
cout << "Year: ";
cin >> year;
cout << "Runnign Time: ";
runTime;
}
void displayInfo ()
{
cout << "Movie title " << title << endl;
cout << "Director " << director << endl;
cout << "Year " << year << endl;
cout << "Run Time " << runTime << endl;
}
};
int main() {
MovieData movie1, movie2;
movie1.setInfo();
movie2.setInfo();
movie1.displayInfo();
movie2.displayInfo();
return 0;
}
I tried defining string variables as c-string, so character arrays, to see if that would help the issue, but it is the same.
答案1
得分: 1
将这一行
runTime;
改为
cin >> runTime;
英文:
Change this line
runTime;
to
cin >> runTime;
答案2
得分: 1
cout << "电影标题:";
cin >> title;
cout << "导演:";
cin >> director;
使用>>
在字符串上只读取单个单词,所以如果你输入魔戒
,那么title
将等于"魔"
,director
将等于"戒"
,等等。
如果你想要读取一整行文本,请使用std::getline()
:
cout << "电影标题:";
getline(cin, title);
cout << "导演:";
getline(cin, director);
当你开始使用getline()
时,非常重要的是阅读(并理解)这篇帖子:
https://stackoverflow.com/questions/21567291/
英文:
A couple of issues pointed out in the other comments and answers, but the main issue is this code:
cout << "Movie title: ";
cin >> title;
cout << "Director: ";
cin >> director;
Using >>
on a string reads a single word only, so if you type Lord of the Rings
, then title
will equal "Lord"
and director
will equal "of"
, etc.
If you want to read a whole line of text then use std::getline()
:
cout << "Movie title: ";
getline(cin, title);
cout << "Director: ";
getline(cin, director);
When you start using getline()
, it's very important to read (and understand) this post:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论