469,950 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

i dont know what the problem is...

11
i know this is set up wrong but i didnt know how to do that--sorry. but this is my program that i have. it compiles and runs, but the values are completely wrong! can anyone help me??
#include <iostream>
#include <iomanip>

using namespace std;

class Student
{
private:
char lastname[40];
char firstname[40];
char ID[10];
double GPA;
int Score;

public:
Student(char [], char [], char [], double , int);
void showStudent();
void setName(char [], char []);
void setID(char []);
int setGPA(double );
int setScore(int );
char getDecision();

};

int main()
{

Student person1 = Student( "Stanko", "Brianne", "146926", 3.3, 1598);
Student person2 = Student( " Hartnett", " Josh", "123654", -2.8, 156);

person1.showStudent();
//changes firstname
person1.setName("Stanko", "Brianne");
person1.showStudent();
//changes both names
person1.setName("Stanko", "Brianne");
person1.showStudent();
//changes id
person1.setID("146926");
person1.showStudent();
//changes to valid score
person1.setScore(1467);
cout << " The new valid score is: " << person1.setScore(1598) << endl;
person1.showStudent();
//changes to invalid score
person1.setScore(1598);
cout << " The invalid score is: " << person1.setScore(156) << endl;
person1.showStudent();
//changes to valid gpa
person1.setGPA(3.5);
cout << " The new gpa is: " << person1.setGPA(3.3) << endl;
person1.showStudent();
//changes to invalid gpa
person1.setGPA(3.3);
cout << " The invalid gpa is: " << person1.setGPA(-2.8) << endl;
person1.showStudent();
person1.getDecision();
cout << " The student for this year:" << person1.getDecision() << endl;

system("pause");

return 0;
}
Student::Student(char newLastname[], char newFirstname[], char newID[],
double newGPA, int newScore)
{
strcpy(lastname, newLastname);
strcpy(firstname, newFirstname);
strcpy(ID, newID);

if(setGPA(newGPA) < 0)
{
cout << " Invalid answer: set to 0";
}

if(setScore(newScore) < 0)
{
cout << " Invalid answer: set to 0";
}

}
void Student::showStudent()
{
cout << endl;
cout << " last name: " << lastname[40];
cout << endl;
cout << " first name: " << firstname[40];
cout << endl;
cout << " id: " << ID[10];
cout << endl;
cout << " gpa: " << GPA;
cout << endl;
cout << " score: " << Score;
cout << endl;
}
void Student::setName(char newFirstname[], char newLastname[])
{
strcpy(lastname, newLastname);
strcpy(firstname, newFirstname);

}
void Student::setID(char newID[])
{
strcpy(ID, newID);

}
int Student::setGPA(double newGPA)
{

if(newGPA >= 0 && newGPA <= 4.0)
{
GPA = newGPA;
return 1;
}
else
{
return 0;
}
}
int Student::setScore(int newScore)
{
if(newScore >= 200 && newScore <= 1600)
{
Score = newScore;
return 1;
}
else
{
return 0;
}
}
char Student::getDecision()
{

if(GPA >= 3.5 || Score > 1300)
{
return 'a';
}
else if(GPA < 2.2 || Score < 900)
{
return 'd';
}
else
{
return 'w';
}
}
May 3 '07 #1
1 1587
ilikepython
844 Expert 512MB
i know this is set up wrong but i didnt know how to do that--sorry. but this is my program that i have. it compiles and runs, but the values are completely wrong! can anyone help me??
#include <iostream>
#include <iomanip>

using namespace std;

class Student
{
private:
char lastname[40];
char firstname[40];
char ID[10];
double GPA;
int Score;

public:
Student(char [], char [], char [], double , int);
void showStudent();
void setName(char [], char []);
void setID(char []);
int setGPA(double );
int setScore(int );
char getDecision();

};

int main()
{

Student person1 = Student( "Stanko", "Brianne", "146926", 3.3, 1598);
Student person2 = Student( " Hartnett", " Josh", "123654", -2.8, 156);

person1.showStudent();
//changes firstname
person1.setName("Stanko", "Brianne");
person1.showStudent();
//changes both names
person1.setName("Stanko", "Brianne");
person1.showStudent();
//changes id
person1.setID("146926");
person1.showStudent();
//changes to valid score
person1.setScore(1467);
cout << " The new valid score is: " << person1.setScore(1598) << endl;
person1.showStudent();
//changes to invalid score
person1.setScore(1598);
cout << " The invalid score is: " << person1.setScore(156) << endl;
person1.showStudent();
//changes to valid gpa
person1.setGPA(3.5);
cout << " The new gpa is: " << person1.setGPA(3.3) << endl;
person1.showStudent();
//changes to invalid gpa
person1.setGPA(3.3);
cout << " The invalid gpa is: " << person1.setGPA(-2.8) << endl;
person1.showStudent();
person1.getDecision();
cout << " The student for this year:" << person1.getDecision() << endl;

system("pause");

return 0;
}
Student::Student(char newLastname[], char newFirstname[], char newID[],
double newGPA, int newScore)
{
strcpy(lastname, newLastname);
strcpy(firstname, newFirstname);
strcpy(ID, newID);

if(setGPA(newGPA) < 0)
{
cout << " Invalid answer: set to 0";
}

if(setScore(newScore) < 0)
{
cout << " Invalid answer: set to 0";
}

}
void Student::showStudent()
{
cout << endl;
cout << " last name: " << lastname[40];
cout << endl;
cout << " first name: " << firstname[40];
cout << endl;
cout << " id: " << ID[10];
cout << endl;
cout << " gpa: " << GPA;
cout << endl;
cout << " score: " << Score;
cout << endl;
}
void Student::setName(char newFirstname[], char newLastname[])
{
strcpy(lastname, newLastname);
strcpy(firstname, newFirstname);

}
void Student::setID(char newID[])
{
strcpy(ID, newID);

}
int Student::setGPA(double newGPA)
{

if(newGPA >= 0 && newGPA <= 4.0)
{
GPA = newGPA;
return 1;
}
else
{
return 0;
}
}
int Student::setScore(int newScore)
{
if(newScore >= 200 && newScore <= 1600)
{
Score = newScore;
return 1;
}
else
{
return 0;
}
}
char Student::getDecision()
{

if(GPA >= 3.5 || Score > 1300)
{
return 'a';
}
else if(GPA < 2.2 || Score < 900)
{
return 'd';
}
else
{
return 'w';
}
}
The problem is that in your showStudent function, you only print out the 41st element of the name arrays. That's bad because your array only has 40 elements. You cannot print an array by just saying "cout << array_name". You have to manually go and print every element in it one by one. You should use a for loop. Also, you might want to make a function that takes an array and prints it out to save space.
Are you getting anything else as wrong values?
May 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Merrua | last post: by
2 posts views Thread by tasiekk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.