我在程序中遇到同样的错误,非常感谢任何帮助。
错误1错误C2784:'std :: basic_istream< _Elem,_Traits> &安培;的std ::函数getline(标准:: basic_istream< _Elem,_Traits> &安培;,的std :: basic_string的< _Elem,_Traits,_Alloc> &)':无法演绎 'std :: basic_string< _Elem,_Traits,_Alloc>的模板参数&安培;”从 'char'c:\ Users \ esmier \ Documents \ Visual Studio 2008 \ Projects \ Chapter 4 实验室\第4章lab \ source.cpp 40第4章实验室
错误2错误C2780:'std :: basic_istream< _Elem,_Traits> &安培;的std ::函数getline(标准:: basic_istream< _Elem,_Traits> &安培;,的std :: basic_string的< _Elem,_Traits,_Alloc> &,const _Elem)':预计3 参数 - 2提供了c:\ Users \ esmier \ Documents \ Visual Studio 2008 \ Projects \ Chapter 4 lab \ Chapter 4 lab \ source.cpp 40第4章实验室
在作为案例b评论的部分中也会出现错误
#include <iostream>
#include<sstream>
int main()
{
double hours;
double Package_A_price=9.95, Package_A_hours=10, Package_A_additional=2.00,Package_A_total;
double Package_B_price=14.95, Package_B_hours=20, Package_B_additional=1.00,Package_B_total;
double Package_C_price=19.95;
char package_choice, additional_hours[3];
//table output
cout<<"Please choose your package below";
cout<<"Package A:\t"<<"For $9.95 per month 10 hours of service are provided.\n";
cout<<"\t\tAdditional hours are $2.00 per hour\n\n";
cout<<"Package B:\t"<<"For $14.95 per month 20 hours of service are provided.\n";
cout<<"\t\tAdditional hours are $1.00 per hour\n\n";
cout<<"Package A:\t"<<"For $19.95 per month unlimited access is provided.\n\n\n";
cout<<"What is your package letter?\n";
cin>>package_choice;
while (package_choice!='A'||'a'||'B'||'b'||'C'||'c')
{
cout<<"You entered an incorrect option.\n";
cout<<"please reenter your choice.\n\n";
}
switch (package_choice)
{
//package A choice
case 'A||a':
cout<<"Did you have any additional hours?\n";
getline(cin, additional_hours);
if (additional_hours == 'yes')
{
cout<<"How many hours did you have?\n";
cin>>hours;
while (hours >744)
{
cout<<"You can not have over 744 hours per month!\n";
cout<<"Time is a linear constant, Please renter your amount.\n";
}
Package_A_total=9.95+(2.00*hours);
cout<<"Your Total for Package A is $"<<Package_A_total;
break;
}
//package B choice
case 'B||b':
cout<<"Did you have any additional hours?\n";
getline(cin, additional_hours);
if (additional_hours == 'yes')
{
cout<<"How many hours did you have?\n";
cin>>hours;
while (hours >744)
{
cout<<"You can not have over 744 hours per month!\n";
cout<<"Time is a linear constant, Please renter your amount.\n";
}
Package_B_total=14.95+(1.00*hours);
cout<<"Your Total for Package B is $"<<Package_B_total;
break;
}
//package C choice
case 'C||c':
cout<<"Your Total for Package C is $"<<Package_B_price;
break;
default: cout<<"You did not Enter A B or C.\n";
cout<<"Please reenter your choice.";
}
system ("pause");
return 0;
}
答案 0 :(得分:5)
additional_hours
被声明为一个字符一个包含3个字符的数组。这种形式的getline期望你使用std :: string(另一种用于C风格char*
的形式是cin的成员函数。)
还有很多其他错误,显然你不知道 char 意味着一个字符:
case 'A||a':
if (additional_hours == 'yes')
那些使用多字节字符常量。基本上它并不意味着你所期望的(它是用ASCII字符编码的单个数字或类似的东西)。
while (package_choice!='A','a','B','b','C','c')
同样,这不是你如何比较多个条件,你需要将它们与逻辑和和(和&amp;&amp;和||)结合起来。实际上,它使用逗号运算符,这意味着计算所有操作数并使用最后一个('c')作为结果。
此外,<cctype>
中的tolower或toupper功能可能会对您有所帮助。
启用编译器警告(-Wall with GCC)可能会显示所有这些错误:[警告]多字符字符常量,[警告]逗号的左侧操作数无效,[警告]案例标签值超过最大值对于类型,由于数据类型的范围有限,[Warning]比较始终为false。
编辑:既然您已将additional_hours
声明为字符数组,您应该知道3个字符不足以存储字符串“yes”,因为空终止符没有空间。你最好使用std :: string类。
答案 1 :(得分:0)
我看到你的while循环没有重置package_choice的一个漏洞:
while (package_choice!='A','a','B','b','C','c'){
cout<<"You entered an incorrect option.\n";
cout<<"please reenter your choice.\n\n";
}
你需要cin&gt;&gt;再次选择包裹。
答案 2 :(得分:0)
它似乎不喜欢你传入getline的内容。尝试将additional_hours声明为std::string
。
答案 3 :(得分:0)
使用std::cout
或std::cin
或std::cin.getline(..)
代替您正在使用的内容。
使用std::cin.get()
代替system("pause")
来固定屏幕
答案 4 :(得分:0)
试试这个:
cin.getline(additional_hour, 3)