【问题标题】:C++ topic: Using "getline" in conjunction with "If If else"C++ 主题:将“getline”与“If If else”结合使用
【发布时间】:2015-02-19 06:23:29
【问题描述】:

我有一个关于使用 getline 和 If/Else If 语句的问题。

目前,我的代码如下所示:

    int yourAge = 13;

        cout << "What's your age dude? ";

           if(yourAge < 21) {

        cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;

         } else if(yourAge >= 21) {

         cout << "Cool!" << yourAge << "? You are good to go.  Don't drink    and drive!" << endl;

      }
        return 0;

  } 

这很好用。 yourAge 是 13,结果是它说“你太年轻了不能喝酒”。

但是,我想在代码中引入 getline 函数,以便结果取决于用户的输入。我试图更改代码如下:

      int yourAge;

      cout << "What's your age dude? ";

       getline(cin, yourAge);

        if(yourAge < 21) {

       cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;

} else if(yourAge >= 21) {

    cout << "Cool!" << yourAge << "? You are good to go.  Don't drink and drive!" << endl;

}

return 0;    
   }

这反过来又会在我尝试编译时导致此错误消息:

  "ctut.cpp: In function ‘int main()’:
   ctut.cpp:25:25: error: no matching function for call to        ‘getline(std::istream&, int&)’
 getline(cin, yourAge);
                     ^
   ctut.cpp:25:25: note: candidates are:
   In file included from /usr/include/wchar.h:90:0,
             from /usr/local/include/c++/4.9.2/cwchar:44,
             from /usr/local/include/c++/4.9.2/bits/postypes.h:40,
             from /usr/local/include/c++/4.9.2/iosfwd:40,
             from /usr/local/include/c++/4.9.2/ios:38,
             from /usr/local/include/c++/4.9.2/ostream:38,
             from /usr/local/include/c++/4.9.2/iostream:39,
             from ctut.cpp:1:
  /usr/include/stdio.h:442:9: note: ssize_t getline(char**, size_t*, FILE*)
  ssize_t getline(char ** __restrict, size_t * __restrict, FILE *    __restrict)      __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);....." 

这只是开始,会持续很长一段时间。

任何关于如何修改的帮助将不胜感激。我想获取用户年龄的用户输入,并根据输入在屏幕上吐出正确的消息。

谢谢!

【问题讨论】:

  • getline(cin, yourAge); 不会工作,除非yourAgestd::string。关键是您要解析每行数据吗?
  • 对不起 WhozCraig,我不认为我正在尝试解析每行数据。我只是一个初学者,正在尝试编写一些东西,让我能够以用户身份输入数据,并根据我的输入输出不同的文本。

标签: c++ if-statement getline


【解决方案1】:

引用cppreference.com

getline 从输入流中读取字符并将它们放入字符串中。

因此,getline() 仅在您的变量 yourAgestd::string 时才有效。 对于阅读intstd::cin 绰绰有余。

【讨论】:

    【解决方案2】:

    如果出于某种原因必须使用“getline()”,则必须将字符串转换为 int:

    int yourAge;
    string age;
    
    cout << "What's your age dude? ";
    
    getline(cin, age);
    yourAge = stoi(age);
    
    if(yourAge < 21) {
    
        cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;
    
    } else if(yourAge >= 21) {
    
        cout << "Cool!" << yourAge << "? You are good to go.  Don't drink and drive!" << endl;
    
    }
    return 0;
    

    【讨论】:

    • 感谢war1oc - 这工作!只是一个初学者,所以我相信我需要我能得到的所有帮助!
    • 不客气 :) 你应该使用“cin >> yourAge”而不是“getline()”
    【解决方案3】:

    getline() 用于读取字符串而不是整数。你最好在这个程序中使用cin&gt;&gt;yourAge;。阅读这些链接以了解有关 getline link1 link2 的更多信息

    【讨论】:

      【解决方案4】:

      您可以使用getline() 读取整数,但不建议这样做。最好使用cin 读取整数。

      如果您想使用getline() 读取整数,请不要忘记使用stoi() 将它们转换为整数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-05
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多