Help with my homework

Feb 1, 2017 at 2:23am
Hi there! I'm a beginner programming student and i was given this homework yesterday. im very confused and having a hard time making it run properly can you guys help me out on this one?

here is my code:

#include <iostream>
using namespace std;

struct studentinfo{

string lastname;
string firstname;
string midname;
string program;


};
struct courseinfo{

string coursename;
float units;
string grade;


};


void studentinput(studentinfo a[]){

for(int i=0;i<5;i++){



cout<<"enter last name \n";
getline(cin,a[i].lastname);

cout<<"enter first name \n";
getline(cin,a[i].firstname);

cout<<"enter middle name\n";
getline(cin,a[i].midname);

cout<<"enter program\n";
getline(cin,a[i].program);



}


}


void input(courseinfo b[]){



for (int i = 0; i < 5; i++) {

cout << i+1 << "Enter CourseNo: ";
cin >> b[i].coursename;

cout << i+1 << "Enter # of Units: ";
cin >> b[i].units;

cout << i+1 << "Enter Grade: ";
cin >> b[i].grade;


}

}



float qpicalc(courseinfo b[]){

double totalUnits=0;
double totalPoints=0;


for (int i=0; i< 5; i++) {
totalUnits = totalUnits + b[i].units;


if (b[i].grade == "A")
totalPoints = totalPoints + (b[i].units * 4.0);
else if (b[i].grade == "B+")
totalPoints = totalPoints + (b[i].units * 3.5);
else if (b[i].grade == "B")
totalPoints = totalPoints + (b[i].units * 3.0);
else if (b[i].grade == "C+")
totalPoints = totalPoints + (b[i].units * 2.5);
else if (b[i].grade == "C")
totalPoints = totalPoints + (b[i].units * 2.0);
else
totalPoints = totalPoints + (b[i].units * 0);
}
return totalPoints / totalUnits;

};

void display(courseinfo b[]){

cout<<"course\tunits\grade"<<endl;

for(int i=0;i<5;i++){

cout<<b[i].coursename<<"\t\t"<<endl;
cout<<b[i].units<<endl<<"\t";
cout<<b[i].grade<<endl;

}

}



int main(int argc,char** argv){

for(int studentnum=1; studentnum<2;studentnum++){

studentinput();
input();
display();

cout<<"QPI: "<<qpicalc<<endl;





}





return 0;
}









Feb 1, 2017 at 2:31am
I was given this homework

What's the assignment? What's the problem? What did you try? How did it fail?
Feb 1, 2017 at 3:29am
well the assignment was
Create a program

1. define a courseType struct with the following members:

1.a courseNo (e.g. CS 111)

1.b numberOfUnits (e.g. 3)

1.c grade (e.g. A)

2. define a studentType struct with the following members:

2.a lastName

2.b firstName

2.c programme (e.g. BSA, BSIT)

2.d an array to hold the courses Taken.


3. Ask the user to input 2 students and their corresponding 5 courses.

4. Compute for the QPI for each student.

5. Display the QPIs of both students and which student is better in academic performance.


E.g.


Ramon's QPI is 2.51

Rona's QPI is 3.68

and the problem is it skips a step where you have to enter a grade and it terminates immediately
Feb 1, 2017 at 9:24pm
2. define a studentType struct with the following members:
2.a lastName
2.b firstName
2.c programme (e.g. BSA, BSIT)
2.d an array to hold the courses Taken.

You haven't defined the array of courses taken by a student.
Your studentType has a middle name, which isn't required.
Feb 1, 2017 at 9:38pm
You have nearly-working code and fixing the remaining problems wouldn't really teach you much so I'm going to give you working code:

- I changed coursename to coursenum in the courseinfo struct
- I removed the middlename from studentinfo and added the array of courses.
- Added cin.ignore(1000, '\n'); to input. This causes it to skip over the newline after you enter the grade. Without this code, when you prompts for the second student's name, it will read that pending newline as their last name and then all hell breaks loose.
- I assume that qpicalc() does all the right stuff, but I'm not familiar with this calculation so I can't be sure. Nice job here. In similar posts, other students have struggled with this sort of thing.
- rewrote main() to have an array of 2 students, get their input and print the results.

You will still need to add some code to determine which student has the highest qpi. I suggest that you store the qpi into a variable before printing it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;

struct courseinfo
{
    int  coursenum;
    float units;
    string grade;
};

struct studentinfo
{
    string lastname;
    string firstname;
    string program;
    courseinfo courses[5];
};

void
studentinput(studentinfo &si)
{
    cout << "enter last name \n";
    getline(cin, si.lastname);

    cout << "enter first name \n";
    getline(cin, si.firstname);

    cout << "enter program\n";
    getline(cin, si.program);

}

void
input(courseinfo b[])
{
    for (int i = 0; i < 5; i++) {
	cout << i + 1 << "Enter CourseNo: ";
	cin >> b[i].coursenum;

	cout << i + 1 << "Enter # of Units: ";
	cin >> b[i].units;

	cout << i + 1 << "Enter Grade: ";
	cin >> b[i].grade;
	cin.ignore(1000, '\n');
    }
}

float
qpicalc(courseinfo b[])
{

    double totalUnits = 0;
    double totalPoints = 0;

    for (int i = 0; i < 5; i++) {
	totalUnits = totalUnits + b[i].units;

	if (b[i].grade == "A")
	    totalPoints = totalPoints + (b[i].units * 4.0);
	else if (b[i].grade == "B+")
	    totalPoints = totalPoints + (b[i].units * 3.5);
	else if (b[i].grade == "B")
	    totalPoints = totalPoints + (b[i].units * 3.0);
	else if (b[i].grade == "C+")
	    totalPoints = totalPoints + (b[i].units * 2.5);
	else if (b[i].grade == "C")
	    totalPoints = totalPoints + (b[i].units * 2.0);
	else
	    totalPoints = totalPoints + (b[i].units * 0);
    }
    return totalPoints / totalUnits;

};

void
display(courseinfo b[])
{
    cout << "course\tunits\tgrade" << endl;

    for (int i = 0; i < 5; i++) {
	cout << b[i].coursenum << "\t";
	cout << b[i].units << "\t";
	cout << b[i].grade << '\n';
    }
}

int
main(int argc, char **argv)
{
    studentinfo students[2];

    for (int i=0; i<2; ++i) {
	studentinput(students[i]);
	input(students[i].courses);
	cout << '\n' << students[i].firstname << ' ' << students[i].lastname << '\n';
	display(students[i].courses);
	cout << "QPI: " << qpicalc(students[i].courses) << endl;

    }

    return 0;
}

Feb 5, 2017 at 3:44pm
hey thanks alot man, sorry i only got to replied now. i totally forgot i posted this
Topic archived. No new replies allowed.