ok I'm having an issue with an assignment, we're creating a type of POS program, and when I try to add a book to the inventory, I need to beable to put an entire line into a 2 dimensional array but I'm failing...lol, heres my code...
Printable View
ok I'm having an issue with an assignment, we're creating a type of POS program, and when I try to add a book to the inventory, I need to beable to put an entire line into a 2 dimensional array but I'm failing...lol, heres my code...
Post your code between CODE tags. It is not convenient for anyone to download files.
sorry about that...
Code:void bookinfo() {
for (int i = 0; i <= arrSize; i++) {
system("cls");
cout << " Serendipity Booksellers"
<< " Book Information\n" << endl;
cout << "ISBN: "; // right here it displays "ISBN: TITLE: " and waits for input, but puts each word into seperate arrays
cin.getline(isbn[i], 14);
cout << "Title: ";
cin.getline(bookTitle[i], 51);
cout << "Author: ";
cin.getline(author[i], 31);
cout << "Publisher: ";
cin.getline(publisher[i], 31);
cout << "Date Added: ";
cin.getline(dateAdded[i], 11);
cout << "Quantity-On-Hand: ";
cin << qtyOnHand[i];
cout << "Wholesale Cost: ";
cin << wholesale[i];
cout << "Retail Price: ";
cin >> retail[i];
}
}
//and in the header file...
const int arrSize = 20;
char bookTitle[arrSize][51];
char isbn[arrSize][14];
char author[arrSize][31];
char publisher[arrSize][31];
char dateAdded[arrSize][11];
int qtyOnHand[arrSize];
double wholesale[arrSize];
double retail[arrSize];
Just looking at your snippet of code, I'm not sure why you are designing it this way. Each book has a title, author, publisher, etc.
So why not design a book struct/class, and then create an array or vector of Books? Why use a char array and not a string?
Code:#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Book{
string title,
author,
publisher,
isbn;
int qtyOnHand;
double wholesale,
retail;
};
int main () {
vector<Book> library;
Book temp;
while(cin){
cout << "ISBN: " << endl;
cin >> temp.isbn;
cout << "Title: " << endl;
cin >> temp.title;
cout << "Author: " << endl;
cin >> temp.author;
//etc
library.push_back(temp);
}
return 0;
}
I like that Idea, but I still get the problem that if you book title has more then 1 word, it places any aditional words in the next variable...i.e.
out " title"
in title
out "author"
in author
and it you type the title "Hello World" it'll place hello into title, and world into author...
This is not correct syntax.Code:cin << qtyOnHand[i];
You did it right several other places, but you screwed up here. Take a close look at your code----don't see what you expect to see, but instead see what's actually there.
OHH haha, I had the arrows the wrong way... or is that not what your talking about... btw, ya thats not only been fixed... but actually replaced, my teacher told me a way to do it, but to me it just seams that there would a more efficient way of doing it, and in-fact more proper way
1st off she said that I should use "cin.getline" for EVERY input throughout the entire program, then convert when needed, because the only other way is to use cin.ignore()... but to me it seems like more of a hassle to convert half the input... am I wrong?Code:void bookinfo() {
cin.ignore();
for (int i = 0; i <= arrSize; i++) {
system("cls");
cout << " Serendipity Booksellers" << endl
<< " Book Information\n" << endl;
cout << "ISBN: ";
cin.getline(isbn[i], 14);
cout << "Title: ";
cin.getline(title[i], 51);
cout << "Author: ";
cin.getline(author[i], 31);
cout << "Publisher: ";
cin.getline(publisher[i], 31);
cout << "Date Added: ";
cin.getline(dateAdded[i], 11);
cout << "Quantity-On-Hand: ";
cin.getline(temp, 11);
qtyOnHand[i] = atoi(temp);
cout << "Wholesale Cost: ";
cin.getline(temp, 11);
wholesale[i] = atof(temp);
cout << "Retail Price: ";
cin.getline(temp, 11);
retail[i] = atof(temp);
}
}
Mixing getlines with formatted input does require careful use of ignore(), yes, or else some getlines may appear to be skipped (in reality they're just reading the newline ignored by the formatted input before them).
Another problem with formatted input is that if the user enters something unexpected, say "a" when you ask for an int, it puts the stream into a fail state and you have to handle that carefully.