Question

we have this given problem to work on which is..

Create a program that would compute for the grade equivalent of two students who just took an exam in Math, Science and English. Your program should consist of two structures: first, struct Subjects that has as its members Math, Science and English in which the grade equivalent of the two students is stored; second, struct Student with members Name, Course, and StudentSub whose type is Subjects.

Use a pointer to store the information for the two different students. After entering the name and course of a student, the user must have a choice of which subject she wants to input a grade first. After the user chooses which subject she wants to put a grade, the user should input the student’s score and the total points of the exam. A separate function should compute for the grade of the student given the score and the total points of the exam.

After entering all information the program must display both the student’s name, course and grades in the different subjects.

Ex:          Name: Sarah

                Course: CS

                Math: 2.5

                English: 2.5

                Science: 2.5  

 

                Name: Mark

                Course: IT

                Math: 2.5

                English: 3

                Science: 2

 

To compute for the grade we must compute for the percent correct first.

 

                Percent Correct = (Score / Highest Possible Score)* 100



And what I did was:

#include <iostream>
#include <string>
using namespace std;


struct Subjects
{
    int math;
    int science;
    int english;

};
struct Student
{
    string name;
    string course;
    string StudentSub;
};

int percent(int a, int b)

    {
        int r;
        r=(a/b)*100;
        return r;
    }

void main()
{    
    int c;
    int score,total;
    char choice;
    Student astud;
    Student *pstud;

    pstud = &astud;

    cout<<"Name :";
    getline(cin,pstud->name);
    cout<<"Course :";
    getline(cin,pstud->course);

    
        cout<<"Choose a subject:"<<endl;
        cout<<"1.Math"<<endl;
        cout<<"2.Science"<<endl;
        cout<<"3.English"<<endl;
        cin>>choice;

        switch(choice)
        {

        case '1' :
        
            cout<<"Math"<<endl;
            cout<<"Score: ";
            cin>>score;
            cout<<"Total Score: ";
            cin>>total;
            c = percent(score,total);
            cout<<"Grade: "<<c<<endl;
            break;
        
        case '2':
        
            cout<<"Science:"<<endl;
            cout<<"Score: ";
            cin>>score;
            cout<<"Total Score: ";
            cin>>total;
            break;
        
        case '3':
        
            cout<<"English:"<<endl;
            cout<<"Score: ";
            cin>>score;
            cout<<"Total Score: ";
            cin>>total;
            break;
        default: 
            cout<<"Try again"<<endl;
        }
}

I got stuck. Specially with the calling of the functions. Can I please get some help? Thank you.

Link to comment
https://www.neowin.net/forum/topic/1267580-i-need-help/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

There are a few problems that I can see. First of all StudentSub should be a Subjects struct, not a string.  Secondly, I would break the program down into logical units such as input and printing. By encapsulating the functionality into distinct functions, you're able to easily process multiple students without code repetition. A student class would be better, but your assignment dictates structs.

This is an example of how you might put it together:

#include <stdlib.h>
#include <string>
#include <iostream>
#include <map>
#include <sstream>
using namespace std;

// Singleton pattern wrapping an id -> enumeration
class SubjectList {
	public:
		typedef enum {MATH, ENGLISH, SCIENCE} Subject;
		static SubjectList* getInstance() {
			if (NULL == singleton)
				singleton = new SubjectList();
			return singleton;
		}
		bool isValidSubject(int id) {
			return list.end() != list.find(id);
		}
		Subject getSubject(int id) {
			return list.at(id).enumeration; 
		}
		int count() {return list.size();}
		void print() {
			map<unsigned int, SubjectType>::iterator iter;
			for (iter = list.begin(); list.end() != iter; iter++)
				cout << iter->first << "." << iter->second.description << endl;
		}			
	private:
		typedef struct {
			Subject enumeration;
			string  description;
		} SubjectType;
		SubjectType makeType(Subject enumeration, string description) {
			SubjectType type = {enumeration, description};
			return type;
		}
		SubjectList() {
			list[1] = makeType(MATH, "Math");
			list[2] = makeType(ENGLISH, "English");
			list[3] = makeType(SCIENCE, "Science");
		}
		static SubjectList* singleton;
		map<unsigned int, SubjectType> list;
};
SubjectList* SubjectList::singleton = NULL;

typedef struct {
	float math;
	float english;
	float science;
} Subjects;

typedef struct {
	string name;
	string course;
	Subjects grades;
} Student;

bool
isValidStudentName(string name) {
	// do student name validation
	return !name.empty();
}
bool
isValidStudentCourse(string course) {
	// validate against list of approved courses
	return !course.empty();
}
float 
calcGrade(int score, int totalPoints) {
	return ((float)score / totalPoints) * 100;
}	
bool 
inputStudent(Student *student) {
	string input;

	cout << "Enter student's name: ";
	getline(cin, input);
	if (!isValidStudentName(input)) {
		cout << "Invalid student name" << endl;
		return false;
	}		
	
	student->name = input;
	cout << "Enter student " << student->name << "'s course: ";
	getline(cin, input);
	if (!isValidStudentCourse(input)) {
		cout << "Invalid course" << endl;
		return false;
	}		

	student->course = input;

	SubjectList* subjects = SubjectList::getInstance();
	for (int i = 0; i < subjects->count(); i++) {
		cout << "Which subject would you like to grade?" << endl;
		subjects->print();	

		int id; 
		getline(cin, input);
		istringstream(input) >> id;
		if (!subjects->isValidSubject(id)) {
			cout << "Invalid subject" << endl;
			return false;
		}
		
		int score, totalPoints;
		cout << "Enter score: ";
		getline(cin, input);
		istringstream(input) >> score;
		cout << "Enter total points of the exam: ";
		getline(cin, input);
		istringstream(input) >> totalPoints;

		float grade = calcGrade(score, totalPoints);
		switch (subjects->getSubject(id)) {
			case SubjectList::MATH:
				student->grades.math = grade;
				break;	
			case SubjectList::ENGLISH:
				student->grades.english = grade;
				break;	
			case SubjectList::SCIENCE:
				student->grades.science = grade;
				break;	
		}			
	}
	
	return true;
}

