| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > help with arrays with ifstream in C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|