【问题标题】:Error No matching function for call to 'getline'错误没有用于调用“getline”的匹配函数
【发布时间】:2022-01-02 16:58:47
【问题描述】:

我目前正在使用 XCODE 在 C++ 中编程,但我遇到了这个错误:

没有匹配的函数调用'getline'

我不知道为什么。有人可以帮忙吗?

我基本上想要做的是从用户那里获得输入,当他们在控制台中输入时还包括空格:

#include <iostream>
#include <string.h>

//Maxium numbers of exams//
#define MAX_EXAMS 30

using namespace std;

//defined type exam which is a struct composed of three elements//
typedef struct exam{
    char name[100];
    char date[100];
    int mark;
}exam_t;

//global array that can be accessed everywhere//
exam_t exams[MAX_EXAMS];
//function to move exams to the next position in the global array//
void MovePreviousExam(){
    exam_t last = exams[MAX_EXAMS - 1];
    for (int i = MAX_EXAMS; i > 0; i--) {
        exams[i] = exams[i - 1];
    }
    exams[0] = last;
}

int main() {
    //Used to check what the user wants//
    bool doesUserWantToViewExams = false;
    bool doesUserWantToEnterExam = false;
    int insertion = 0;
    //The loop is used to keep the program going until the user decides to stop it//
    do {
        cout << "You want to view exams? Press 1.\nYou want to enter an exam? Press 2.\nYou want to close program? Press 3.\n";
        cin >> insertion;
        //Checking insertion and enabling variables//
        if (insertion ==  1) {
            doesUserWantToViewExams = true;
        }else if(insertion == 2){
            doesUserWantToEnterExam = true;
        }else if(insertion == 3){
            cout << "Goodbye :)\n";
        }else{
            cout << "Unkown Value, try again.\n";
        }
        //Done with the conditions, use if else statements to execute selected task.//
        if (doesUserWantToViewExams == true && doesUserWantToEnterExam == false){
            cout << "Here are your exams:\n";
        }
        
        if (doesUserWantToEnterExam == true && doesUserWantToViewExams == false) {
            //Since the user will add more exams, this function will move the previous to the next spot in the array//
            MovePreviousExam();
            //Enter exam//
            cout << "Enter your exam's name, date and grade in this order:\n";
            
            getline(cin, exams[0].name, "\n");
            getline(cin, exams[0].date, "\n");
            getline(cin, exams[0].mark, "\n");
            
            }
    } while (insertion != 3);
    //Code block to determine what the user wants to select//

    return 0;
}

只是告诉你,我是 C++ 的初学者,所以我真的不是最擅长的。

【问题讨论】:

  • 发布有关构建错误的问题时,首先请将fullcomplete 构建输出复制粘贴到问题中。它通常包含有助于解决问题的提示和其他信息。另外,请在显示错误的代码中添加 cmets。如果可能,请尝试将代码压缩为正确的minimal reproducible example 以向我们展示(并向我们展示该示例中的错误)。
  • std::getline - 在标题中定义 &lt;string&gt; 参见 en.cppreference.com/w/cpp/string/basic_string/getline
  • 提示:std::getline(您使用的)和istream::getline 之间存在差异。我建议您继续使用std::getline,但开始对所有字符串使用C++ 类std::string
  • 一个字符的字符串,如"\n",与单个字符不同,如'\n'

标签: c++ string char whitespace cin


【解决方案1】:

getline() 从输入流 (cin) 中读取字符并将它们放入 字符串(更多信息 visit

我使用 stoi() 方法从 string

制作 int

所以改变结构中的变量类型:

typedef struct exam{
    string name;
    string date;
    int mark;
}exam_t;

以及整个程序的输入代码(我建议在你的程序中只使用 getline() )

string tmp;
getline(cin, tmp, '\n');
insertion = stoi(tmp);

getline(cin, exams[0].name, '\n');
getline(cin, exams[0].date, '\n');
getline(cin, tmp, '\n');
exams[0].mark = stoi(tmp);

现在,您必须稍微使用 True/False Flags 才能让您的程序正常运行。 祝你好运!

【讨论】:

  • 换行符'\n'std::getline的默认行终止符,不需要显式添加它作为参数。
猜你喜欢
  • 1970-01-01
  • 2017-10-16
  • 2018-10-09
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多