void
printStudent(Student *student) {
	cout << student->name << endl;
	cout << student->course << endl;
	cout << "Maths: " << student->grades.math << endl;
	cout << "English: " << student->grades.english << endl;
	cout << "Science: " << student->grades.science << endl;
}

int
main (void) {

	Student unus;
	Student duo;
	
	if (!inputStudent(&unus)) {
		cout << "Invalid input for first student!" << endl;
		return EXIT_FAILURE;
	}
	if (!inputStudent(&duo)) {
		cout << "Invalid input for second student!" << endl;
		return EXIT_FAILURE;
	}

	printStudent(&unus);
	printStudent(&duo);
	
	return EXIT_SUCCESS;
}

 

Link to comment
https://www.neowin.net/forum/topic/1267580-i-need-help/#findComment-596961754
Share on other sites

This topic is now closed to further replies.
  • Posts

    • Camtasia 2026.1.3 by Razvan Serea TechSmith Camtasia is the complete professional solution for high-quality screen recording, video editing and sharing. Camtasia 2026 makes editing your videos easier, and faster than ever. The new editor is packed with enhanced video processing, all-new production technology, an innovative library, and stock videos and other creative assets to help you create more polished, professional videos. No video experience needed. Anyone can create informative, engaging videos. Create professional, eye-catching videos: Add special video effects - Apply Behaviors that are perfectly designed to animate your text, images, or icons. Get a crisp, polished look without being a professional video editor. Drag-and-drop your edits - What you see is what you get. Every effect and element in your video can be dropped and edited directly in the preview window. And you can edit at resolutions up to beautiful 4K, for clear video at any size. Get exceptional performance - Camtasia takes full advantage of your computer’s processor with 64-bit performance. You’ll get fast rendering times and enhanced stability—even on your most complex projects. Camtasia 2026.1.3 changelog: Feature Updates Improved keyboard navigability in tool panels. Improved screen reader accessibility of headings in Preferences. Tool panels can now be resized using a keyboard-navigable control. Updated color of folder icon in User Library tab for better visibility. Grouped media now render a composite waveform considering all audio media within that group. Added Long Path Aware to the manifest of Editor and Recorder. Performance Improvements Improved performance for editing groups on the timeline. Improved the project loading performance when timeline has lots of trec media with cursor data. Updates for IT Administrators Updated cpp-httplib from 0.38.0 to 0.43.3. Updated expat from 2.7.4 to 2.8.0. Updated freetype from 2.13.3 to 2.14.3. Updated harfbuzz from 13.0.1 to 14.2.0. Updated libpng16 from 1.6.55 to 1.6.58. Updated pango from 1.57.0 to 1.57.1. Updated girepository from 2.86.3 to 2.88.0. Updated pcre2-posix from 10.47.0 to 12.0.2. Added new harfbuzz-gpu.dll. Updated FFmpeg from 7.1.1 to 7.1.2. Updated aom from 3.11.0 to 3.13.1. Updated dav1d from 1.5.0 to 1.5.1. Updated ogg from 1.3.5 to 1.3.6. Updated SDL2 from 2.32.4 to 2.32.10. Updated zlib from 1.3.1 to 1.3.2. Updated Nalpeiron binaries to version 4.4.69.3. Bug Fixes Fixed an issue which prevented some user submitted crash reports from being sent. Fixed a potential memory leak when decoding HEVC or VP9 video. Fixed a potential crash when trying to delete a range selection on a magnetic track. Fixed a bug with the Properties Panel showing stale properties when only a caption is selected on the timeline. Fixed an issue that could prevent the Opacity and Blur properties from being changed in the Background Removal effect. Fixed an issue where larger Camtasia online projects may fail to open in Camtasia Editor. Table of contents thumbnails are no longer created for Smart Player exports with no table of contents. Fix resetting skew revert to revert just skew and not scale as well. Fixed editing in Snagit with snagX file with Unicode characters. Fixed a bug where grouped visual media could be cropped in some cases. Fixed importing SnagX files with Unicode characters. Localization fixes. Download: Camtasia 2026.1.3 | 309.0 MB (Shareware) View: Camtasia Homepage | Tutorials | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • yeah it seems to be Edge only. The dialog buttons work as expected in Chrome and Firefox. The phone is using Android 16 (OneUI 8.5) and Edge version  149.0.4022.53
    • I'm not aware of this issue, but to help the other guys.  What version of Android are you using? Did you try a different browser? To see if Edge is the issue here.
    • I agree when are you going to read this (really poor BTW) article? Here is a better article so you actually know what is going on and answers questions you had in other comments --> https://arstechnica.com/gadgets/2026/05/speed-boosting-low-latency-profile-is-one-of-the-improvements-coming-to-windows-11/ It is unclear if one will be able to disable the new profile at this point but I am not seeing any reason why one would.
  • Recent Achievements

    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      510
    2. 2
      PsYcHoKiLLa
      188
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!