#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    2
    Rep Power
    0

    Exclamation AP Computer Science Help - Structs


    I have these two structs

    struct BookInfoType
    {
    apstring Title;
    apstring Author;
    apstring ISBN;
    apstring PubName;
    int PubYear;


    };

    struct Book
    {
    int number;
    apvector<BookInfoType> list;

    };

    the problem comes when I try to enter the data (which is in a text file with spaces in the name such as Mr. John etc.) how do I use the getline thing in struct to ignore spaces. so far I have this:

    void getData(Book &data)
    {
    ifstream infile("data.txt", ios::in);

    infile >> data.number;

    data.list.resize(data.number);


    int i = 0;

    while((!infile.eof() && i < data.number))
    {

    getline(infile, data.list[i].Title);
    infile >> data.list[i].Title;
    infile >> data.list[i].PubYear;
    infile >> data.list[i].ISBN;


    i++;



    }


    }

    -------------

    Please help!!! I am stumped. If I glue the names together it comes fine, but I need to show the spaces in the cout.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    2
    Rep Power
    0
    heres the whole program

    PHP Code:
    #include <iostream.h>
    #include <fstream.h>
    #include "apstring.h"
    #include "apvector.h"
    #include <iomanip.h>

    struct BookInfoType
    {
        
    apstring Title;
        
    apstring Author;
        
    apstring ISBN;
        
    apstring PubName;
        
    int PubYear;
        
        
    };


    struct Book
    {
        
    int number;
        
    apvector<BookInfoType> list;
        
    };

    void getData(Book &data);
    void printData(Book &data);

    main()
    {

        
    Book data;
        

        
    getData(data);
        
    printData(data);

        return 
    0;
    }


    void getData(Book &data)
    {
        
    ifstream infile("data.txt"ios::in);

        
    infile >> data.number;

        
    data.list.resize(data.number);


    int i 0;
        
        while((!
    infile.eof() && data.number))
        {
            
            
    getline(infiledata.list[i].Title);
            
    infile >> data.list[i].Title;
            
    infile >> data.list[i].PubYear;
            
    infile >> data.list[i].ISBN;
            
            
            
    i++;
            
            

        }


    }


    void printData(Book &data)
    {


        for(
    int k 0data.numberk++)
        {

        
        
    cout << data.list[k].Title << setw(10);
        
    cout << data.list[k].PubYear << setw(10);
        
    cout << data.list[k].ISBN << setw(10);
        
    cout << endl;

        }

        
    cout << endl;


    -------------

    HELP I am screwed. I have finals and I need to know how to do this. I have searched all over the internet without finding an example of this.
    Last edited by rpgwh; May 22nd, 2003 at 11:48 PM.

IMN logo majestic logo threadwatch logo seochat tools logo