CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: getline

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    getline

    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...
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2010
    Posts
    66

    Re: getline

    Post your code between CODE tags. It is not convenient for anyone to download files.

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Re: getline

    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];

  4. #4
    Join Date
    Sep 2010
    Posts
    66

    Re: getline

    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;
    }

  5. #5
    Join Date
    Oct 2010
    Posts
    5

    Re: getline

    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...

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: getline

    Code:
    cin << qtyOnHand[i];
    This is not correct syntax.

  7. #7
    Join Date
    Oct 2010
    Posts
    5

    Re: getline

    Quote Originally Posted by Lindley View Post
    Code:
    cin << qtyOnHand[i];
    This is not correct syntax.
    I'm quite new to c++ but isn't that how you would store integers into an array?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: getline

    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.

  9. #9
    Join Date
    Oct 2010
    Posts
    5

    Question Re: getline

    Quote Originally Posted by Lindley View Post
    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
    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);
    	}
    }
    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?

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: getline

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured