-4

not sure what i'm doing wrong but this is my code

int main (){
int marks [100];
int i=0;
ifstream inputfile;
ofstream outputfile;
inputfile.open("data.txt");
 if(!inputfile.is_open())
 {
    cout<< "File did not open"<< endl;
    return 0;
 }
 cout<<"Marks in File:"<<endl;

 while (marks [i] != -1)
 {
    inputfile>>marks[i];
    cout << marks[i] <<endl;
    i++;
 }
 return 0;
}

the output is messed up and returns stuff that was never in the data file to begin with

kritikaTalwar
  • 1,730
  • 1
  • 17
  • 25
J.Jass
  • 1
  • 3
  • It's unclear what you are trying to do, If you want to write some marks into a file you need to open it as `ofstream` so you can write on it. The other thing is that you never initial your `marks` array, it will contain some garbage values. – HMD Apr 10 '18 at 05:11
  • You're trying to check if a value is -1 before you're actually read it. Just step through this code line by line as if you're the computer. You should use a `while(true)` loop, and an if statement check if the inputted value is -1, then use `break` if it is. – eesiraed Apr 10 '18 at 23:28

3 Answers3

0

Here is the minimal code for reading data from a file and write it to console. Description is added as comments

#include <fstream>
#include <sstream> 
#include <string>
#include <iostream>
using namespace std;
int main()
{
    ifstream configrecord("D:\\Temp\\temp.txt");  //  opening the file named temp for reading.

    if(configrecord.good()) // check whether the file is opened properly.
    {  
        string line;
        while (getline(configrecord, line)) // reading a line from file to std::string
        {  
            cout << line; // printing the line, if you want it to store in an array, you can use the std::string::data() api to get the character pointer.
        }
        configrecord.close(); // closing the file after reading completed
    } 
}
Akhil V Suku
  • 870
  • 2
  • 13
  • 34
  • The OP is trying to read ints, and you're reading entire lines... I don't see how this has much to do with the OP's question. – eesiraed Apr 10 '18 at 23:48
0

If we translate your code to English, we get:

  1. Check if the current array element is -1, if it is, break the loop.
  2. Read the value into the current array element.
  3. Output the value.
  4. Move to the next array element and repeat.

Notice a big problem: We're checking if the value is -1 before we actually read it. We need to reverse the order of steps 1 and 2 so that we get:

  1. Read the value into the current array element.
  2. Check if the current array element is -1, if it is, break the loop.
  3. Output the value.
  4. Move to the next array element and repeat.

We can do this by using true as our loop condition and then using an if statement to check if the inputted value is -1 later in the loop, using break to break the loop if it is.

#include <fstream>
#include <iostream>
//using namespace std; is considered bad practice
int main()
{
    std::ifstream inFile("input.txt");
    int marks[100];
    //check if file is open stuff...
    for(int i = 0; true; i++)
    {
        inFile >> marks[i];
        if(marks[i] == -1) break;
        std::cout << marks[i] << '\n'; //endl flushes the buffer, unnecessary here.
    }
}
eesiraed
  • 4,626
  • 4
  • 16
  • 34
-1

Of Note: it is good practice that if you use an if statement, you also include an else statement. Also, your while loop is confusing, because it stops if it encounters negative one, so I am assuming you know that integer -1 is not in the file.

int n = -1;
if(!inputfile.is_open())
{
   cout<< "File did not open"<< endl;
}
else
{
    cout<<"Marks in File:"<< endl;

    while(!inputfile.eof()){ // .eof is bad practice, but for this instance it works.
    File >> marks[n];
    n++; // Essentially the size of the array (Number of values), keeping track of how many values there are will assist you in the output process.
    }
}

When you are done reading the file, you should close it and then use the data in the array.

inputfile.close();

Lastly, in order to output an array of data, you must use a for loop or some type of iterator to access the values stored in the array.

for(int i=0; i < n ; i++) // Output array. Where array size is less than n.
{
    cout << marks[i] << " "; // " " inputs a space in between each word.
}
Shayne
  • 19
  • 1
  • 1
  • 4
  • So if the file fails to open, you carry on with -1 items? That doesn't sound good. – Quentin Apr 10 '18 at 15:17
  • One of the first things that you do is try to input into `marks[-1]`. You also have the `eof()` problem, which doesn't work in this instance if the file has a terminating newline, a very common thing. In fact, proper input/output files should end with a newline. If you know something is bad, don't teach it to someone new. [Why `eof()` in loop condition is wrong](https://stackoverflow.com/q/5605125/9254539). – eesiraed Apr 10 '18 at 23:24
  • No, it if fails to open, it outputs "File did not open". However, if that if statement is not tripped, then it goes to the else statement. – Shayne Apr 13 '18 at 00:04