C++/c++

Advertisement


Question
QUESTION: could you pls help mi!!!i do nt knw which part of my code went wrong.wen i run the program,it onli printed out the correct ans,follow by 1 student's name and his ans.it did nt print the rest of the student's name,ans,grade and scores at all.i realli need your help!!!i will be appreciated for all the help frm you!!!


the output should hv correct ans,10 student's name follow by the their ans,grade and scores.i hv created a file name test.txt,tis is the info inside the file:

ACCADDEBBABAADCCAEAA

Jack    ACBADDE BACBADCBAEBA
Angie   CCBAEEA BBACDDEDEBCC
Grace   CEBABBB BCBCCCAEECBA
Fiona   ACBACCA ECABBBACCCEA
Helen   AAABDDE BACBADCBAEBA
Bernard CCCEDDE EEECCCCAEBAC
Charlie BBADECE CECABECACEAA
Desmond CACADEB BACBAACEBACC
Eve     AAACEBA BACBBBACECAA
Isaac   CDECDAA CCCEEEBAAABE

code:
#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>
#include <conio.h>
#define MAX_STUDENT 10

using namespace std;

struct student_record
{
  char name[80];
  char answer[20];
  int score;
  char grade;

};


void check_correct_answers (char answer[],struct student_record);
void display_student_grade(int score,struct student_record);
int findlowscore(int score[], int size);
int findhighscore(int score[], int size);


int main()
{
  
  char answer;
  void display_student_grade();
  int findhighscore();
  int findlowscore();
     
     
  ifstream inFile;
   

   inFile.open ("test.txt");
   
  if (!inFile.good())
  {
     cerr << "File could not be opened" << endl;
     //exit( 1 );
  }
   cout << answer <<endl;
   student_record student[MAX_STUDENT];
    
    for (int i=0;i<10;i++)
    {
       
     inFile.getline(student[i].name,80,' ');
     inFile.getline(student[i].answer,20,'\n');
     
     
     cout <<student[i].name<<' ';
     cout <<student[i].answer<<endl;
  
    }
      
   
  system("pause");
  
  return 0;
}


void check_correct_answers(char answer[])
{
  int i, correct = 0, incorrect = 0, blank = 0;
  char studentAnswers[20];
  double score;
  for (i = 0; i < 20; i++)
    {
     if (answer[i] == studentAnswers[i])
         correct++;
     if (answer[i] != studentAnswers[i])
         incorrect++;
     if (studentAnswers[i] == ' ')
     {
         blank++;
         incorrect--;
     }
     score = (correct * 2) + (incorrect * -1) + (blank * 0);
   }
 cout << score << endl;
 
}


void display_student_grade(int score,struct student_record)
{
   int i;
   for (i = 0; i < 10; i++)
   {
       if (score >= 80)
         cout<<"A"<<endl;     
       else if (score >=70)
         cout<<"B"<<endl;
       else if (score >=60)
         cout<<"C"<<endl;
       else if (score >=50)
         cout<<"D"<<endl;
       else
         cout<<"F"<<endl;
   }
   
}

int findhighscore(int score[], int size)
{
     
   int highscore = score[100];
   for (int i = 0; i < size; i++)
  {

         if (score[i] > highscore)
     {
         highscore = score[i];
       }
   
   }
  
  
   return highscore;
}


int findlowscore(int score[], int size)
{

   int lowscore = score[0];
   for (int i = 0; i < size; i++)
  {

       if (score[i] < lowscore)
     {
         lowscore = score[i];
       }
  
  
   }

   return lowscore;
}



ANSWER: In you data file,

ACCADDEBBABAADCCAEAA

Jack    ACBADDE BACBADCBAEBA
Angie   CCBAEEA BBACDDEDEBCC
Grace   CEBABBB BCBCCCAEECBA
.....

do away with the space in between the answers; it gets in the way of reading an array of 20 chars. create it like this instead:

ACCADDEBBABAADCCAEAA

Jack    ACBADDEBACBADCBAEBA
Angie   CCBAEEABBACDDEDEBCC
Grace   CEBABBBBCBCCCAEECBA
.....

Avoid using the header <conio.h>, it is not part of either standard C++ or standard C. In any case, you do not need it for this program.

also avoid the using system("pause") ; in your programs. it is not portable. It may also cause a security vulnerability in your code. Use a simple std::cin.get() instead.

The number of characters in the answer is 20, so you need an array which can hold 21 chars (C strings require a null character at the end).

And do not hard-code 'magic numbers' in your program, use manifest constants instead. see http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constant

Now we come to the crux of the problem:

a. the functions

 istream& istream::getline( char* buffer, streamsize num );
 istream& istream::getline( char* buffer, streamsize num, char delim );

read at most num-1 characters. (They place a null character after the last character read.) To read 20 characters from a stream, num should be specified as 21.

b. the delimiter (space or newline in your code) delim character (newline normally) is not read. they remain in the input stream buffer. You have to extract an discard them before the next item can be read. An easy way to do this if delimiters are white spaces is to use the manipulator std::ws.


#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

enum { MAX_STUDENT = 10, NAME_SIZE = 81, ANSWER_SIZE = 21 } ;

using namespace std;

struct student_record
{
      char name[NAME_SIZE];
      char answer[ANSWER_SIZE];
      int score;
      char grade;
};

// ...

