Need Help

This is my coding
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

struct records
{
int Eng,Math,Phy,Che,Bio,GPA;
char name[30],IC[30],Fcode[30],result[30];
char year[30];

};

main()
{
int ns;
char IC[30],year1[30],result1[30];
char Egrade,Mgrade,Cgrade,Pgrade,Bgrade;
ifstream Test1;
Test1.open("Test1.txt");

struct records T1[3];
int i,x;


while(!Test1.eof())
{
Test1.getline(T1[x].IC,30);
Test1.getline(T1[x].name,30);
Test1.getline(T1[x].Fcode,30);
Test1.getline(T1[x].result,30);
Test1.getline(T1[x].year,30);
Test1>>T1[x].Eng;
Test1>>T1[x].Math;
Test1>>T1[x].Phy;
Test1>>T1[x].Che;
Test1>>T1[x].Bio;
Test1>>T1[x].GPA;
}

cout<<"SAPS\n"<<endl;
cout<<"-----------\n"<<endl;
cout<<"Login"<<endl;
cout<<"-----------"<<endl;
cout<<"IC / Passport no :\n";
cin.getline(IC,30);

system("pause");// to pause the system
system("cls");//clear screen for the next output screen

for(i=0;i<3;i++)
{
if (strcmp(T1[i].IC,IC)==0)
{
do
{

cout<<"--------------"<<endl;
cout<<"Enter year :"<<endl;
cout<<"--------------\n"<<endl;
cout<<"2015"<<endl;
cout<<"2016"<<endl;
cout<<"2017\n"<<endl;
cin>>year1;
cout<<endl<<endl;

system("pause");// to pause the system
system("cls");//clear screen for the next output screen

cout<<"--------------"<<endl;
cout<<"Enter the number of the type of result needed:"<<endl;
cout<<"--------------\n"<<endl;
cout<<"1.Test 1"<<endl;
cout<<"2.Mid Term Test"<<endl;
cout<<"3.Final Test"<<endl;
cin>>result1;
cout<<endl<<endl;

system("pause");// to pause the system
system("cls");//clear screen for the next output screen

//calculation for grades

if(T1[i].Eng>=80)
Egrade='A';
else if(T1[i].Eng>=60)
Egrade='B';
else if(T1[i].Eng>=50)
Egrade='C';
else if(T1[i].Eng>=40)
Egrade='D';
else
Egrade='F';

if(T1[i].Math>=80)
Mgrade='A';
else if(T1[i].Math>=60)
Mgrade='B';
else if(T1[i].Math>=50)
Mgrade='C';
else if(T1[i].Math>=40)
Mgrade='D';
else
Mgrade='F';

if(T1[i].Phy>=80)
Pgrade='A';
else if(T1[i].Phy>=60)
Pgrade='B';
else if(T1[i].Phy>=50)
Pgrade='C';
else if(T1[i].Phy>=40)
Pgrade='D';
else
Pgrade='F';

if(T1[i].Che>=80)
Cgrade='A';
else if(T1[i].Che>=60)
Cgrade='B';
else if(T1[i].Che>=50)
Cgrade='C';
else if(T1[i].Che>=40)
Cgrade='D';
else
Cgrade='F';

if(T1[i].Bio>=80)
Bgrade='A';
else if(T1[i].Bio>=60)
Bgrade='B';
else if(T1[i].Bio>=50)
Bgrade='C';
else if(T1[i].Bio>=40)
Bgrade='D';
else
Bgrade='F';

if ((strcmp(T1[i].year,year1)==0) && (strcmp(T1[i].result,result1)==0))
{
cout<<"Student Name :"<<T1[i].name<<endl;
cout<<"Student Faculty Code :"<<T1[i].Fcode<<endl;
cout<<"Type of Test"<<endl;
cout<<"1.Test 1\n2.Mid Term Test \n3.Final Test"<<endl;
cout<<"Choosen :"<<T1[i].result<<endl;
cout<<"-------------------------------"<<endl;
cout<<"No.| Subject | Marks | Grade "<<endl;
cout<<"-------------------------------"<<endl;
cout<<"1. | English |"<<T1[i].Eng<<" | "<<Egrade<<endl;
cout<<"2. | Mathematics |"<<T1[i].Math<<" | "<<Mgrade<<endl;
cout<<"3. | Physics |"<<T1[i].Phy<<" | "<<Pgrade<<endl;
cout<<"4. | Chemistry |"<<T1[i].Che<<" | "<<Cgrade<<endl;
cout<<"5. | Biology |"<<T1[i].Bio<<" | "<<Bgrade<<endl;
cout<<"GPA for the test:"<<T1[i].GPA<<endl;

cout<<"---------------------------------"<<endl;
cout<<"Choose 1-3 to proceed to next step :"<<endl;
cout<<"---------------------------------\n"<<endl;
cout<<"1. Print report card"<<endl;
cout<<"2. Back to result type page"<<endl;
cout<<"3. Quit\n"<<endl;
cin>>ns;
}
system("pause");// to pause the system
system("cls");//clear screen for the next output screen
//calculation for grades
}while(ns==2);

if (ns==1)
{
cout<<"Printing Result..."<<endl;
return 0;
}
else if (ns==3)
return 0;


}
}
Test1.close();
}



