Why??

faizdead's Avatar, Join Date: Apr 2012
Newbie Member
Code:
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

struct student{
       char firstname[15],sirname[15],answer[25];
       long int metric;
       int mark;
       char grade;
};

char schanswer[]={"BACDACBDABBACADAABACABCDA"};

int num=0;

void calcmark(student tp2[]);
int assigngrade(student tp2[]);

int main()
{
    student tp2[20];
    ifstream file;
    file.open("student.dat");
    if(file.fail())
    {
                   cerr<<"fail to open file"<<"student.dat";
                   exit(-1);
}
while(file>>tp2[num].firstname)
{
                               file>>tp2[num].sirname;
                               file>>tp2[num].metric>>tp2[num].answer;
                               num++;
                               }
calcmark(tp2);
assigngrade(tp2);
cout<<"\n"<<setw(11)<<"name"<<setw(9)
    <<"mark"<<setw(15)<<"grade"<<" \n\n";
for(int i=0; i<num; i++)
   cout<<setw(8)<<tp2[i].firstname<<setw(8)<<tp2[i].sirname<<setw(8)<<tp2[i].mark<<setw(8)<<tp2[i].grade<<"\n";
   file.close();
   system("pause");
   return 0;
}

void calcmark(student tp2[])
{
     int i,j;
     for(int i=0;i<num;i++)
     {
             tp2[i].mark=0;
             for(int j=0;j<25;j++)
             if(tp2[i].answer[j]==schanswer[j])
                tp2[i].mark+=4;
             else
                tp2[i].mark -=1;
                }
                }
int assigngrade( student tp2[])
{
     int i;
     char A,B,C,D,E;
     for(int i=0;i<num;i++)
     {
           
             if
             (tp2[i].mark>=75 && tp2[i].mark<=100){
                tp2[i].grade='A';}
            else if
             (tp2[i].mark>=65 && tp2[i].mark<=74){
                 tp2[i].grade='B';
                 }
              else if
             (tp2[i].mark>=55 && tp2[i].mark<=64){
                 tp2[i].grade='C';}
              else if
            (tp2[i].mark>=45 && tp2[i].mark<=54){
                 tp2[i].grade='D';}
                else {
                 tp2[i].grade='E';}
              }
            
              
                          
}





this is my coding..i need it to read more than 10 student..but the problem is when it comes to student number 9 and above,weird ouput were shown..why is it?can someone help me?

this is example of the file..


Siti Salleh 11111 BACDACBDABBACACDABCDABCDA
Nuraini Ismail 11122 BBCCDDAABBCCDDAABBCCDDAAB
Muhammad Hazim 11133 AAAAABBBBBCCCCCDDDDDABCDA
Hafiz Hashim 11144 BACDACBDABBACADAABACABCDA
Azhar Ruddin 11155 BABACDBBDCBACDBACDBACDABC
Izzat Yong 11166 BAADAABDABBADACDCBCDABCAA
Ros Lan 11177 BDCDACDBAABACDCAABDDCBCDD
Sau Fi 11188B DCDACDDAABACDCAABDDCBCDD
Muhai Min 11199 BDCBACDDAABACDBADBDDCBCDD
Ci Kong 11100 ADCDBCDCAABBCBCAABDDCACAD
Jun Bin 11200 BDCDDCDCBABBCBCABBADCACAD

Last edited by shabbir; 27Apr2012 at 07:16.. Reason: Code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Please use code blocks when posting code.

What is the weird output you see?