User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,817 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 161 | Replies: 3
Reply
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

trouble using fstream with arrays

  #1  
32 Days Ago
In the code below i can'f figure out how to get the numbers from my inFile that will be reversed then put into my outFile. I left part of the code unfinished. not sure what I am supposed to put in between >> >>. pretty lost on this one. Im new at this so go easy. Thanks


  1. [icode]
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. std::string word[5];
  8.  
  9. int main ()
  10. {
  11. //Declaring Variables
  12. ifstream inFile;
  13. ifstream inFile1;
  14. ofstream outFile;
  15.  
  16. int item[5]; // An array item of 5 components
  17. int counter;
  18. int num, num1, num2, num3, num4;
  19.  
  20.  
  21. inFile.open("numbers.txt");
  22. inFile1.open("words.txt");
  23. outFile.open("numberout.txt");
  24.  
  25.  
  26.  
  27. for (counter = 0; counter < 5; counter++)
  28. {
  29. inFile >> num >> num1 >> num2 >> num3 >> num4;
  30. }
  31.  
  32. for (counter = 5; counter >= 0; counter--)
  33. {
  34. // unfinished code
  35. outFile << << endl;
  36. }
  37. [/icode]
Last edited by greyghost86 : 32 Days Ago at 6:25 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: trouble using fstream with arrays

  #2  
32 Days Ago
You need to rethink why you are using a loop.
Ideally, if you have to do things multiple times, it suggests a loop and/or an array.

Since you need to remember five numbers, instead of writing:
int num, num1, num2, ...

you can instead just use an array:
int items[ 5 ];

I would have named it "numbers":
int numbers[ 5 ];
(But I'll still use "items" below.)

Now below, you want to read 5 numbers (and remember them). You've got a nice loop there that counts from 0 to 4, and a nice array, so let's put them together:
  1. for (counter = 0; counter < 5; counter++)
  2. {
  3. cin >> items[ counter ];
  4. }

You've already got a nice loop there that counts from 5 to 0. You'll have to fix that five to the proper number to start at. (Remember, arrays are index from 0 to N-1, where N is the number of elements in the array.) And I think you should know by now what goes inside the loop.

Hope this helps.
Last edited by Duoas : 32 Days Ago at 6:32 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

Re: trouble using fstream with arrays

  #3  
32 Days Ago
Thank you for the help. What you said worked perfectly. I just can't believe I didn't see it before. Thanks again for help.

Grey
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: trouble using fstream with arrays

  #4  
32 Days Ago
Heh, don't feel bad. Nothing is obvious until you see it. You'll do fine.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC