如何定义变量或稍后在代码中重新计算?

时间:2014-09-09 14:20:55

标签: c++ visual-studio-2010 visual-c++

因此,代码将运行,但是当firstE-fourthE变量等于0时,计算total和average变量的计算。当然有一种方法可以在代码中重新定义它们或重新计算它们吗?我为可怕的格式和缩进道歉,这个网站非常挑剔。

#include <iostream>
using namespace std;
int main()
{
    char fi = '\0', mi = '\0', li = '\0', end = '\0';
    float firstE = 0,
        secondE = 0, 
        thirdE = 0, 
        fourthE = 0, 
        total = firstE + secondE + thirdE + fourthE, 
        average = total / 4;

    cout << "This program will calculate the average of a student's exam grades." << endl;
    cout << "Please enter the first initial of the student's name: ";
    cin >> fi;
    cout << "Please enter the middle initial of the student's name: ";
    cin >> mi;
    cout << "Please enter the last initial of the student's name: ";
    cin >> li;
    cout << "Please enter the student's first exam score: ";
    cin >> firstE;
    cout << "Please enter the student's second exam score: ";
    cin >> secondE;
    cout << "Please enter the student's third exam score: ";
    cin >> thirdE;
    cout << "Please enter the student's fourth exam score: ";
    cin >> fourthE;

    /*float total = firstE + secondE + thirdE + fourthE,
      average = total / 4;*/

    cout << "Student's initials: " << fi << mi << li << endl;
    cout << "Exam 1: " << firstE << endl;
    cout << "Exam 2: " << secondE << endl;
    cout << "Exam 3: " << thirdE << endl;
    cout << "Exam 4: " << fourthE << endl;
    cout << "Total: " << total << endl;
    cout << "Average: " << average << endl;
    cin >> end;
}

3 个答案:

答案 0 :(得分:2)

你为什么要进行计算之前你甚至有值进行计算?您的代码序列应为:

1. define variables
2. get input from user
3. do calculations
4. present results

在你去商店购买鸡蛋/牛奶/糖之前,你想吃蛋糕,更不用说混合/烘烤了。

答案 1 :(得分:0)

  

如何在代码中定义变量或重新计算变量?

喜欢这个

#include <iostream>

int main()
{
    int a; // define it once
    int b; // define it once
    int c; // define it once

    int total; // define it once

    a = 2; // change its value
    b = 4;
    c = 1;

    total = a + b + c; // or calculate its value

    std::cout << "total: " << total << '\n';

    a = 9; // change its value AGAIN
    b = 1;
    c = 12;

    total = a + b + c; // and calculate its value AGAIN

    std::cout << "total: " << total << '\n';
}

答案 2 :(得分:0)

要从代码中获得所需的行为,您需要计算输入后的总数和平均值。假设成绩是整数,您可以使用int来存储每个考试成绩:

#include <iostream>

int main()
{
    char fi = '\0', mi = '\0', li = '\0', end = '\0';
    int firstE = 0,
        secondE = 0, 
        thirdE = 0, 
        fourthE = 0, 

    /*IO*/

    float average = (firstE + secondE + thirdE + fourthE)/4;

    /*IO*/

    return 0;
}

这将为您提供工作代码。

我们可以进一步清理它。为什么我们无法将John Edward Smith的首字母存储为"JES"而不是单独输入每个字符?

#include <iostream>
#include <string>

int main()
{
    std::string student_initials;
    int firstE = 0,
        secondE = 0, 
        thirdE = 0, 
        fourthE = 0, 

    std::cout << "This program will calculate the average of a student's exam grades." << endl;
    std::cout << "Please enter the student's initials: ";
    std::cin >> student_initials;
    /*MORE IO*/

    float average = (firstE + secondE + thirdE + fourthE)/4;

    std::cout << "Student's initials: " << student_initials << std::endl;
    /*MORE IO*/

    return 0;
}

更好的是,用户界面更简单一些。但是,如果学生参加额外的考试怎么办?如果他们只拿3个怎么办?您要么无法输入所有结果,要么数学错误。 我们可以为可以处理结果输入的代码引入一个循环:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    /* STUFF */

    int total;
    int exam_result;
    int exam_count = 1;
    string input;

    //this loop will capture multiple exam results (safely)
    do
    {
        std::cout << "Please enter the results of exam #" << exam_count \
                  << "\nleave blank if all results have been entered" << std::endl;
        std::getline (std::cin, input);
        stringstream(input) >> exam_result;
        if (exam_result)
        {
            total += exam_result;
            exam_count++;
        }
    } while (exam_result);

    float average = total/exam_count;

    /* STUFF */

    return 0;
}

这使用一种处理cin的安全方法捕获多个检查结果(以及总数和计数) 但是它会阻止您在平均值之前打印出每个单独的结果,但是您可以将它们存储在std::vector中并迭代它们来执行此操作。我会把它留给你。

最终代码:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string student_initials;

    std::cout << "This program will calculate the average of a student's exam grades." << endl;
    std::cout << "Please enter the student's initials: ";
    std::cin >> student_initials;
    int total;
    int exam_result;
    int exam_count = 1;
    string input;

    //this loop will capture multiple exam results (safely)
    do
    {
        std::cout << "Please enter the results of exam #" << exam_count \
                  << "\nleave blank if all results have been entered" << std::endl;
        std::getline (std::cin, input);
        stringstream(input) >> exam_result;
        if (exam_result)
        {
            total += exam_result;
            exam_count++;
        }
    } while (exam_result);

    float average = total/exam_count;

    std::cout << "Student's initials: " << student_initials << std::endl;
    cout << "Total: " << total << endl;
    cout << "Average: " << average << endl;

    return 0;
}