Im reading a file which have 3 people data but then the coding didt not work when it compare. This is the input coding
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct records
{
int Eng,Math,Phy,Che,Bio,GPA;
char name[30],IC[30],Fcode[30],result[30];
char year[30];

};

main()
{

ofstream Test1;
Test1.open("Test1.txt");

struct records T1 [3];
int i;

for(i=0;i<3;i++)
{
cout<<"Enter IC\n";
cin.getline(T1[i].IC,30);
cout<<"Enter Name\n";
cin.getline(T1[i].name,30);
cout<<"Enter Faculty Code\n";
cin.getline(T1[i].Fcode,30);
cout<<"Enter Test Result\n";
cout<<"1.Test 1"<<endl;
cout<<"2.Mid Term Test"<<endl;
cout<<"3.Final Test"<<endl;
cin.getline(T1[i].result,30);
cout<<"Enter Year\n";
cin.getline(T1[i].year,30);
//store marks3
cout<<"Enter English Marks\n";
cin>>T1[i].Eng;
cout<<"Enter Mathematic Marks\n";
cin>>T1[i].Math;
cout<<"Enter Physics Marks\n";
cin>>T1[i].Phy;
cout<<"Enter Chemistry Marks\n";
cin>>T1[i].Che;
cout<<"Enter Biology Marks\n";
cin>>T1[i].Bio;
T1[i].GPA=(T1[i].Eng+T1[i].Math+T1[i].Phy+T1[i].Che+T1[i].Bio)/5;
cin.ignore();
Test1<<T1[i].IC<<endl;
Test1<<T1[i].name<<endl;
Test1<<T1[i].Fcode<<endl;
Test1<<T1[i].result<<endl;
Test1<<T1[i].year<<endl;
Test1<<T1[i].Eng<<endl;
Test1<<T1[i].Math<<endl;
Test1<<T1[i].Phy<<endl;
Test1<<T1[i].Che<<endl;
Test1<<T1[i].Bio<<endl;
Test1<<T1[i].GPA<<endl;
}
Test1.close();
}
and the data in the file is like dis
002343234
Chin Thiam Fatt
DICTN
1
2017
54
54
54
54
54
54
000912
D
D
1
2017
54
54
54
54
54
54
0007362832
CHin Thiam Fatt
DICTN
1
2017
54
54
54
54
54
54
but still when i didt do any loop it work and just for one data , when i have three person data it will stop working
1. "need help" isn't a subject title.
http://www.catb.org/esr/faqs/smart-questions.html#bespecific

2. Use [code][/code] tags when you post code.
Like so.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>  //!! for system()
using namespace std;

struct records {
  int Eng, Math, Phy, Che, Bio, GPA;
  char name[30], IC[30], Fcode[30], result[30];
  char year[30];

};


