Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Why?? (http://www.go4expert.com/forums/why-t28275/)

faizdead 26Apr2012 21:49

Why??
 
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

xpi0t0s 27Apr2012 03:11

Re: Why??
 
Please use code blocks when posting code.

What is the weird output you see?


All times are GMT +5.5. The time now is 11:36.