I'm having input (cin) issues in my program?

closed account (3UMLy60M)
Hello all,

This program starts out with authors and book titles initialized to NONE and prices initialized to $0. The user is then asked to plug in all of the information and the program outputs it back at the end.

The problem I'm having is that I can't input multiple words as book titles without the program skipping to the final output. I'm thinking I need to use getline but I'm really not sure. Can anybody help me out with this problem? This is my code:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

//Structure to hold the book information for an author
struct BookInfo
{
	string title;
	double price;

        BookInfo() {
                title = "NONE";
                price = 0;
        }
};

//Structure to hold the author and their book information
struct Author
{
	string author;
	BookInfo books[3];

        Author() { 
                author = "NONE"; 
        }
};

//Function Prototypes
void showInfo(Author a[], int size);
void getInfo(Author a[], int size);

int main()
{
	int index = 3;
	Author catalogue[3];

	cout << "Here is the data after initialization:\n";
	showInfo(catalogue,index); //Displays the data in the author structure called catalogue

	cout << "\nGet user's input:";
	getInfo(catalogue,index); //Allows the user to enter data in the author structure called catalogue

	cout << "\nHere is the data after the user's input:";
	showInfo(catalogue,index);  //Displays the data in the author structure called catalogue

	system("pause");
	return 0;
}

void showInfo(Author a[], int size) //Uses nested for loops to display the information contained in the catalogue
{
	cout << fixed << showpoint << setprecision(2); //Sets the formatting used for doubles

	for(int x = 0; x < size; x++)
		{
			cout << "The author: " << a[x].author << endl;
		
			for(int y = 0; y < size; y++)
				{
					cout << "\tThe title: " << a[x].books[y].title << ", the price: $" << a[x].books[y].price << endl;
			}
	}
}

void getInfo(Author a[], int size) //Uses nested for loops to read data into the catalogue
{
	
	for(int x = 0; x < size; x++)
		{
			cout << "\nEnter the author's name: ";
			cin >> a[x].author;

			if(a[x].author == "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 << ": ";
					cin >> a[x].books[y].title;
					if(a[x].books[y].title == "NONE")
						break;
					else
					cout << "Enter price " << y+1 << ": "; 
					cin >>a[x].books[y].price;
			}
	}
}
Last edited on
First of all a lot of those functions should be parts of those structs, I don't mean to get off topic here but if you're going to learn it then learn it right. To address your question yes you can use getline, you can also change the delimiter on the >> operator or you could recursively grab the data until the buffer is empty and use sprintf or += to consolidate it into the one variable. I'm sure there are a half dozen other ways that I didn't mention. What problem are you having with getline?
Last edited on
closed account (3UMLy60M)
I'm still pretty new to C++ so eventually I'll make less and less mistakes. The problem I'm having is that when it comes to entering the author's name or a book title, I can only enter one word otherwise it won't work.
Again, my questions is what problem are you having with getline? You didn't use it in the code you posted above so I can't tell where you might be running into trouble. The default delimiter is a carriage return and it doesn't ignore whitespace so it should fit for your needs rather well. Here's a link to the reference if that's what you needed: http://www.cplusplus.com/reference/istream/basic_istream/getline/
closed account (3UMLy60M)
The problem is in this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void getInfo(Author a[], int size) //Uses nested for loops to read data into the catalogue
{
	
	for(int x = 0; x < size; x++)
		{
			cout << "\nEnter the author's name: ";
			getline(cin, a[x].author);

			if(a[x].author == "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].books[y].title);
					if(a[x].books[y].title == "NONE")
						break;
					else
					cout << "Enter price " << y+1 << ": "; 
					cin >>a[x].books[y].price;
			}
	}
}

It causes this to happen:

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:
Your problem looks to be with the extraction operator acting on a[x].books[y].price, Line 21 in your last post. It might be leaving a carriage return in the buffer. If your not doing any math in the project you then price can be a char and you can use getline there as well with no trouble.
Topic archived. No new replies allowed.