我正在上课的C ++项目有一些问题。我不断收到一条错误消息,指出“没有重载函数实例”。我做了一些谷歌搜索,似乎每个人都说此错误是由于将字符串传递给cin.get()函数引起的,但是我将此函数与char而不是字符串一起使用。 Visual Studio说该错误位于:“ cin.get(nameFull);”但我已将nameFull定义为一个字符,而不是字符串。任何帮助将不胜感激,谢谢您的时间。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int MONTHS = 12;
const int RETRO_MONTHS = 6;
char nameFull[30]; // INPUT - Employee's full name
float salaryCurrent; // INPUT - Current annual salary
float percentIncrease; // INPUT - Percent increase due
float salaryNew; // OUTPUT - New salary after increase
float salaryMonthly; // OUTPUT - New monthly salary
float retroactivePay; // OUTPUT - Retroactive pay due employee
int count; // CALC - Counter for loop
for (int count = 0; count < 3; count++) {
cout << "What is your name?" << endl;
cin.get(nameFull);
cout << "What is your current salary?" << endl;
cin >> salaryCurrent;
cout << "What is your pay increase?" << endl;
cin >> percentIncrease;
salaryNew = salaryCurrent + (salaryCurrent * percentIncrease);
salaryMonthly = salaryNew / MONTHS;
retroactivePay = (salaryNew - salaryCurrent) * RETRO_MONTHS;
cout << nameFull << "'s SALARY INFORMATION" << endl;
cout << "New Salary"
<< setw(20) << "Monthly Salary"
<< setw(20) << "Retroactive Pay" << endl;
cout << setprecision(2) << fixed << setw(10) << salaryNew
<< setw(20) << salaryMonthly
<< setw(20) << retroactivePay << endl;
cout << "<Press enter to continue>" << endl << endl;
cin.get();
}
return 0;
}
答案 0 :(得分:2)
nameFull
是char
(更具体地说是char[30]
)的数组,该数组衰减为指向字符(char*
)的指针。没有std::istream::get
的重载,它只接受一个指向字符的指针,但有一个 可以接受一个指针+您想读入的缓冲区大小。
因此,您所需要做的就是传递一个附加参数:
cin.get(nameFull, 30);