7 Replies - 6116 Views - Last Post: 08 February 2012 - 01:53 PM Rate Topic: -----

#1 darshilpatel34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-February 12

C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 11:50 AM

I need to declare a structure that contains: student Id, first name, last name, and five test scores. Also need to declare an array of 10 student structures. Prompt the user and read the student data from the keyboard and store the information in the array. After each student ask the user if there is to be another entry. When there are no more entries, clear the screen (system(“cls”)); then loop through the array to print each student. Calculate the average score for each student and test. Write the information in tabular form to the screen, one line for each student (including the average score) and a total line for the average score for each test. This is what I have so far. It should look something like this:

Id Student Name 1 2 3 4 5 Avg
1 Arnold, Edward 87 86 90 89 95 89.4
2 Bara, Theda 79 80 76 87 80 80.4
3 Davis, Bette 90 92 100 93 95 94.0
4 Hayes, Patrick 88 70 75 80 73 77.2
5 Day, Loraine 82 71 100 98 99 90.0
6 Ball, Lucille 98 85 72 89 91 87.0
7 Bond, James 95 100 100 90 100 97.0
Average: 88 83 88 89 90 87.9

//  Student List

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int SIZE = 2;

struct Student
{
    int studentID;
    string firstName;
    string lastName;
    int scores[5];
  
    
    
};

bool getStudent(Student& stu);
void printStudent(const Student sList[], int size);

int main()
{
   cout << "Begin Studnet struct Demo";
   
    int index = 0;
    
    Student stu;
    Student sList[SIZE];
    
    bool done = false;
    while(!done)
     {
          done = getStudent(stu);
          if(!done)
            {
              index++;
              }
          // student listsize = index;
    
    
   // cout << "Student Listing\n\n";
    printStudent(sList, SIZE);
//    cout << "\n\nEnd Student List\n\n";

    system("pause");
    return 0;
} 

}
bool getStudent(Student& stu) 

{
    char discard;
   
    
    
        cout << "Enter the first name of student " ;

        getline(cin, stu.firstName);
        
        cout << "Enter the last name of student " ;
        getline(cin, stu.lastName);
        
        cout << "Enter student test score: ";
    for (int i = 0; i < 5; i++)
    {
        cin >> stu.scores[i]; 
        }
        cin.get(discard);
        system("cls");
    }



void printStudent(const Student sList[], int size)
{ 
    //cout << fixed << noshowpoint << setprecision(0); 
    cout << left << setw(25)<< "Student Name"
         << setw(11) << "Test Score"<< endl;

    for (int i = 0; i < size; i++)
        cout << left //<< setw(25) 
             << sList[i].firstName  
             << sList[i].lastName << right << " " 
             << setw(25) << sList[i].scores << endl;
            
            
    return;    
}






Is This A Good Question/Topic? 0
  • +

Replies To: C++ I need to calculate average of 10 students from left to right

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 11:56 AM

What is your question ? What are you having difficulties with exactly ?

This post has been edited by Karel-Lodewijk: 08 February 2012 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 darshilpatel34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-February 12

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 12:03 PM

View PostKarel-Lodewijk, on 08 February 2012 - 11:56 AM, said:

What is your question ? What are you having difficulties with exactly ?
I'm having problem with entering 10 students and getting the average of individual student and the whole class. Thank You.
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,673
  • Joined: 19-February 09

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 12:16 PM

You get the student data but you don't enter it in to your list.

37	          done = getStudent(stu);



You could simply use an assignment.

     done = getStudent(stu);
     ...
     if(!done) 
     {
       sList[index] = stu;



You also need to check that you aren't trying to input more data records than the array/list will hold.
Was This Post Helpful? 0
  • +
  • -

#5 darshilpatel34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-February 12

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 12:57 PM

View Post#define, on 08 February 2012 - 12:16 PM, said:

You get the student data but you don't enter it in to your list.

37	          done = getStudent(stu);



You could simply use an assignment.

     done = getStudent(stu);
     ...
     if(!done) 
     {
       sList[index] = stu;



You also need to check that you aren't trying to input more data records than the array/list will hold.
Thank You. I got that. Now how do I prompt the user to add other student information ? And I have to ask the user "If you want to add other student information if yes then type in student's first name last name and grade if no then exit" If i stick in the following code at the end of the code i have so far it shows error @ " return resp == "y" " ?
{
     char resp;
     do
     {
         cout << "Do you want to add student information? [Y]es | [N]o : ";
         cin >> resp;
         cin.ignore(50,'\n');
         resp = tolower(resp);
     } while (resp != 'y' && resp != 'n');
     return resp == 'y';     
}    

Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,673
  • Joined: 19-February 09

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 01:22 PM

You don't say what the error is, but you could try parentheses.

10	     return (resp == 'y'); 


Was This Post Helpful? 0
  • +
  • -

#7 darshilpatel34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-February 12

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 01:32 PM

View Post#define, on 08 February 2012 - 01:22 PM, said:

You don't say what the error is, but you could try parentheses.

10	     return (resp == 'y'); 

When i compile and run the compiler error says in function "void printStudent(constStudent*,int)':

and then it says "return-statement with a value, in a function returning 'void'. And I'm sorry I'm kinda newbie in C++. Thank You for replying.
Was This Post Helpful? 0
  • +
  • -

#8 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,673
  • Joined: 19-February 09

Re: C++ I need to calculate average of 10 students from left to right

Posted 08 February 2012 - 01:53 PM

The function getStudent returns a boolean value. Why?

bool getStudent(Student& stu);



I wouldn't put it in printStudent (or getStudent), it is best to have the function just do as expected from the name. Also printStudent should only be called once after all the student data has been read.

I think you should have another function which asks whether there is to be another entry, and it can return a boolean value.

If you need more help please post an update of your code.

Edit: printStudent() is printing the list of students so might properly be named printStudentList().

// printing list of student records
void printStudentList(const Student sList[], int size);

// printing one student record
void printStudent(const Student &student);
  ...
  // could be called like so 
  printStudent(sList[i]);


This post has been edited by #define: 08 February 2012 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1