c ++错误,"无法推断出'std ...'的参数

0 c++

我一直在我的程序上得到同样的错误,任何帮助将不胜感激.

错误1错误C2784: '的std :: basic_istream <_Elem,_Traits>&的std ::函数getline(标准:: basic_istream <_Elem,_Traits>&,性病:: basic_string的<_Elem,_Traits,_Alloc>&)':无法推断出模板参数关于 '的std :: basic_string的<_Elem,_Traits,_Alloc>&' 从 '字符' C:\用户\ esmier \文件\的Visual Studio 2008 \项目\第4章实验室\第4章实验室\ source.cpp 40第4章实验室

错误2错误C2780: '的std :: basic_istream <_Elem,_Traits>&的std ::函数getline(标准:: basic_istream <_Elem,_Traits>&,性病:: basic_string的<_Elem,_Traits,_Alloc>&,常量_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;
Run Code Online (Sandbox Code Playgroud)

}

Unc*_*ens 5

additional_hours被声明为一个由3 个字符组成的数组.这种形式的getline期望你使用std :: string(另一种用于C风格的形式char*是cin的成员函数).

还有很多其他错误,显然你不知道char意味着一个字符:

case 'A||a':
if (additional_hours == 'yes')
Run Code Online (Sandbox Code Playgroud)

那些使用多字节字符常量.基本上它并不意味着你所期望的(它是用ASCII字符编码的单个数字或类似的东西).

while (package_choice!='A','a','B','b','C','c')
Run Code Online (Sandbox Code Playgroud)

同样,这不是你如何比较多个条件,你需要将它们与逻辑和和(和&和||)结合起来.实际上,它使用逗号运算符,这意味着计算所有操作数并使用最后一个('c')作为结果.

此外,tolower或toupper功能<cctype>可能会帮助您.

打开编译器警告(-Wall with GCC)可能会显示所有这些错误:[警告]多字符字符常量,[警告]逗号的左侧操作数无效,[警告]案例标签值超出类型的最大值,由于数据类型的范围有限,[警告]比较始终为假.

编辑:既然你已经声明additional_hours为一个字符数组,你应该知道3个字符不足以存储字符串"是",因为没有空格终止符.你最好使用std :: string类.