int main()
{
   // ...

   ifstream inFile;
   inFile.open ("test.txt");

   if (!inFile.good())
   {
       cerr << "File could not be opened" << '\n' ;
       return 1 ;
   }

   // read the first line (answers)
   char answer[ANSWER_SIZE] ; // should be an array
   inFile.getline( answer, ANSWER_SIZE ) ;
   inFile >> ws ; // discard the debris left the buffer
   cout << "answer: " << answer <<endl;

   // read the student names and grades
   student_record student[MAX_STUDENT];

   for (int i=0;i<10;i++)
   {
       inFile.getline( student[i].name,NAME_SIZE,' ');
       inFile >> ws ; // discard the debris left the buffer
       inFile.getline( student[i].answer, ANSWER_SIZE, '\n' );
       inFile >> ws ; // discard the debris left the buffer

       cout << i << ". " <<student[i].name<< " - " ;
       cout << student[i].answer << '\n' ;
   }

   cin.get() ;
}

// ...






---------- FOLLOW-UP ----------

QUESTION: in a data file i want the text file to be able to read a blank space.example qn1 is A,qns 2 is C,qns 3 is B.student jack did nt ans qns 8.correct ans 2 marks,wrong ans deduct 1 mark,no ans 0 mark.the function and the delimiter u mention where should i put the code that u hv just wrote.i hv changed the code tat u ask mi to change.i do nt knw y the last student is nt printed out.the grade n scores did nt print out also.could you pls help mi.wen i run the program it become:

answer: ACCADDEBBABAADCCAEAA
0. Jack - ACBADDE BACBADCBAEBA
1. Angie - CCBAEEA BBACDDEDEBCC
2. Grace - CEBABBB BCBCCCAEECBA
3. Fiona - ACBACCA ECABBBACCCEA
4. Helen - AAABDDE BACBADCBAEBA
5. Bernard - CCCEDDE EEECCCCAEBAC
6. Charlie - BBADECE CECABECACEAA
7. Desmond - CACADEB BACBAACEBACC
8. Eve - AAACEBA BACBBBACECAA
9.  -

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

enum { MAX_STUDENT = 10, NAME_SIZE = 81, ANSWER_SIZE = 21 };

using namespace std;

struct student_record
{
  char name[NAME_SIZE];
   char answer[ANSWER_SIZE];
   int score;
   char grade;

};


void check_correct_answers (char answer[ANSWER_SIZE],struct student_record);
void display_student_grade(int score,struct student_record);
int findlowscore(int score[], int size);
int findhighscore(int score[], int size);


int main()
{
  
  void display_student_grade();
  int findhighscore();
  int findlowscore();
     
     
  ifstream inFile;
   inFile.open ("test2.txt");
   
  if (!inFile.good())
  {
     cerr << "File could not be opened" << '\n';
     return 1;
  }
   
   // read the first line (answers)
    char answer[ANSWER_SIZE] ; // should be an array
    inFile.getline( answer, ANSWER_SIZE ) ;
    inFile >> ws ; // discard the debris left the buffer
    cout << "answer: " << answer <<endl;

    // read the student names and grades
    student_record student[MAX_STUDENT];
   
   for (int i=0;i<10;i++)
    {
      inFile.getline( student[i].name,NAME_SIZE,' ');
      inFile >> ws ; // discard the debris left the buffer
      inFile.getline( student[i].answer, ANSWER_SIZE, '\n' );
      inFile >> ws ; // discard the debris left the buffer

      cout << i << ". " <<student[i].name << " - " ;
      cout << student[i].answer << '\n' ;
    }
   
   cin.get();
}


void check_correct_answers(char answer[ANSWER_SIZE])
{
  int i, correct = 0, incorrect = 0, blank = 0;
  char studentAnswers[20];
  double score;
  for (i = 0; i < 20; i++)
    {
     if (answer[i] == studentAnswers[i])
         correct++;
     if (answer[i] != studentAnswers[i])
         incorrect++;
     if (studentAnswers[i] == ' ')
     {
         blank++;
         incorrect--;
     }
     score = (correct * 2) + (incorrect * -1) + (blank * 0);
   }
 cout << score << endl;
 
}

// PRINT OUT STUDENT GRADES
void display_student_grade(int score,struct student_record)
{
   int i;
   for (i = 0; i < 10; i++)
   {
       if (score >= 80)
         cout<<"A"<<endl;     
       else if (score >=70)
         cout<<"B"<<endl;
       else if (score >=60)
         cout<<"C"<<endl;
       else if (score >=50)
         cout<<"D"<<endl;
       else
         cout<<"F"<<endl;
   }
   
}

int findhighscore(int score[], int size)
{
     
   int highscore = score[100];
   for (int i = 0; i < size; i++)
  {

       // If the current score is higher than our last recorded score, record it as the new high score.
       if (score[i] > highscore)
     {
         highscore = score[i];
       }
   
   }
  
  
   return highscore;
}


int findlowscore(int score[], int size)
{

   int lowscore = score[0];
   for (int i = 0; i < size; i++)
  {

       if (score[i] < lowscore)
     {
         lowscore = score[i];
       }
  
  
   }

   return lowscore;
}  

Answer
> i want the text file to be able to read a blank space.

You can leave a blank space space in between provided the number of spaces plus non-spaces in the answer does not exceed ANSWER_SIZE-1

It would be a better idea to indicate an unanswered question by a special character like X or U. You would not need to worry about distinguishing between a white space and an unanswered question.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.