如何在按下 Enter 键时阻止我的 C++ setter 函数填充所有变量?

huangapple go评论65阅读模式
英文:

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;
}

如何在按下 Enter 键时阻止我的 C++ setter 函数填充所有变量?

我尝试将字符串变量定义为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 &lt;iostream&gt;     //Header file needed to do inputs and outputs
#include &lt;iomanip&gt;		//Header file needed to use setw
#include &lt;math.h&gt;       //Header file needed to use power function
using namespace std;

class MovieData
{
    private:
        string title, director;
        int year, runTime;

    public:
        void setInfo()
        {  
            cout &lt;&lt; &quot;Movie title: &quot;;
            cin &gt;&gt; title;
            cout &lt;&lt; &quot;Diector: &quot;;
            cin &gt;&gt; director;
            cout &lt;&lt; &quot;Year: &quot;;
            cin &gt;&gt; year;
            cout &lt;&lt; &quot;Runnign Time: &quot;;
            runTime;
        }
        void displayInfo ()
        {
            cout &lt;&lt; &quot;Movie title &quot; &lt;&lt; title &lt;&lt; endl;
            cout &lt;&lt; &quot;Director &quot; &lt;&lt; director &lt;&lt; endl;
            cout &lt;&lt; &quot;Year &quot; &lt;&lt; year &lt;&lt; endl;
            cout &lt;&lt; &quot;Run Time &quot; &lt;&lt; runTime &lt;&lt; endl;
        }
};

int main() {
    MovieData movie1, movie2;

    movie1.setInfo();
    movie2.setInfo();

    movie1.displayInfo();
    movie2.displayInfo();

    return 0;
}

如何在按下 Enter 键时阻止我的 C++ setter 函数填充所有变量?

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 &gt;&gt; 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 &lt;&lt; &quot;Movie title: &quot;;
cin &gt;&gt; title;
cout &lt;&lt; &quot;Director: &quot;;
cin &gt;&gt; director;

Using &gt;&gt; on a string reads a single word only, so if you type Lord of the Rings, then title will equal &quot;Lord&quot; and director will equal &quot;of&quot;, etc.

If you want to read a whole line of text then use std::getline():

cout &lt;&lt; &quot;Movie title: &quot;;
getline(cin, title);
cout &lt;&lt; &quot;Director: &quot;;
getline(cin, director);

When you start using getline(), it's very important to read (and understand) this post:

https://stackoverflow.com/questions/21567291/

huangapple
  • 本文由 发表于 2023年2月16日 13:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定