pulling my hair out over errors - help!

Hello,

I'm working on a program for school and I'm getting these errors and I'm stuck. Line 23 gives an error saying "no matching function for call to ‘std::basic_istream<char>::getline(double [6], const int&)’| "

I've read a bunch of posts where people were getting the same error, and my understanding is that the compiler thinks I'm calling a function that isn't there. Is that right? My question is why it thinks that. I have a similar line above that causes no error and I can't see any difference between the two. Main is my only function. Can anyone please enlighten me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

const int MAX_NAME_LEN = 20;
const int MAX_PRICE_LEN = 6;

int main()
{
    char productName[MAX_NAME_LEN];
    double productPrice[MAX_PRICE_LEN];
    double totalPrice;
    char answer;

    do
    {
        cout << "Enter an item name: ";
        cin.getline(productName, MAX_NAME_LEN);

        cout << "Enter a price: ";
        cin.getline(productPrice, MAX_PRICE_LEN);
        totalPrice = productPrice + totalPrice;

        cout << "You entered " << productName << " for $" << productPrice << endl;
        cout << "Would you like to continue? Press y to continue or q to quit and get your total: ";
        cin >> answer;

        if(answer == 'q')
            break
        else
            continue
    }
    while (answer != 'q');

    cout << "Your total price is $" << totalPrice;

}


Here are the errors:

1
2
3
4
5
6
7
8
9
10
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
/home/school/CS162/shoppingPrice2.cpp||In function ‘int main()’:|
/home/school/CS162/shoppingPrice2.cpp|23|error: no matching function for call to ‘std::basic_istream<char>::getline(double [6], const int&)’|
/usr/include/c++/7/istream|647|note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]|
/usr/include/c++/7/istream|647|note:   candidate expects 3 arguments, 2 provided|
/usr/include/c++/7/istream|427|note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]|
/usr/include/c++/7/istream|427|note:   no known conversion for argument 1 from ‘double [6]’ to ‘std::basic_istream<char>::char_type* {aka char*}’|
/home/school/CS162/shoppingPrice2.cpp|24|error: invalid operands of types ‘double [6]’ and ‘double’ to binary ‘operator+’|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


Thanks in advance!
newGrl
This runs and look at your bloopers then decide if it's doing what you want. FWIW if this is your own work it's pretty good :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

const int MAX_NAME_LEN = 20;
const int MAX_PRICE_LEN = 6;

int main()
{
    char productName[MAX_NAME_LEN];
    double productPrice; //[MAX_PRICE_LEN];
    double totalPrice;
    char answer;

    do
    {
        cout << "Enter an item name: ";
        cin.getline(productName, MAX_NAME_LEN);

        cout << "Enter a price: ";
        //cin.getline(productPrice, MAX_PRICE_LEN);
        
        cin >> productPrice;
        totalPrice = productPrice + totalPrice;

        cout << "You entered " << productName << " for $" << productPrice << endl;
        cout << "Would you like to continue? Press y to continue or q to quit and get your total: ";
        cin >> answer;

        if(answer == 'q')
            break; // <--
        else
            continue; // <--
    }
    while (answer != 'q');

    cout << "Your total price is $" << totalPrice;

}
Last edited on
Thanks! Programming is not coming easily for me and I'm pretty hard on myself about it, so it's nice to hear that my work isn't terrible.

I realized on my own that I didn't need to use getline for the price and that price didn't need to be an array. That simplifies things a lot. Were these the cause of all of the errors? I guess yes, because it runs now! Thanks for the help!

newGrl
You should also consider using std::string instead of the C-string.

1
2
3
4
5
6
7
8
9
10
11
...
#include <string>
...

int main()
{
    std::string productName;
...
        cout << "Enter an item name: ";
        getline(std::cin, productName);
...

@OP, strings are better but stay cool because most C++ courses include C-style strings at the start to introduce arrays which are extremely important in setting the groundwork for future.

Good luck :)
Registered users can post here. Sign in or register to post.