如何定义变量或稍后在代码中重新计算?
Posted
技术标签:
【中文标题】如何定义变量或稍后在代码中重新计算?【英文标题】:How do I define a variable or recalculate it later in the code? 【发布时间】:2014-09-09 14:20:54 【问题描述】:所以代码将按原样运行,但是当 firstE-fourthE 变量等于 0 时计算总变量和平均变量的计算。肯定有一种方法可以稍后在代码中重新定义它们或重新计算它们吗?对于可怕的格式和缩进,我深表歉意,这个网站非常挑剔。
#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;
【问题讨论】:
看起来您需要做的只是不重新定义变量,而只需通过取消注释计算代码、删除float
声明并用分号分隔来计算(;
) 而不是逗号...
我建议在尝试编写更多代码之前先阅读一些 C++ 教程。 Try this one
从第一个声明块中删除 total
和 average
并取消注释已注释掉的代码。
【参考方案1】:
您为什么要在在之前进行计算,甚至有值可以用来进行计算?你的代码序列应该是:
1. define variables
2. get input from user
3. do calculations
4. present results
您甚至在去商店购买鸡蛋/牛奶/糖之前就已经尝试吃蛋糕了,更不用说混合/烘烤了。
【讨论】:
这是我的问题,我该怎么做?得到用户输入后,我会定义总变量和平均变量吗?我是 C++ 新手,我的母语是 python。而且我的教授的中东口音很浓,有时很难听懂。 好吧,如果您希望“B”出现在“A”之后,那么您就不要编写代码“B->A”。你把它写成“A->B”。 C++(和 Python,以及任何其他你想考虑的语言)不能进行时间旅行。您在计算值之前进行计算。所以重新排列你的代码,这样就不需要时间旅行了。 @MarcB,这不是真的,在 Python 中你可以include TimeTravel
@YourPalAl 你可以像在 Python 中一样做到这一点。变量可以在任何地方声明,只要它是在第一次使用它们之前。 (在函数开头有一个大的 Pascal 风格的“声明块”通常被认为是糟糕的 C++ 风格。)
我想我很困惑,因为我可以在 python 中完全按照你们告诉我的操作,但我忘记了您不必事先预留内存位置,即您可以定义一个变量并提示用户在一行中输入 (firstE= int (input("第一次考试成绩是多少?)")【参考方案2】:
要从您的代码中获得所需的行为,您需要在填充输入后计算总数和平均值。假设成绩是整数,您可以使用int
s 来存储每个考试结果:
#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;
【讨论】:
【参考方案3】:如何定义变量或稍后在代码中重新计算?
像这样
#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';
【讨论】:
以上是关于如何定义变量或稍后在代码中重新计算?的主要内容,如果未能解决你的问题,请参考以下文章