GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Nov-2008, 14:33
abbyadnez abbyadnez is offline
New Member
 
Join Date: Nov 2008
Posts: 1
abbyadnez is on a distinguished road

Reading from a text fie into an array


I am supposed to create a program that reads 7 numbers from a text file and then out puts then it should calculate the average of the 7 numbers, find the largest number and the smallest and outputs the result into the screen and a new text file

I am having problems reading from the text file. Here is what I've come up with so far but it's not even compiling. There are errors. Can someone help me out please?

CPP / C++ / C Code:
# include <iostream>
# include <fstream>
# include <cstdlib>
using namespace std;

void getArrayValues (int scores []);
int main (){
    int scores [7];
    getArrayValues(scores);
    return 0;
}
void getArrayValues (int scores []) {
     
     // Declare Stream and Open Text File
     ifstream inStream;
     inStream.open("biola.txt");
     
     if (inStream.fail()) {
     cerr << "Can't open file!\n";
     exit(1);
     }
 
     else {
        while (!inStream.eof()) {
        inStream >> scores[7];
           for (int j = 0; j < scores[7]; j++) {
                inStream.close();
               
     }

Thanks
Last edited by admin : 14-Nov-2008 at 00:48. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 13-Nov-2008, 14:47
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help with readig from a text fie into an array c++


Quote:
Originally Posted by abbyadnez
CPP / C++ / C Code:
     else {
        while (!inStream.eof()) {
        inStream >> scores[7];
           for (int j = 0; j < scores[7]; j++) {
                inStream.close();
               
     }
Given the following:
Quote:
CPP / C++ / C Code:
inStream.open("biola.txt");
...you are implying that the data is found in a text file. Therefore, the following statement:
Quote:
CPP / C++ / C Code:
        inStream >> scores[7];
...will not work for a number of reasons:
  • You cannot read all values into the array at once.
  • scores[7] indicates the eighth element in the array (remember, indexing is 0-based...) which is outside the bounds of the array declared.
Because the data is text, you will need to read each line of the file as text & convert into integer values. Begin with studying the following tutorial:

http://www.cplusplus.com/doc/tutorial/files.html

...& begin by getting the string data from the file. Once you accomplished this, respond back with specific questions.
  #3  
Old 27-Nov-2008, 06:57
Allan Allan is offline
New Member
 
Join Date: Nov 2008
Posts: 14
Allan is on a distinguished road

Re: Reading from a text fie into an array


CPP / C++ / C Code:
// it will read an integer from the text file until eof, 
int count = 0;
while( !instream.eof() )
{
  inStream >> scores[count];
  count++;
}
assume that all are integers
hope this little code will help
  #4  
Old 27-Nov-2008, 10:09
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Reading from a text fie into an array


Quote:
Originally Posted by Allan
CPP / C++ / C Code:
// it will read an integer from the text file until eof, 
int count = 0;
while( !instream.eof() )
{
  inStream >> scores[count];
  count++;
}
This assumes that the number of values in the file matches the size of the array. I would suggest adding a check which will ensure that the writing past the end of the array never occurs. Likewise if too few values are specified in the file, not all array elements will be assigned values. This may or may not have dire consequences.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 60 20-Aug-2009 06:04
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
How to read unknown total of int data from text file to a 2-dim array in C or C++? ladyscarlet99 C Programming Language 2 09-Sep-2005 15:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.