-2
struct student{
        char name[20];
        char course[20];
        int age;
        float gpa;
    };

using namespace std;
main()
{
    ifstream infile;
    infile.open("student data.txt");
    ofstream outfile;
    outfile.open("student data 2.txt");
    if(!infile || !outfile)
    {
        cout << "error opening or creating file" << endl;
        exit(1);
    }

    student stu[2];
    infile.seekg(0, ios::beg);
    while(!infile.eof())
    {
    int i;
    for(i=0; i<=2; i++)
        {
            infile >> stu[i].name;
            infile >> stu[i].course;
            infile >> stu[i].age;
            infile >> stu[i].gpa;
        }
    }
    outfile.seekp(0, ios::beg);
    int i =0;
    while(i<3)
    {
        outfile << stu[i].name << endl;

        outfile << stu[i].course << endl;

        outfile << stu[i].age << endl;

        outfile << stu[i].gpa << endl;
        i++;
    }

    infile.close();
    outfile.close();
}

I am trying to figure out why this code is not working... its not giving any compilation error but when i run the application it stops working.it says rough.exe (app name) is not working. can anyone help :(

Mr.bunty
  • 9
  • 1
  • 2
  • `while(!infile.eof())` combined with `for(i=0; i<=2; i++)` seems a bit odd. In the `for` loop you could try to read past the end of the file before you get back to testing for the end of the file. And a quick words from the experts on using `while(!infile.eof())`: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Jun 13 '18 at 22:24
  • It won't even compile as C++ code - please edit it so that it does. –  Jun 13 '18 at 22:24
  • Prefer to use `std::string` for text, it will save you a lot of grief (such as buffer overrun issues). – Thomas Matthews Jun 13 '18 at 22:38
  • both these loops are working fine. the for loop runs for three times then exits, and then we reach the end of the file thus the while loop also exits. half the code is working fine(till getting the data into the structure). the writing part into the outfile(student data 2.txt) is not working. and its not giving any compiler errors. – Mr.bunty Jun 13 '18 at 22:39
  • 1
    IMHO, you should overload `operator>>` within your class. Keep the knowledge of the class' data members inside the class; the class knows how to read the data members. – Thomas Matthews Jun 13 '18 at 22:39
  • Prefer to use `std::vector` than arrays. Arrays are fixed in size and suffer from buffer overrun issues (and the number of valid items is not determined). – Thomas Matthews Jun 13 '18 at 22:40
  • Your problem is indices. You are creating an array of two structs `student stu[2];`. You are then filling it with three `for(i=0; i<=2; i++)`. – fcooper8472 Jun 13 '18 at 22:42
  • I should also say it would be much easier to help if you (1) pasted code that actually compiles (including `#include ...`), and (2) if you share any input data necessary to reproduce your problem (the contents of `"student data.txt"`). – fcooper8472 Jun 13 '18 at 22:48
  • its #include , and . the input data in input file is:nasir CS201 23 3 Jamil CS201 31 4 Faisal CS201 25 3.5 – Mr.bunty Jun 13 '18 at 22:55
  • fcooper8472 .... dude array count starts from 0 i think. and there are three arrays. stu[0],stu[1],stu[2]. – Mr.bunty Jun 13 '18 at 22:58
  • can anyone just edit my code and let me see please... – Mr.bunty Jun 13 '18 at 22:59
  • user8541301 thanks for pointing out the while loop.. it was not wrong but useless here. – Mr.bunty Jun 13 '18 at 23:02
  • @Mr.bunty lol, yes array count starts at 0, but if you do not know about how `char myarray[10]` gives you indexes from 0-9, please go to a c++ reference website and learn how c++ works before posting here. If you declare an array of size 2, and index starts at 0, why do you think the size magically becomes 3? "and there are three arrays". No, you declared an array of size 2, there are two objects. One at index 0, and another at index 1. – MPops Jun 13 '18 at 23:24
  • 1
    thank you so much mpops and fcooper8472... i just made a stupid mistake in array indexing. thanks guyz and sorry to fcooper8472 if my above comment offended you, :) – Mr.bunty Jun 13 '18 at 23:43

1 Answers1

0

Alright, so this is fortunately a very simple solution! The problem you are facing is related to memory management. This line is causing you all the trouble:

student stu[2];

AND

int i =0;
while(i<3)

Basically, you're initializing 2 structs (indexes 0-1) of type student, then expecting to iterate over 3 structs (from index 0-2). When attempting to read in the third struct, you can get a memory access violation error.

So I ran your code with this text in "student data.txt"

nasir CS201 23 3
Jamil CS201 31 4
Faisal CS201 25 3.5

When only two structs were initialized, "student data 2" remained blank. However, when increasing the value to student stu[3];, "student data 2.txt" gave the following output:

nasir
CS201
23
3
Jamil
CS201
31
4
Faisal
CS201
25
3.5

Here is the code I used to create a successful version of your program, with a couple of extra printf's for debugging purposes.

#include <iostream>
#include <fstream>

    struct student {
    char name[20];
    char course[20];
    int age;
    float gpa;
};

using namespace std;
int main()
{
    ifstream infile;
    infile.open("student data.txt");
    ofstream outfile;
    outfile.open("student data 2.txt");
    if (!infile || !outfile)
    {
        cout << "error opening or creating file" << endl;
        exit(1);
    }

    student stu[3];                 //NOTE THIS LINE NOW HAS 3 STRUCTS INITIALIZED
    infile.seekg(0, ios::beg);
    while (!infile.eof())
    {
        int i;
        for (i = 0; i <= 2; i++)
        {
            infile >> stu[i].name;
            infile >> stu[i].course;
            infile >> stu[i].age;
            infile >> stu[i].gpa;
            printf("%s %s %d %f\n", stu[i].name, stu[i].course, stu[i].age, stu[i].gpa);
        }
    }

    outfile.seekp(0, ios::beg);
    int i = 0;
    while (i<3)
    {
        outfile << stu[i].name << endl;
        outfile << stu[i].course << endl;
        outfile << stu[i].age << endl;
        outfile << stu[i].gpa << endl;
        printf("%s %s %d %f\n", stu[i].name, stu[i].course, stu[i].age, stu[i].gpa);
        i++;
    }

    infile.close();
    outfile.close();
    return 1;
}

Hope this helps!

armitus
  • 552
  • 3
  • 19
  • thank you so much dude... you have pointed out the right issue but this problem was already solved as you can see in the comments. anyways thanks a lot. :) – Mr.bunty Jun 14 '18 at 00:45