User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,909 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,636 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 950 | Replies: 1
Reply
Join Date: Feb 2007
Posts: 2
Reputation: tatumkay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tatumkay tatumkay is offline Offline
Newbie Poster

struct functions - lost a wee bit (i hope) in calling the functions

  #1  
Mar 29th, 2007
I cannot figure out how to "call" the functions into the main. Please help!!

 
// ******************************************************************
//
//  Grades.cpp
//
//  This program computes student grades. For each student, two
//  quiz grades (graded on a 10 point basis), one midterm exam grade
//  and one final exam grade (each on a 100 point basis) are read
//  in. The final numeric grade is computed weighing the final 
//  exam 50%, the midterm 25%, and the quizzes 25%. The numeric
//  grade and corresponding letter grade are output.
//
// ******************************************************************
#include <iostream>
using namespace std;                       


//declarations
double quiz1;
double quiz2;
double midtermExam;
double finalExam;
double courseAverage;
//
// Structure for a student record
struct StudentRecord
{
//member functions
int quiz1, quiz2, midtermExam, finalExam, courseAverage, letterGrade;
};

//
//function declarations
void inputRecord (StudentRecord record);
//postcondition 
void computeAverage (StudentRecord& record);
//precondition
char letterGrade (double numericGrade);
//precondition
void outputRecord (StudentRecord record);
//precondition

//
//main function - declares StudentRecord, Calls functions into action
int main()
{
StudentRecord record;
void inputRecord (StudentRecord.record);
void computeAverage (StudentRecord.record);
char letterGrade (double numericGrade);
void outputRecord (StudentRecord.record);
}

//
//input definition
void inputRecord (StudentRecord record)
{ 
  cout << "Please Enter Your Grades Below" << endl << "Quiz Scores: ";
  cin >> quiz1;
  cout << " and "; 
  cin >> quiz2; 
  cout << endl;
  cout << "Midterm Exam Score: ";
  cin >> midtermExam; 
  cout << endl;
  cout << "Final Exam Score: ";
  cin >> finalExam;
  cout << endl;
}

//
// average definition
void computeAverage (StudentRecord& record)
{
  const double EXAM_WT = 0.5;
  const double MIDTERM_WT = 0.25;
  const double QUIZ_WT = 0.25;
  double quiz1Percent, quiz2Percent;

  // Convert the 10 point quizzes to a percent, then find the average
  quiz1Percent = 100 * record.quiz1 / 10.0;
  quiz2Percent = 100 * record.quiz2 / 10.0;
  double quizAvg = (quiz1Percent + quiz2Percent) / 2;

  // Compute the weighted average to get the numeric course grade
  record.courseAverage = quizAvg * QUIZ_WT + record.midtermExam * MIDTERM_WT + 
  record.finalExam * EXAM_WT;

  // Call the letterGrade function to find the letter grade
  record.letterGrade = letterGrade (record.courseAverage);
} 

//
// letterGrade defintion
char letterGrade (double numericGrade)
{
  char letter;

  if (numericGrade < 60)
    letter = 'F';
  else if (numericGrade < 70)
    letter = 'D';
  else if (numericGrade < 80)
    letter = 'C';
  else if (numericGrade < 90)
    letter = 'B';
  else
    letter = 'A';

  return letter;
}


//output definition
void outputRecord (StudentRecord record)
{
  cout << endl;
  cout << "Quiz Scores: " << record.quiz1 << "  " << record.quiz2 << endl;
  cout << "Midterm Exam Score: " << record.midtermExam << endl;
  cout << "Final Exam Score: " << record.finalExam << endl;
  cout << endl;
  cout << "Course Average: " << record.courseAverage << endl;
  cout << "Final Letter Grade: " << record.letterGrade << endl;
  cout << endl;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,446
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 265
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: struct functions - lost a wee bit (i hope) in calling the functions

  #2  
Mar 29th, 2007
First of all, trash these globals at the top of the program!
  1. double quiz1;
  2. double quiz2;
  3. double midtermExam;
  4. double finalExam;
  5. double courseAverage;

That's why you made a struct. So you wouldn't have to use globals. If you need to modify values inside one of your functions, use references or pointers. That's what they're there for.

Also, don't put the function's return type when calling. For example, this is wrong in main():
  1. void inputRecord (StudentRecord.record);
Another thing is that when you're passing record, it's just that: record. You don't need to prefix it with StudentRecord. .

And don't forget that you need to pass a reference to inputRecord. Otherwise you'll just lose the values that the user inputted when the function returns.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC