error using struct

Can someone help me with the following struct, for some reason, I am able to enter data only when my variables are of string type and system doesn't allow values for int or float types.
---------------------
//create and manage a book catalog using stream files and structs or arrays

# include <iostream>
# include <cmath>
# include <fstream>
# include <cstdlib>
# include <string>

using namespace std;

struct bookList
{
string bookId;
string aLastname;
string aFirstname;
string bookTitle;
int bookYearPub;
float bookPrice;
//string bookYearPub;
//string bookPrice;

};

void readBookInfo(bookList&);

int main()
{
bookList abook;
char ans;
cout << "Do you want to enter a book info? press Y for YES or N for NO";
cin >> ans;
if ((ans='y')||(ans='Y'))
{
readBookInfo (abook);
}
else
{
cout << "You entered that you don't wish to enter any book info" << endl;
}
system ("pause");
return 0;
}

void readBookInfo(bookList& abook)
{
char NWLN='\n';
char space='\t';
cout << "Enter ISBN number for the book and press tab: ";
getline(cin, abook.bookId,space);
cout << "Enter author's Lastname: ";
cin >> abook.aLastname;
cout << "Enter author's Firstname: ";
cin >> abook.aFirstname;
cout << "Enter book title: ";
cin >> abook.bookTitle;
cout << "Enter year the book was published: ";
cin >> abook.bookYearPub;
cout << "Enter book price: ";
cin >> abook.bookPrice;
}

Do you want to enter a book info? press Y for YES or N for NOy
Enter ISBN number for the book and press tab: 01-99-4567
Enter author's Lastname: doe
Enter author's Firstname: jim
Enter book title: the book
Enter year the book was published: Enter book price: Press any key to continue .
Try putting the following line after every cin >> variable; : cin.ignore(100,"\n");
If that doesn't work there are other methods that can be called like flush, but try that one and let us know what happens.
Last edited on
Firstly, please use code blocks to make your code more readable.

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
int main()
{
	bookList abook;
	char ans;
	cout << "Do you want to enter a book info? press Y for YES or N for NO";
	cin >> ans;

        //This statement effectively does nothing. because you use the assignment operator,
        //it sets the value of ans to 'y' when the if statement is executed.
        //to get what you're trying to do accomplished, you need to use '==', which will compare
        //ans to 'y' and see if they are the same. If they are it will yield a true.
        //so if((ans=='y')||(ans=='Y")) would be proper declaration.
	if ((ans='y')||(ans='Y'))
	{
		readBookInfo (abook);
	}
	else
	{
		cout << "You entered that you don't wish to enter any book info" << endl;
	}
	
        //this works fine. the console retrieves the float and int values fine.
        //(i didn't make any changes in your code, i just simply added a long cout statement
        //to show all the variables. There was nothing wrong with your code.
	cout << abook.bookId << endl << abook.aLastname << endl
		<< abook.aFirstname << endl << abook.bookPrice << endl << abook.bookTitle 
		<< endl << abook.bookYearPub << endl << abook.bookPrice << endl;

		system ("pause");
		return 0;
}


void readBookInfo(bookList& abook)
{
	char NWLN='\n';
	char space='\t';
	cout << "Enter ISBN number for the book and press tab: ";
	getline(cin, abook.bookId,space);
	cout << "Enter author's Lastname: ";
	cin >> abook.aLastname;
	cout << "Enter author's Firstname: ";
	cin >> abook.aFirstname;
	cout << "Enter book title: ";
	cin >> abook.bookTitle;
	cout << "Enter year the book was published: ";
	cin >> abook.bookYearPub;
	cout << "Enter book price: ";
	cin >> abook.bookPrice;
}
Last edited on
Your if statements should be if((ans=='y')||(ans=='Y')). The single '=' sign is an assignement operator not a comparison, an easy way to remember this is that all of the other comparison operators require two symbols examples: '<=' '>=' '!='.
Last edited on
while my earlier question is solved, now my display is not what I expect it to be, why is '\t' not creating the tab space (instead prints in a new line) for id, lastname & firstname??

-------------------------------------------------------------------------------------------------------------
cout << "BookID" << '\t' << "author Lastname" << " " << "author Firstname" <<
" " << "Title" << " " << "Year Published" << "Price" << endl;

cout << abook.bookId << '\t' << abook.aLastname << '\t' << abook.aFirstname <<
'\t' << abook.bookTitle << '\t' << abook.bookYearPub << '\t' << abook.bookPrice << endl;
...
.
.
.
void readBookInfo(bookList& abook)
{
char NWLN='\n';
char space='\t';
cout << "Enter ISBN number for the book and press tab: ";
getline(cin, abook.bookId, space);
cout << "Enter author's Lastname: ";
getline(cin, abook.aLastname, space);
cout << "Enter author's Firstname: ";
getline(cin, abook.aFirstname, space);
cout << "Enter book title: ";
getline(cin, abook.bookTitle,space);
cout << "Enter year the book was published: ";
cin >> abook.bookYearPub;//: cin.ignore(100,'\n');
cout << "Enter book price: ";
cin >> abook.bookPrice;
}

--------------------------------------------------------------------------------------------------
Do you want to enter a book info? press Y for YES or N for NO: y
Enter ISBN number for the book and press tab: 11-09-6768
Enter author's Lastname: jim
Enter author's Firstname: tim
Enter book title: title
Enter year the book was published: 1965
Enter book price: 33
BookID author Lastname author Firstname Title Year PublishedPrice

11-09-6768
jim
tim
title 1965 33
Press any key to continue . . .
I'm not sure what the problem is. It all outputs properly for me..
Once again, please use [ code ] and [ /code ] (without the spaces) to envelope your code to make it easier to read.

my output:

Do you want to enter a book info? press Y for YES or N for NOy
Enter ISBN number for the book and press tab: 22-22-2222
Enter author's Lastname: jim
Enter author's Firstname: tim
Enter book title: book
Enter year the book was published: 2010
Enter book price: 20.99
BookID  author Lastname author Firstname Title Year PublishedPrice

22-22-2222      jim     tim     book    2010    20.99
Press any key to continue . . .
not sure why my output is not the same as yours...as I pointed out, my output is displaying the results in new lines instead of on the same line.
Topic archived. No new replies allowed.