如果用户要求重复程序,我该如何清除以前计算的数据?C

How do I clear previous calculated data if user asks to repeat program? C++

本文关键字:清除 计算 数据 何清除 用户 程序 如果      更新时间:2023-10-16

您好,所以我是一个新的编程学生,我正在练习决赛。我知道我的程序有缺陷,但是主要缺陷是当用户重复程序时,新计算将添加到先前的计算中。我将如何纠正?我的代码下面是此任务:

// 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循环的每次迭代中重置sum2sum3sum4变量回到0。这就是为什么它们的值对程序的重复累积。

实际上,您甚至根本没有初始化它们,因此总和无论如何都是随机的垃圾。

输入for循环之前,您需要将它们重置为每个while迭代中的0。

解决方案是将所有代码放置在诸如void userInput();之类的函数中,然后在int main()中具有while循环,该循环调用userInput();,然后 system("CLR");允许您重置。