//!!foo.cpp:13:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
//!! main()
//!!      ^
int main()
{
  int ns;
  char IC[30], year1[30], result1[30];
  char Egrade, Mgrade, Cgrade, Pgrade, Bgrade;
  ifstream Test1;
  Test1.open("Test1.txt");

  struct records T1[3];
  int i, x;

  //!! Beware that reading 3 records from a file containing 3 records will still leave eof() as false
  //!! eof() only becomes true AFTER an input function like getline() has also failed.
  while (!Test1.eof()) {
    //!! foo.cpp:31:31: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    //!! Test1.getline(T1[x].IC, 30);
    Test1.getline(T1[x].IC, 30);
    Test1.getline(T1[x].name, 30);
    Test1.getline(T1[x].Fcode, 30);
    Test1.getline(T1[x].result, 30);
    Test1.getline(T1[x].year, 30);
    Test1 >> T1[x].Eng;
    Test1 >> T1[x].Math;
    Test1 >> T1[x].Phy;
    Test1 >> T1[x].Che;
    Test1 >> T1[x].Bio;
    Test1 >> T1[x].GPA;
    //!! it might be a good idea to increment x as well
  }

  cout << "SAPS\n" << endl;
  cout << "-----------\n" << endl;
  cout << "Login" << endl;
  cout << "-----------" << endl;
  cout << "IC / Passport no :\n";
  cin.getline(IC, 30);

  system("pause");              // to pause the system
  system("cls");                //clear screen for the next output screen

  for (i = 0; i < 3; i++) {
    if (strcmp(T1[i].IC, IC) == 0) {
      do {
        cout << "--------------" << endl;
        cout << "Enter year :" << endl;
        cout << "--------------\n" << endl;
        cout << "2015" << endl;
        cout << "2016" << endl;
        cout << "2017\n" << endl;
        cin >> year1;
        cout << endl << endl;

        system("pause");        // to pause the system
        system("cls");          //clear screen for the next output screen

        cout << "--------------" << endl;
        cout << "Enter the number of the type of result needed:" << endl;
        cout << "--------------\n" << endl;
        cout << "1.Test 1" << endl;
        cout << "2.Mid Term Test" << endl;
        cout << "3.Final Test" << endl;
        cin >> result1;
        cout << endl << endl;

        system("pause");        // to pause the system
        system("cls");          //clear screen for the next output screen

//calculation for grades

        if (T1[i].Eng >= 80)
          Egrade = 'A';
        else if (T1[i].Eng >= 60)
          Egrade = 'B';
        else if (T1[i].Eng >= 50)
          Egrade = 'C';
        else if (T1[i].Eng >= 40)
          Egrade = 'D';
        else
          Egrade = 'F';

        if (T1[i].Math >= 80)
          Mgrade = 'A';
        else if (T1[i].Math >= 60)
          Mgrade = 'B';
        else if (T1[i].Math >= 50)
          Mgrade = 'C';
        else if (T1[i].Math >= 40)
          Mgrade = 'D';
        else
          Mgrade = 'F';

        if (T1[i].Phy >= 80)
          Pgrade = 'A';
        else if (T1[i].Phy >= 60)
          Pgrade = 'B';
        else if (T1[i].Phy >= 50)
          Pgrade = 'C';
        else if (T1[i].Phy >= 40)
          Pgrade = 'D';
        else
          Pgrade = 'F';

        if (T1[i].Che >= 80)
          Cgrade = 'A';
        else if (T1[i].Che >= 60)
          Cgrade = 'B';
        else if (T1[i].Che >= 50)
          Cgrade = 'C';
        else if (T1[i].Che >= 40)
          Cgrade = 'D';
        else
          Cgrade = 'F';

        if (T1[i].Bio >= 80)
          Bgrade = 'A';
        else if (T1[i].Bio >= 60)
          Bgrade = 'B';
        else if (T1[i].Bio >= 50)
          Bgrade = 'C';
        else if (T1[i].Bio >= 40)
          Bgrade = 'D';
        else
          Bgrade = 'F';

        if ((strcmp(T1[i].year, year1) == 0)
            && (strcmp(T1[i].result, result1) == 0)) {
          cout << "Student Name :" << T1[i].name << endl;
          cout << "Student Faculty Code :" << T1[i].Fcode << endl;
          cout << "Type of Test" << endl;
          cout << "1.Test 1\n2.Mid Term Test \n3.Final Test" << endl;
          cout << "Choosen :" << T1[i].result << endl;
          cout << "-------------------------------" << endl;
          cout << "No.| Subject | Marks | Grade " << endl;
          cout << "-------------------------------" << endl;
          cout << "1. | English |" << T1[i].Eng << " | " << Egrade << endl;
          cout << "2. | Mathematics |" << T1[i].Math << " | " << Mgrade << endl;
          cout << "3. | Physics |" << T1[i].Phy << " | " << Pgrade << endl;
          cout << "4. | Chemistry |" << T1[i].Che << " | " << Cgrade << endl;
          cout << "5. | Biology |" << T1[i].Bio << " | " << Bgrade << endl;
          cout << "GPA for the test:" << T1[i].GPA << endl;

          cout << "---------------------------------" << endl;
          cout << "Choose 1-3 to proceed to next step :" << endl;
          cout << "---------------------------------\n" << endl;
          cout << "1. Print report card" << endl;
          cout << "2. Back to result type page" << endl;
          cout << "3. Quit\n" << endl;
          cin >> ns;
        }
        system("pause");        // to pause the system
        system("cls");          //clear screen for the next output screen
//calculation for grades
      } while (ns == 2);

      if (ns == 1) {
        cout << "Printing Result..." << endl;
        return 0;
      } else if (ns == 3)
        return 0;
    }
  }
  Test1.close();
}


3. How much code did you write before deciding to test it for the first time?
If it was more than say 10 lines, you're doing it wrong.

Find a comfort zone where you can write 'n' lines, compile and test successfully with reasonable success. If a test fails, you need to FIX it before digging an even bigger hole.

Write everything and dump it on a forum for someone else to fix isn't a long term strategy.

4. Look at your code.
a) lines 85 to 138 are copy/pasted. You should have 5 lines in main calling a function with a single if / else if sequence in it.
b) lines 142 to 155 should be a separate function as well.

There are other similar small blocks of code which could be turned into functions just to make your main() nice and tidy.

5. Are you learning C or C++ (or some Frankenstein C/C++)?
You're using nice C++ file streams, but you're also using horrid C-style arrays and strcmp.
Topic archived. No new replies allowed.