Passing user input to function with char* parameters
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 'c开发者_Python百科onst 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;
}
cin >> lastName.c_str() >> firstName.c_str() >> courseName.c_str()
>> letterGrade;
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:
cin >> lastName >> firstName >> courseName >> letterGrade;
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 use std::string
as much as possible.
First off, change all the parameters you're not modifying to const char*
. Secondly, change all those parameters to be of type const std::string&
. Thirdly, if the previous steps fail for some design reason, use std::string::c_str()
which returns a const char*
that lives untill the end of the function calls.
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*.
精彩评论