如果用户要求重复程序,我该如何清除以前计算的数据?C
How do I clear previous calculated data if user asks to repeat program? C++
您好,所以我是一个新的编程学生,我正在练习决赛。我知道我的程序有缺陷,但是主要缺陷是当用户重复程序时,新计算将添加到先前的计算中。我将如何纠正?我的代码下面是此任务:
// Andranik Keshishyan, Quiz 2
#include <iostream>
#include <string>
#include <iomanip> //For setw and other formatting
#include <ctime> //For random number generation
using namespace std;
int main()
{
bool repeat = true; //Repeats program if true
srand(time(0)); //Random number generation
double amount_thrown, dice1, dice2, sum1, sum2, sum3, sum4, avg1, avg2, avg3; //Variables
char space = ' '; //Space for Formatting
cout <<"This program will roll 2 dice and calculate their sum and average depending on the amount of throwsn";
cout <<"The amount of throws cannot exceed 12n";
while (repeat){ //Will repeat program if true.
cout <<"How many times would you like the dice thrown?: ";
cin >> amount_thrown;
cout << "n";
if ((amount_thrown < 1 || amount_thrown > 12)||(!(cin>>amount_thrown))){ //Checks to see if user input is valid.
cout <<"This is an invalid input of dice throws.n";
}
else //Continues program if valid.
{
cout <<"Throw" << setw(3) << space <<"Die 1" << setw(3) << space << "Die 2" << setw(3) << space << "Sumn";
for(int x=1; x<=amount_thrown; x++){ //Loops program until amount of throws equals to user input
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
sum1 = dice1+dice2; //Calculates sum of dice1 + dice 2
sum2 += dice1; //Calculates sum of all dice 1 throws
sum3 += dice2; // Calcules sum of all dice 2 throws
sum4 += sum1; // Calculates summ of the sum
avg1 = sum2/2; // Calculates avg of sum of dice 1 throws
avg2 = sum3/2; // Calculates avg of sum of dice 2 throws
avg3 = sum4/2; // Calculates ave of sum of dice 1 + dice 2
cout << setw(2) << x << setw(7) << dice1 << setw(8) << dice2 << setw(9) << sum1 << endl;
}
cout << "-------------------------------------n";
cout << "Sum" << setw(5) << space << sum2 << setw(6) << space << sum3 << setw(6) << space << sum4 << endl;
cout << "Avg" << setw(5) << space << avg1 << setw(5) << space << avg2 << setw(5) << space << avg3 << endl;
}
cout << "nWould you like to roll the dice again?[y=repeat / anything else=stop]: ";
char answer;
cin >> answer;
cin.ignore(1000, 'n'); //Makes sure other inputs are ignored
answer=tolower(answer);
repeat = answer == 'y'; //Program will repeat if user inputs true char y.
}
cout << "Thank you, goodbye" << endl;
return 0;
}
您没有在while
循环的每次迭代中重置sum2
,sum3
和sum4
变量回到0。这就是为什么它们的值对程序的重复累积。
实际上,您甚至根本没有初始化它们,因此总和无论如何都是随机的垃圾。
输入for
循环之前,您需要将它们重置为每个while
迭代中的0。
解决方案是将所有代码放置在诸如void userInput();
之类的函数中,然后在int main()
中具有while
循环,该循环调用userInput();
,然后 system("CLR");
允许您重置。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 计算平均值,不包括上次得分
- 如果用户要求重复程序,我该如何清除以前计算的数据?C