473,566 Members | 3,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion with homework .. infile

21 New Member
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
15 4852
gasfusion
59 New Member
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
jjh
21 New Member
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
jjh
21 New Member
this is a test
Sep 25 '06 #4
gasfusion
59 New Member
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
jjh
21 New Member
string tempTitle;
infile.getline( tempTitle,MAXLE NGTH);
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\visua l studio 2005\projects\h w 4\hw 4.cpp(34) : error C2664: 'std::basic_ist ream<_Elem,_Tra its> &std::basic_ist ream<_Elem,_Tra its>::getline(_ Elem *,std::streamsi ze)' : cannot convert parameter 1 from 'std::string' to 'char *'
with
[
_Elem=char,
_Traits=std::ch ar_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,MAXLE NGTH);
//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
9,065 Recognized Expert Moderator Expert
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
jjh
21 New Member
#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,MAXLE NGTH);
//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
9,065 Recognized Expert Moderator Expert
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
gasfusion
59 New Member
duh, you're incrementing the struct array, not the .title lol. I'm such an idiot.
Sep 25 '06 #10

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

Similar topics

14
9729
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\" into table addresses"; $result = mysql_query($SQL) or die(mysql_error()); The file is located in the same directory as my .PHP file. How do I...
7
12075
by: las | last post by:
I'm having a wee problem with the get method, here is my code : ifstream infile; char x; infile.open("temp.txt"); if( !infile.good() ) { cout << "Error opening file" << endl; system("PAUSE"); exit(1); }
0
6673
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to be able to place the PHP file on the server, and I guess you probably can't do that either. Talk about catch 22... The only other way I can think...
0
1640
by: Steffen | last post by:
Hello, I´m using mysql 3.23.58 and I want to enable the local-infile option. The manual on dev.mysql.com says the following to that: ..... If you use LOAD DATA LOCAL in Perl scripts or other programs that read the group from option files, you can add the local-infile=1 option to that group. However, to keep this from causing problems...
1
16055
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason is for the sake of security. What does that mean ?
2
4291
by: Jason3231 | last post by:
well i've found and pieced together a script (mainly found; i'm very new to perl) to scan a group of cisco switches that i have and print information out in a text file. everything seems to work pretty well except when a particular cmd doesn't apply to one of the switches listed in my infile. the script seems to get stuck in these instances and...
10
3633
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and...
2
4805
by: jjh | last post by:
Trying to input a file say file.txt The first line I am trying to input is: Principles of Biochemistry Trying to put "Principles of Biochemistry" into a char array book.title with a max of MAXBOOKS Why wont it work when I use... infile.getline(book.title, MAXBOOKS);
15
2986
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy Tubbs
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.