0

I can't figure out how to fix this code.

void getInfo(Author a[], int size)
{
    for(int x = 0; x < size; x++)
        {
            cout << "Enter the author's name: ";
            getline(cin, a[x].name);

            if(a[x].name == "NONE")
                break; //Breaks the loop if the author has no further books
            else

            for(int y = 0; y < size; y++)
                {
                    cout << "Enter title " << y+1 << " :";
                    getline(cin, a[x].book[y].title);

                    if(a[x].book[y].title == "NONE")
                        break;
                    else
                        cout << "Enter price " << y+1 << " :  $"; 
                        cin >> a[x].book[y].price;
                }
            cout << endl;
        }
}

This is how it is compiling:

Get user's input:
Enter the author's name: John Smith
Enter title 1: How to Tie a Shoe
Enter price 1: 20
Enter title 2: Enter price 2: 

Could someone please help me understand how to fix this loop. When I try a getline(cin, a[x].book[y].price); ,visual studio tells me that it is overloaded.

4

1 回答 1

0

要解决您的问题,请使用cin.ignore();after cin >>

std::getline接受 astd::string作为其第二个参数。

它认为您通过getline(cin, a[x].book[y].price);.

于 2013-04-22T09:29:12.813 回答