Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;这一直困扰着我_C++_Fstream_Ifstream_Ofstream - Fatal编程技术网

C++ C++;这一直困扰着我

C++ C++;这一直困扰着我,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream,正确的解决方案是什么? 正确的解决方案是什么? 正确的解决方案是什么 我的代码(显然是错误的): 包括和库 int main() { int num = 0; int totalCount = 0; std::ifstream inFile; double average = 0.0; int totalTwo = 0; double total = 0.0; const double VALUE_ONE = 858.5; st

正确的解决方案是什么? 正确的解决方案是什么? 正确的解决方案是什么

我的代码(显然是错误的):

包括

int main() {

    int num = 0;
    int totalCount = 0;
    std::ifstream inFile;
    double average = 0.0;
    int totalTwo = 0;
    double total = 0.0;
    const double VALUE_ONE = 858.5;

    std::cout << "What is the number?  ";
    std::cin >> num;
    std::cout << std::endl;

    inFile.open("numbers.txt");
    while (inFile >> num) {

        totalCount += num;
    }

    total =  num * VALUE_ONE;
    average = total/totalCount;
    totalTwo = total * num;

   

    inFile.close();

    return 0;
}
intmain(){
int num=0;
int totalCount=0;
std::ifstream-infle;
双平均=0.0;
整数totalTwo=0;
双倍合计=0.0;
常数双值_ONE=858.5;
std::cout>num;
std::cout>num){
totalCount+=num;
}
总计=num*值\u一;
平均值=总数/总数;
totalTwo=总计*num;
infle.close();
返回0;
}
number.txt执行此操作时:

std::cin >> num;
std::cout << std::endl;

inFile.open("numbers.txt");
while (inFile >> num) {

    totalCount += num;
}
std::cin>>num;
std::cout>num){
totalCount+=num;
}

您正在读取
num
,然后立即用输入文件中的数据覆盖它。对两个输入使用两个变量。

您的代码实际上与指令中的操作不匹配

将用户选择的乘数读入
num
,然后将文本文件中的数字读入
num
,从而丢失乘数。而且您没有记录从文件中读取的数字数量,这是计算
平均值所需的

您需要向代码中添加更多的变量来分隔内容。此外,根本不需要您的
值\u ONE
常量

尝试类似以下内容:

#包括
#包括
int main()
{
整数乘数=0;
int total=0,TOTALLTWO=0;
整数数量=0;
int num=0;
双平均=0.0;
std::cout>乘法器;
std::cout>num){
总数+=num;
++数量;
}
infle.close();
平均值=双倍(总)/数量;
totalTwo=总计*乘数;

std::cout哪两个输入?第二个输入是什么?您正在从标准输入(
cin
)中读取一个数字,您正在从文件中读取一系列数字,但在使用其中一个之前,您正在将它们存储到同一个变量中。哦,好的。您知道计算(总计、平均和总计二)的数学吗@OzzyTheBossy看看我的答案。