-1

The point of this programme is to write to a file the first, middle, last name and test score of a student and if their score is between 40 and 50 points it has to output their names. However, it is skipping the names of some students or outputting them twice. example: input: n=2

Jon Jones Jameson 41

George Peterson Harrison 51

output: none

input: n=2 Jon Jones Jameson 49

George Peterson Harrison 43

output: George Peterson Harrison George Peterson Harrison

and that's my code:

#include <iostream>
#include <fstream>
using namespace std;
struct Student{
char first_name[20];
char second_name[20];
char last_name[20];
double score;
};
int main(){
Student students;
fstream file;
file.open("c:/students.txt",ios::in);
int n,i;
cout<<"n:";
cin>>n;
for(i=0;i<n;i++ ){
    cout<<"First name: ";
    cin>>students.first_name;
    cout<<"Second name: ";
    cin>>students.second_name;
    cout<<"Last name: ";
    cin>>students.last_name;
    cout<<"Score: ";
    cin>>students.sr_uspeh;
    file<<students.first_name<<" ";
    file<<students.second_name<<" ";
    file<<students.last_name<<" ";
    file<<students.score<<" ";

}
file.seekg(0);
for(int i=0;i<n;i++){
    file>>students.first_name;
    file>>students.second_name;
    file>>students.last_name;
    file>>students.score;
    if(students.score>=40 && students.sr_uspeh<=50){
        cout<<students.first_name<<" ";
        cout<<students.second_name<<" ";
        cout<<students.last_name<<endl;
    }

}
file.close();
return 0;
}
  • 1
    Appropriate formatting would make reading the code easier. And [about using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Jan 15 '20 at 17:52
  • 1
    `Student students;` maybe you want to rename it to `Student student;` since you only hold 1 student at a time (the most recent one that was read). – drescherjm Jan 15 '20 at 17:56
  • `sr_uspeh` is not defined at all in `Student`. Got that deleted by accident or did you forget to rename some of the references to? So far, it won't compile, you should keep an eye on creating an [mre] (earlier name was minimal, *complete* and verifiable example, and *completeness* is certainly not met by your code...). – Aconcagua Jan 15 '20 at 18:00
  • aconcagua, i changed the names up a bit because it was in bulgarian. it should be score – 66_fukin_6 Jan 15 '20 at 18:02
  • Well, good idea to write all code in English only even for yourself, you won't have that issue then... Apart from, don't you think that mixture of languages is somehow – errm... *unaesthetic*? – Aconcagua Jan 15 '20 at 18:03
  • `file.open("c:/students.txt",ios::in);` – you open a file for reading, but then *write* to it? If you want to read *and* write, you should open it with `ios::in | ios::out`! Not fully sure, but I think operating on a file stream in a mode it wasn't opened for is undefined behaviour. – Aconcagua Jan 15 '20 at 18:06
  • Still doesn't work sadly. – 66_fukin_6 Jan 15 '20 at 18:09

1 Answers1

0

I changed the names, ios::in | ios::out and some formats, then the code works as wanted

#include <iostream>
#include <fstream>
using namespace std;
struct Student{
    char first_name[20];
    char second_name[20];
    char last_name[20];
    double score;
};
int main(){

    Student student;
    fstream file;
    file.open("fileName.txt",ios::in|ios::out);//You will input and output
    int n;
    cout<<"n:";
    cin>>n;

    for(int i=0;i<n;i++ ){
        cout<<"\nFirst name: ";
        cin>>student.first_name;
        cout<<"\nSecond name: ";
        cin>>student.second_name;
        cout<<"\nLast name: ";
        cin>>student.last_name;
        cout<<"\nScore: ";
        cin>>student.score;

        cout<<"\nI got you inputs and I'm processing them.\n";

        file<<student.first_name<<" ";
        file<<student.second_name<<" ";
        file<<student.last_name<<" ";
        file<<student.score<<"\n";

    }

    file.seekg(0);

    for(int i=0;i<n;i++){
        file>>student.first_name;
        file>>student.second_name;
        file>>student.last_name;
        file>>student.score;

        if(student.score>=40 && student.score<=50){
            cout<<student.first_name<<" ";
            cout<<student.second_name<<" ";
            cout<<student.last_name<<endl;
        }

    }

    file.close();

return 0;
}

On inputing n->4 then

name1 Jones Jameson 41
name2 Peterson Harrison 51
name3 Jones Jameson 49
name4 Peterson Harrison 43

The output is

I got you inputs and I'm processing them.
name1 Jones Jameson
name3 Jones Jameson
name4 Peterson Harriso
asmmo
  • 6,922
  • 1
  • 11
  • 25