By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,263 Members | 2,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,263 IT Pros & Developers. It's quick & easy.

Confusion with homework .. infile

P: 21
So with infile I have this so far:

#define MAXBOOKS 9
ifstream infile("library.txt");
struct Book
{
title[MAXBOOKS]
}
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
char tempTitle[MAXBOOKS];
infile.getline(book.title, MAXLENGTH);
}
}

I also have the main function, but my question is with the getline. My problem is how do i get the book.title to go into the title[0].
if i try to declare it by setting book.title[0] = book.title, I come up with an error obviously.. but how do i get the infile into book.title[0]????
Sep 24 '06 #1
Share this Question
Share on Google+
15 Replies


P: 59
Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.     Title[MAXBOOKS];
  4. };
  5.  
  6. int main() 
  7. {
  8.     Book myBooks;
  9.     int i = 0;
  10.  
  11.     ifstream infile("library.txt");
  12.  
  13.     for (i=0;i<MAXBOOKS; i++)
  14.         infile.getline(myBooks.Title[i], MAXLENGTH);
  15.     return 0;
  16. }
  17.  
Sep 24 '06 #2

P: 21
jjh
Will that work if i am bringing in a a name of a book ex. "Principles of Chemistry", or will i have to use a 2d array ????
Sep 24 '06 #3

P: 21
jjh
this is a test
Sep 25 '06 #4

P: 59
you don't have to use 2D arrays.
You can pass a number to that array and it'll give you the book title back.
What i don't understand is why you're using structs at all.
Unless of course you're planning to save more stuff in that struct.
Sep 25 '06 #5

P: 21
jjh
string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
If I put the infile in for a string and then assigning it into a char array it doesn't work.

I get this error

c:\documents and settings\owner\my documents\visual studio 2005\projects\hw 4\hw 4.cpp(34) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

CAN YOU THINK OF SOMETHING

this is what I have so far.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
#define MAXBOOKS 9
#define MAXKEYWORDS 3
#define MAXLENGTH 50
struct Book
{
char title[MAXBOOKS];
char keyWord[MAXKEYWORDS][MAXLENGTH];
char position;
};
int fillArray()
{
char title;
char keyword1;
char keyword2;
char keyword3;
Book theBook;
ifstream infile("library.txt");
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
// infile >> theBook.title[i];

string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
//strcpy(theBook.title[i], tempTitle);
cout << theBook.title[i];

for(int j = 0; j < MAXKEYWORDS; j++)
{

}
}
}
return 0;
}

plus a main function
Sep 25 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
This
Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.     Title[MAXBOOKS];
  4. };
  5.  
is not a proper definition because Title has no type. It probably ought to be something like

Expand|Select|Wrap|Line Numbers
  1. #define MAXBOOKS              500  // Set these values yourself
  2. #define MAXTITLE                   50
  3.  
  4. struct Book
  5. {
  6.     char Title[MAXTITLE];
  7. };
  8.  
in which case main should be

Expand|Select|Wrap|Line Numbers
  1. int main() 
  2. {
  3.     Book myBooks[MAXBOOKS];
  4.     int i = 0;
  5.  
  6.     ifstream infile("library.txt");
  7.  
  8.     for (i=0;i<MAXBOOKS; i++)
  9.         infile.getline(myBooks[i].Title, MAXTITLE);
  10.     return 0;
  11. }
  12.  
Sep 25 '06 #7

P: 21
jjh
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
#define MAXBOOKS 9
#define MAXKEYWORDS 3
#define MAXLENGTH 50
struct Book
{
char title[MAXBOOKS];
char keyWord[MAXKEYWORDS][MAXLENGTH];
char position;
};
int fillArray()
{
char title;
char keyword1;
char keyword2;
char keyword3;
Book theBook;
ifstream infile("library.txt");
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
// infile >> theBook.title[i];

string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
//strcpy(theBook.title[i], tempTitle);
cout << theBook.title[i];

for(int j = 0; j < MAXKEYWORDS; j++)
{

}
}
}
return 0;
}

This is what I have but it doesn't work can you inform me there is something wrong with me infile but I dont know what.
Sep 25 '06 #8

Banfa
Expert Mod 5K+
P: 8,916
This is the error

Expand|Select|Wrap|Line Numbers
  1. string tempTitle;
  2. infile.getline(tempTitle,MAXLENGTH);
  3.  
getline does not work on type string, it works on array of char.

If you are going to have more than 1 book you need an array of Book structures (or a linked list but let's not complicate things) and finally you have declared you book local to FillArray so as soon as you exit the data will be gone

Expand|Select|Wrap|Line Numbers
  1. Book Books[MAXBOOKS];
  2.  
  3. int fillArray()
  4. {
  5.     ifstream infile("library.txt");
  6.  
  7.     for(int i = 0; i < MAXBOOKS; i++)
  8.     {
  9.         infile.getline(Books[i].title, sizeof Books[i].title);
  10.         cout << Books[i].title;
  11.  
  12.         for(int j = 0; j < MAXKEYWORDS; j++)
  13.         {
  14.             infile.getline(Books[i].keyWord[j], sizeof Books[i].keyWord[j]);
  15.         }
  16.     }
  17.  
  18.     return 0;
  19. }
  20.  
Sep 25 '06 #9

P: 59
duh, you're incrementing the struct array, not the .title lol. I'm such an idiot.
Sep 25 '06 #10

P: 21
jjh
I tried this :
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < MAXBOOKS; i++)
  2.         {
  3.  
  4.             string tempTitle;
  5.             infile.getline(theBook[i].title ,MAXLENGTH);
  6.             cout << theBook[i].title;
  7.                  }
  8.  
and then i got the error

c:\documents and settings\owner\my documents\visual studio 2005\projects\hw 4\hw 4.cpp(33) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

The message i am trying to bring it is "Principles of chem"
Sep 25 '06 #11

Banfa
Expert Mod 5K+
P: 8,916
Not if you are using the structure

Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.   char title[MAXTITLE];
  4.   char keyWord[MAXKEYWORDS][MAXLENGTH];
  5.   char position;
  6. };
  7.  
  8. struct Book theBook[MAXBOOKS];
  9.  
you aren't so I suspect you aren't doing that or you haven't change the code correctly.
Sep 25 '06 #12

P: 21
jjh
Ok i got that alright and thank you, but one other thing...
The file I am wanting to bring in, is read like this...

Title
keyword
keyword
$

my problem now is how do I make the key words is a single array... Is there a way to call theBook[i].keyword[j] and have j as a inside for loop???
Sep 25 '06 #13

Banfa
Expert Mod 5K+
P: 8,916
See this post

http://www.thescripts.com/forum/post2107947-9.html

that I have already made to this thread.
Sep 25 '06 #14

P: 21
jjh
Oh I am sorry i didn't even see it. Thank you so much.
Sep 25 '06 #15

P: 7
Can I ask a question on this thread?

What if you have no idea how big the infile is? How do you set MAXBOOKS and MAXLENGTH?
Dec 8 '06 #16

Post your reply

Sign in to post your reply or Sign up for a free account.