【发布时间】: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++ 的初学者,所以我真的不是最擅长的。
【问题讨论】:
-
发布有关构建错误的问题时,首先请将full 和complete 构建输出复制粘贴到问题中。它通常包含有助于解决问题的提示和其他信息。另外,请在显示错误的代码中添加 cmets。如果可能,请尝试将代码压缩为正确的minimal reproducible example 以向我们展示(并向我们展示该示例中的错误)。
-
std::getline- 在标题中定义<string>参见 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