I need help with this structures program
Apr 15, 2019 at 12:22am UTC
I am writing a program to display two student's grades and if they are a OK GOOD or EXCELENT student, and for some reason when it won't let me enter the p2 person's data.
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 104 105 106 107 108 109 110 111 112 113
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
struct Person
{
char name[50];
int grade;
float gpa;
};
void displayData(Person);
void studentStanding(Person);
void displayData2(Person);
void studentStanding2(Person);
int main()
{
Person p;
Person p2;
cout << "Enter Full name: " ;
cin.get(p.name, 50);
cout << "Enter student's grade level: " ;
cin >> p.grade;
cout << "Enter studen's GPA: " ;
cin >> p.gpa;
displayData(p);
studentStanding(p);
cout << endl;
cout << "Studen Number 2: " << endl;
cout << "Enter Full name: " ;
cin.get(p2.name, 50);
cout << "Enter student's grade level: " ;
cin >> p2.grade;
cout << "Enter studen's GPA: " ;
cin >> p2.gpa;
cout << endl;
displayData2(p2);
studentStanding2(p2);
system("pause" );
return 0;
}
void displayData(Person p)
{
cout << "\n--Displaying Information--" << endl;
cout << "Name: " << p.name << endl;
cout << "Grade Level: " << p.grade << endl;
cout << "GPA: " << p.gpa << endl;
}
void displayData2(Person p2)
{
cout << "\n--Displaying Information--" << endl;
cout << "Name: " << p2.name << endl;
cout << "Grade Level: " << p2.grade << endl;
cout << "GPA: " << p2.gpa << endl;
}
void studentStanding(Person p)
{
if (p.gpa > 3.3 and p.gpa < 3.9)
{
cout << p.name << " is a good student, but could be better! " << endl;
}
if (p.gpa < 3.3)
{
cout << p.name << " is an ok student. Has some work to do if they want to get better." << endl;
}
if (p.gpa > 3.9) {
cout << p.name << " is an EXCELLENT student! Excels past most students." << endl;
}
}
void studentStanding2(Person p2)
{
if (p2.gpa > 3.3 and p2.gpa < 3.9)
{
cout << p2.name << " is a good student, but could be better! " << endl;
}
if (p2.gpa < 3.3)
{
cout << p2.name << " is an ok student. Has some work to do if they want to get better." << endl;
}
if (p2.gpa > 3.9) {
cout << p2.name << " is an EXCELLENT student! Excels past most students." << endl;
}
}
Apr 15, 2019 at 1:09am UTC
Well, this is a buffer problem. When you read extra stuff, the buffer jams and that happens. If you do this it will work fine I guess.
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 104 105 106 107 108 109
#include <cmath>
#include <string.h>
using namespace std;
struct Person
{
char name[50];
int grade;
float gpa;
};
void displayData(Person);
void studentStanding(Person);
void displayData2(Person);
void studentStanding2(Person);
int main()
{
Person p;
Person p2;
cout << "Enter Full name: " ;
cin.get(p.name, 50);
cout << "Enter student's grade level: " ;
cin >> p.grade;
cout << "Enter studen's GPA: " ;
cin >> p.gpa;
displayData(p);
studentStanding(p);
cin.clear(); //that clears the buffer
cin.ignore(1000, '\n' );
cout << endl;
cout << "Studen Number 2: " << endl;
cout << "Enter Full name: " ;
cin.get(p2.name, 50);
cout << "Enter student's grade level: " ;
cin >> p2.grade;
cout << "Enter studen's GPA: " ;
cin >> p2.gpa;
cout << endl;
displayData2(p2);
studentStanding2(p2);
system("pause" );
return 0;
}
void displayData(Person p)
{
cout << "\n--Displaying Information--" << endl;
cout << "Name: " << p.name << endl;
cout << "Grade Level: " << p.grade << endl;
cout << "GPA: " << p.gpa << endl;
}
void displayData2(Person p2)
{
cout << "\n--Displaying Information--" << endl;
cout << "Name: " << p2.name << endl;
cout << "Grade Level: " << p2.grade << endl;
cout << "GPA: " << p2.gpa << endl;
}
void studentStanding(Person p)
{
if (p.gpa > 3.3 and p.gpa < 3.9)
{
cout << p.name << " is a good student, but could be better! " << endl;
}
if (p.gpa < 3.3)
{
cout << p.name << " is an ok student. Has some work to do if they want to get better." << endl;
}
if (p.gpa > 3.9) {
cout << p.name << " is an EXCELLENT student! Excels past most students." << endl;
}
}
void studentStanding2(Person p2)
{
if (p2.gpa > 3.3 and p2.gpa < 3.9)
{
cout << p2.name << " is a good student, but could be better! " << endl;
}
if (p2.gpa < 3.3)
{
cout << p2.name << " is an ok student. Has some work to do if they want to get better." << endl;
}
if (p2.gpa > 3.9) {
cout << p2.name << " is an EXCELLENT student! Excels past most students." << endl;
}
}
Apr 15, 2019 at 1:13am UTC
Sorry about the unclear and superficial explanation about the buffer problem. I'm kinda in a rush here. Hope it helped!
Apr 15, 2019 at 1:23am UTC
Thanks a lot, it did I will research the buffer thing!
Apr 15, 2019 at 1:36am UTC
In addition, you do not need for every person its own function. To do the same one is enough for all, just call it with another argument:
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
#include <iostream>
#include <string.h>
using namespace std;
struct Person
{
char name[50];
int grade;
float gpa;
};
//--------------------------------------------------------------
void displayData(Person x)
{
cout << "\n--Displaying Information--" << endl;
cout << "Name: " << x.name << endl;
cout << "Grade Level: " << x.grade << endl;
cout << "GPA: " << x.gpa << endl;
}
//--------------------------------------------------------------
void studentStanding(Person x)
{
cout << x.name;
if (x.gpa < 3.3)
cout << " is an ok student. Has some work to do if they want to get better.\n" ;
else if (x.gpa < 3.9)
cout << " is a good student, but could be better!\n" ;
else
cout << " is an EXCELLENT student! Excels past most students.\n" ;
}
//============================================================
int main()
{
Person p, p2;
cout << "Enter Full name: " ;
cin.get(p.name, 50);
cout << "Enter student's grade level: " ;
cin >> p.grade;
cout << "Enter studen's GPA: " ;
cin >> p.gpa;
displayData(p);
studentStanding(p);
cout << endl;
cin.clear(); //that clears the buffer
cin.ignore(1000, '\n' );
cout << "Studen Number 2: " << endl;
cout << "Enter Full name: " ;
cin.get(p2.name, 50);
cout << "Enter student's grade level: " ;
cin >> p2.grade;
cout << "Enter studen's GPA: " ;
cin >> p2.gpa;
cout << endl;
displayData(p2);
studentStanding(p2);
// system("pause");
return 0;
}
Topic archived. No new replies allowed.