尝试将变量值分配给结构元素。起初变量有正确的值,但结构元素有垃圾值

Posted

技术标签:

【中文标题】尝试将变量值分配给结构元素。起初变量有正确的值,但结构元素有垃圾值【英文标题】:Trying to Assign Variable Value to Struct Element. At first variable has right value, but struct element has garbage value 【发布时间】:2018-02-06 07:05:52 【问题描述】:
//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 变量类型,看看这是否会改变输出,但这不起作用。

【问题讨论】:

只标记 c++ 而不是 c。这是两种不同的语言 您开始使用变量pupil 为您的student 数组建立索引,然后稍后(输入名称后),您切换到按等级索引列表。我怀疑您想收集每个学生的成绩,因此您可能需要重新考虑您的方法。考虑一次收集一个学生的所有数据(这意味着一切都在外部 pupil for 循环中)。 你从来没有给realgrd赋值,也许adjust = student[grade].realgrd;是落后的? ideone.com/z5FTPx 使用调试器逐步完成 【参考方案1】:

我认为你把这条线倒过来了

 adjust=student[grade].realgrd;

你是说

student[grade].realgrd = adjust;

【讨论】:

我试过了,效果很好。当我在脑海中读到第一行时,对我来说它说“调整的值分配给第 n 个学生的真实成绩”。当我阅读第 2 行时,对我来说它说“学生 n 的实际成绩被分配调整”。对我来说,这两种说法都合乎逻辑,除非我遗漏了一条微妙的 C/C++ 规则。 程序中的所有其他分配都是右侧分配给左侧。为什么这个会有所不同? 在几乎所有编程语言中(当然在所有类似 c 的语言中)x = y 表示“更改 x 的值,使其现在包含 y”

以上是关于尝试将变量值分配给结构元素。起初变量有正确的值,但结构元素有垃圾值的主要内容,如果未能解决你的问题,请参考以下文章

如何将变量值传递给julia中的宏?

Objective-C,将变量值分配给 plist 字符串

excel中怎样用宏将变量值写到指定单元格中

区分 0 和 ""

如何将变量值与 select 语句的结果一起放入 sql 表中?

如何将变量值“传递”给 JSON 动态对象?