C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old March 29th, 2004, 12:40 AM
drtychimp drtychimp is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 drtychimp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
help with arrays with ifstream in C++

I'm pretty new to C++ and I am stuck.

I need to take numbers from an input file, and place them in arrays.

An example of the file could be:
123698 2 5 7 8 9 . . .
(repeating with the same pattern)


this is the code that I have so far, I know that it is horribly wrong. I need to store the 6 digit number in the stock array, and the 5 following single digit numbers in the cred array.


struct cust
{
int stock[20];
int cred[20];
};

int main ()
{
int i, u;
ifstream cust_file;

for (i = 0, u = 0; i < 20; i++)
{
do
{
cust test;
test.stock;
cust_file.open("data.txt");
cust_file >> test.stock[i] >> test.cred[u++] >> test.cred[u++] >> test.cred[u++] >> test.cred[u++] >> test.cred[u++];
}
while ( .............
}

I cant figure out how to make the do while loop stop at the end either.

Thank you for all of your help, and if you don't help, then thank you anyway.

drtychimp

Reply With Quote
  #2  
Old April 12th, 2004, 02:20 AM
fisch fisch is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 fisch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
First you need to get the cust_file.open("data.txt"); statement outside your for and do loops. It'll just keep reopening the file, so each time you loop you're reading in the first line again and again. And if you don't close the cust_file before trying to reopen the program might die.

I don't know why you have the for loop at all. You should never use a for loop if your going to be incrementing the index yourself(in this case i and u) I'm presuming you have this b/c you have 20 sets of numbers to read in. Won't work b/c you're reading 5 numbers into the cred array each time anyhow so cred[] would have to be of size 100.

If you want to know when you hit the end of the file use the cust_file.eof() statement. This will be true if you're at the end of the file. otherwise it'll be false.

Example

while (!cust_file.eof()){
}//will loop until the the end of file.

Work on that for a while, i'll check back in a couple days if you need some more help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > help with arrays with ifstream in C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway