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>
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.
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;
}
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: '<=' '>=' '!='.
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 << 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 . . .