尝试将变量值分配给结构元素。起初变量有正确的值,但结构元素有垃圾值
Trying to Assign Variable Value to Struct Element. At first variable has right value, but struct element has garbage value
//Program tracks 10 students
//Prompts for first and last names
//Collect 4 grades from each student: final exam, 2 quizzes, and class project
//Report will display names w/ individual scores, class average, and highest grade
//Each students individual grade will be calculated with grade weights
/*Grade Weights
-2 Quizzes @ 15% each
-1 Final Exam @ 20%
-1 Class Project @ 50%
*/
#include<iostream>
using namespace std;
int main()
{
struct goodStudent
{
string fname;
string lname;
double exam;
double quiz1;
double quiz2;
double project;
float realgrd; //(sum of ALL grade weights
};
int pupil = 10; //represents # of students in class
goodStudent student[pupil]; //value of 'pupil' subs as value used to define #elements of student[]
//******Prompt for Student Names:
int i;
for (i = 0; i < pupil; i++)
{
cout << "\n Student " << i+1 << " First Name: ";
cin >>student[i].fname;
cout << "\t Student " << i+1 << " Last Name: ";
cin >>student[i].lname;
}
cout <<"--------------------------------";
//*****Grade Collection and Weights Prompt
int grade;
double quizweight,examweight,projweight,cuml;
for (grade = 0; grade < pupil; grade++)
{
cout <<"\n \t Student"<<" "<< grade+1;
cout << "\n Enter Quiz Grade 1: \n";
cin >>student[grade].quiz1;
cout <<"\n Enter Quiz 2 Grade:\n ";
cin >>student[grade].quiz2;
quizweight = ((student[grade].quiz1)+(student[grade].quiz2))*.30;
cout <<"\n Enter Final Exam Grade:\n ";
cin >> student[grade].exam;
examweight =(student[grade].exam)*.20;
cout <<"\n Enter Project Grade:\n ";
cin >>student[grade].project;
projweight =(student[grade].project)*.50;
//MATH GETS JUMBLED UP HERE.IDK had to do ALOT of conversion and revisions.... ;(
cuml = ((projweight)+(examweight)+(quizweight));
double adjust=((cuml/130))*100;//130 is max pts student can earn across all assignments
cout<<adjust;
adjust=student[grade].realgrd;
cout<<"\n\n\n"<<student[grade].realgrd;
/*
FOR SOME REASON PROGRAM DOESN'T LIKE PUTTING ASSIGNED VALUES INTO ARRAY STRUCT..
fix=student[grade].realgrd;
cout<<"\n\nLet's Try it AGAIN: "<<student[grade].realgrd;
cout<<"\nGrade For Student "<<grade+1<<"\tIs A: "<<fix;
*/
system("PAUSE");
}
}
好的。所以我试图为结构数组中的元素赋值。在将值分配给数组中的元素之前,我打印了该值。这是我想要的值,但是当我打印数组以检查是否存在相同的值时,它是一个很长的垃圾数字,如 7.6674323e-309。我什至将结构成员 "realgrd" 更改为 double、float 和 long 变量类型,看看是否会改变输出,但这不起作用。
我认为你把这条线倒过来了
adjust=student[grade].realgrd;
你是说
student[grade].realgrd = adjust;
//Program tracks 10 students
//Prompts for first and last names
//Collect 4 grades from each student: final exam, 2 quizzes, and class project
//Report will display names w/ individual scores, class average, and highest grade
//Each students individual grade will be calculated with grade weights
/*Grade Weights
-2 Quizzes @ 15% each
-1 Final Exam @ 20%
-1 Class Project @ 50%
*/
#include<iostream>
using namespace std;
int main()
{
struct goodStudent
{
string fname;
string lname;
double exam;
double quiz1;
double quiz2;
double project;
float realgrd; //(sum of ALL grade weights
};
int pupil = 10; //represents # of students in class
goodStudent student[pupil]; //value of 'pupil' subs as value used to define #elements of student[]
//******Prompt for Student Names:
int i;
for (i = 0; i < pupil; i++)
{
cout << "\n Student " << i+1 << " First Name: ";
cin >>student[i].fname;
cout << "\t Student " << i+1 << " Last Name: ";
cin >>student[i].lname;
}
cout <<"--------------------------------";
//*****Grade Collection and Weights Prompt
int grade;
double quizweight,examweight,projweight,cuml;
for (grade = 0; grade < pupil; grade++)
{
cout <<"\n \t Student"<<" "<< grade+1;
cout << "\n Enter Quiz Grade 1: \n";
cin >>student[grade].quiz1;
cout <<"\n Enter Quiz 2 Grade:\n ";
cin >>student[grade].quiz2;
quizweight = ((student[grade].quiz1)+(student[grade].quiz2))*.30;
cout <<"\n Enter Final Exam Grade:\n ";
cin >> student[grade].exam;
examweight =(student[grade].exam)*.20;
cout <<"\n Enter Project Grade:\n ";
cin >>student[grade].project;
projweight =(student[grade].project)*.50;
//MATH GETS JUMBLED UP HERE.IDK had to do ALOT of conversion and revisions.... ;(
cuml = ((projweight)+(examweight)+(quizweight));
double adjust=((cuml/130))*100;//130 is max pts student can earn across all assignments
cout<<adjust;
adjust=student[grade].realgrd;
cout<<"\n\n\n"<<student[grade].realgrd;
/*
FOR SOME REASON PROGRAM DOESN'T LIKE PUTTING ASSIGNED VALUES INTO ARRAY STRUCT..
fix=student[grade].realgrd;
cout<<"\n\nLet's Try it AGAIN: "<<student[grade].realgrd;
cout<<"\nGrade For Student "<<grade+1<<"\tIs A: "<<fix;
*/
system("PAUSE");
}
}
好的。所以我试图为结构数组中的元素赋值。在将值分配给数组中的元素之前,我打印了该值。这是我想要的值,但是当我打印数组以检查是否存在相同的值时,它是一个很长的垃圾数字,如 7.6674323e-309。我什至将结构成员 "realgrd" 更改为 double、float 和 long 变量类型,看看是否会改变输出,但这不起作用。
我认为你把这条线倒过来了
adjust=student[grade].realgrd;
你是说
student[grade].realgrd = adjust;