简体   繁体   中英

c++ error, “could not deduce argument for 'std…”

i keep getting the same errors on my program, any help would be greatly appreciated.

Error 1 error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'char' c:\\Users\\esmier\\Documents\\Visual Studio 2008\\Projects\\Chapter 4 lab\\Chapter 4 lab\\source.cpp 40 Chapter 4 lab

Error 2 error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided c:\\Users\\esmier\\Documents\\Visual Studio 2008\\Projects\\Chapter 4 lab\\Chapter 4 lab\\source.cpp 40 Chapter 4 lab

there errors also happen in the section commented as case 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;

}

additional_hours is declared as a character an array of 3 characters. This form of getline expects you to use std::string (another form that is for C-style char* is the member function of cin).

There are numerous other errors, apparently from you not knowing that char means a single character:

case 'A||a':
if (additional_hours == 'yes')

Those use multibyte character constants. Basically it just doesn't mean what you expect at all (it is a single number encoded in ASCII characters or something like that).

while (package_choice!='A','a','B','b','C','c')

Again this is not how you compare multiple conditions, which you need to combine with logical and and or (&& and ||). As it is, it uses the comma operator which means evaluate all operands and use the last ('c') as the result.

Also, the tolower or toupper function in <cctype> might help you.

Turning on compiler warnings (-Wall with GCC) might reveal all these errors: [Warning] multi-character character constant, [Warning] left-hand operand of comma has no effect, [Warning] case label value exceeds maximum value for type, [Warning] comparison is always false due to limited range of data type.

Edit: Now that you've declared additional_hours as an array of chars, you should be aware that 3 characters is not enough to store the string "yes" because there is no room for the null-terminator. You would be best off using std::string class.

I see one flaw already your while loop doesnt reset the 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";
}

You need to cin >> the package choice again.

It doesn't appear that it likes what you're passing in to getline. Try declaring additional_hours as an std::string .

Use std::cout or std::cin or std::cin.getline(..) instead of what you are using.

Use std::cin.get() instead of system("pause") to hold your screen

尝试这个:

cin.getline(additional_hour, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM