使用 char* 参数将用户输入传递给函数
我的代码出现以下错误。
无法将参数 1 从“std::string”转换为“char *”
未找到采用“const char *”类型右侧操作数的运算符
有人能告诉我在下面的 menuSelection 函数中我做错了什么吗?重点是,如果用户从菜单选项中选择 1,程序将获取用户输入(lastName、firstName、courseName、letterGrade)并将其传递给 addRecord 函数,但我认为我的数据类型已关闭。
抱歉发了这么长的帖子。 menuSelection 函数位于 main() 和 head() 函数的右下角。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void GradeBook::addRecord(char* lastName, char* firstName, char* className, char letterGrade)
{
StudentRecord* newRecord = new StudentRecord(lastName, firstName, className, letterGrade);
if (_headRecord == NULL) {
_headRecord = newRecord;
} else {
int compare = _headRecord->compareTo(lastName, firstName);
if (compare < 0) {
// we insert before the first element
newRecord->setNext(_headRecord);
_headRecord = newRecord;
} else {
_headRecord->insert(newRecord);
}
}
}
void menuSelection(int selection, GradeBook& gradeBook) {
string firstName;
string lastName;
string courseName;
char letterGrade;
switch(selection) {
case 1: cin >> lastName.c_str() >> firstName.c_str() >> courseName.c_str() >> letterGrade;
gradeBook.addRecord(lastName, firstName, courseName, letterGrade);
case 4: gradeBook.read("students.txt");
gradeBook.displayAllRecords();
break;
case 5: gradeBook.write("students1.txt");
break;
default: cout << "Enter a valid choice.\n";
}
if (selection != 5) {
displayMenu();
cin >> selection;
menuSelection(selection, gradeBook);
}
}
void header() {
cout << "STUDENT GRADEBOOK AND GPA CALCULATOR\n";
cout << "=====================================\n";
}
void main()
{
GradeBook gradeBook;
int userSelection;
header();
displayMenu();
cin >> userSelection; // get the user input for menu selection
menuSelection(userSelection, gradeBook);
char pause;
std::cin >> pause;
}
I get the following errors with my code.
cannot convert parameter 1 from 'std::string' to 'char *
no operator found which takes a right-hand operand of type 'const char *
Can someone tell me what I'm doing wrong in the menuSelection function below? The point of it is if a user selects 1 from the menu options, the program gets user input (lastName, firstName, courseName, letterGrade) and passes it to the addRecord function but I think my data types are off.
Sorry for the long post. The menuSelection function is towards the bottom right about the main() and head() functions.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void GradeBook::addRecord(char* lastName, char* firstName, char* className, char letterGrade)
{
StudentRecord* newRecord = new StudentRecord(lastName, firstName, className, letterGrade);
if (_headRecord == NULL) {
_headRecord = newRecord;
} else {
int compare = _headRecord->compareTo(lastName, firstName);
if (compare < 0) {
// we insert before the first element
newRecord->setNext(_headRecord);
_headRecord = newRecord;
} else {
_headRecord->insert(newRecord);
}
}
}
void menuSelection(int selection, GradeBook& gradeBook) {
string firstName;
string lastName;
string courseName;
char letterGrade;
switch(selection) {
case 1: cin >> lastName.c_str() >> firstName.c_str() >> courseName.c_str() >> letterGrade;
gradeBook.addRecord(lastName, firstName, courseName, letterGrade);
case 4: gradeBook.read("students.txt");
gradeBook.displayAllRecords();
break;
case 5: gradeBook.write("students1.txt");
break;
default: cout << "Enter a valid choice.\n";
}
if (selection != 5) {
displayMenu();
cin >> selection;
menuSelection(selection, gradeBook);
}
}
void header() {
cout << "STUDENT GRADEBOOK AND GPA CALCULATOR\n";
cout << "=====================================\n";
}
void main()
{
GradeBook gradeBook;
int userSelection;
header();
displayMenu();
cin >> userSelection; // get the user input for menu selection
menuSelection(userSelection, gradeBook);
char pause;
std::cin >> pause;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为你真的想这么做。
c_str()
是将常规 C++ std::string 转换为旧的 C 风格“字符串”,以便与需要这种野兽的函数一起使用。如果您尝试输入常规 C++ 字符串,则应将其更改为:
事实上,除非您使用 C++ 的 C 库部分(或明确需要 C“字符串”的第三方函数),否则您永远不应该需要使用
char *
样式的字符串。拥抱该语言并尽可能使用std::string
。I don't think you really want to do that.
c_str()
is to turn a regular C++ std::string into an old C-style "string" for use with functions that expect such a beast.If you're trying to input a regular C++ string, you should change that to:
In fact, unless you're using the C library part of C++ (or third-party functions which explicitly requires C "strings"), you should never need to use a
char *
style of string. Embrace the language and usestd::string
as much as possible.首先,将所有未修改的参数更改为
const char*
。其次,将所有这些参数更改为 const std::string& 类型。第三,如果前面的步骤由于某些设计原因失败,请使用 std::string::c_str() ,它返回一个一直存在到函数结束的const char*
来电。First off, change all the parameters you're not modifying to
const char*
. Secondly, change all those parameters to be of typeconst std::string&
. Thirdly, if the previous steps fail for some design reason, usestd::string::c_str()
which returns aconst char*
that lives untill the end of the function calls.看看下面的网址:
http://www.bogotobogo.com/cplusplus/string.html
在名为C++ 字符串。
您基本上应该将字符串转换为 char*。
Have a look at the following url:
http://www.bogotobogo.com/cplusplus/string.html
in the section called C++ string.
You should basically convert your string to char*.