C++ C++&引用;使用未初始化的内存。。(变量名)";
该程序从文本文件“penguins.txt”中读取行,并将数据复制到“FeedingOutput.dat”中。它在另一台电脑上工作正常,但当我在笔记本电脑上运行它时,会出现以下错误:C++ C++&引用;使用未初始化的内存。。(变量名)";,c++,file,memory,file-io,C++,File,Memory,File Io,该程序从文本文件“penguins.txt”中读取行,并将数据复制到“FeedingOutput.dat”中。它在另一台电脑上工作正常,但当我在笔记本电脑上运行它时,会出现以下错误: Using Uninitialized Memory 'zFeeding' Using Uninitialized Memory 'zPercent' Using Uninitialized Memory 'dFeeding' Using Uninitialized Memory 'dPercent' U
Using Uninitialized Memory 'zFeeding'
Using Uninitialized Memory 'zPercent'
Using Uninitialized Memory 'dFeeding'
Using Uninitialized Memory 'dPercent'
Using Uninitialized Memory 'wFeeding'
Using Uninitialized Memory 'wPercent'
文本文件“penguins.txt”如下所示:
Zany A 5 4
Den B 4 8
Windi C 5 2.1
.txt和.dat文件与.cpp文件位于同一目录中
这是我的密码:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double zFeeding; //Declaring variables
double dFeeding;
double wFeeding;
double zPercent;
double dPercent;
double wPercent;
double zFeedingNew;
double dFeedingNew;
double wFeedingNew;
char filename[50];
string zName, dName, wName, zID, dID, wID;
ifstream penguinInfo; //Allows input and output for the two different files
ofstream dataOutput;
cout << "Enter filename containing penguins information" << endl; //Asking for user to input file name, then opening that file
cin.getline(filename, 50);
penguinInfo.open(filename);
dataOutput.open("FeedingOutput.dat");
dataOutput << showpoint << fixed << setprecision(2); ////formating the output
//this will set the information from penguins.txt to actual variables.
penguinInfo >> zName, zID, zFeeding, zPercent, dName, dID, dFeeding, dPercent, wName, wID, wFeeding, wPercent;
zFeedingNew = zFeeding + (zFeeding * (zPercent / 100)); //equations for new feeding amounts
dFeedingNew = dFeeding + (dFeeding * (dPercent / 100));
wFeedingNew = wFeeding + (wFeeding * (wPercent / 100));
dataOutput << zName << " " << zID << " " << zFeedingNew << " lbs." << endl; //Outputs data to FeedingOutput.dat for Zany
dataOutput << dName << " " << dID << " " << dFeedingNew << " lbs." << endl; //Outputs data to FeedingOutput.dat for Den
dataOutput << wName << " " << wID << " " << wFeedingNew << " lbs." << endl; //Outputs data to FeedingOutput.dat for Windi
penguinInfo.close(); //close files and requires approval to close the program
dataOutput.close();
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
双Zfeed;//声明变量
双进料;
双进料;
双倍增长;
双D%;
加倍;
双zFeedingNew;
双D进料新;
双进新;
字符文件名[50];
字符串zName、dName、wName、zID、dID、wID;
ifstream penguinfo;//允许输入和输出两个不同的文件
流数据输出;
不能
根据运算符>>
的优先级高于运算符,
与
(penguinInfo >> zName), zID, zFeeding, zPercent, dName, dID, dFeeding, dPercent, wName, wID, wFeeding, wPercent;
i、 e.只有zName
设置为penguinfo>>zName
你可以把它改成
penguinInfo >> zName >> zID >> zFeeding >> zPercent >> dName >> dID >> dFeeding >> dPercent >> wName >> wID >> wFeeding >> wPercent;
给定
根据运算符>>
的优先级高于运算符,
与
(penguinInfo >> zName), zID, zFeeding, zPercent, dName, dID, dFeeding, dPercent, wName, wID, wFeeding, wPercent;
i、 e.只有zName
设置为penguinfo>>zName
你可以把它改成
penguinInfo >> zName >> zID >> zFeeding >> zPercent >> dName >> dID >> dFeeding >> dPercent >> wName >> wID >> wFeeding >> wPercent;
问题在于,不会像您认为的那样做。它只是丢弃左侧和右侧,并继续使用右侧:
在逗号表达式E1、E2中,表达式E1被求值,其结果被丢弃(尽管如果它具有类类型,则在包含完整表达式的结尾之前不会被销毁),其副作用在表达式E2的求值开始之前完成(请注意,用户定义的运算符不能保证排序)(直到C++17)
最重要的是,它不会用任何东西填充逗号表达式中使用的所有变量。因此,您的变量保持未初始化状态(因为您没有在上面初始化它们)
实际上,您要做的是像链接>
操作符一样链接操作符。问题在于,该操作符不做您认为它做的事情。它只是丢弃左侧和右侧,并继续使用右侧:
在逗号表达式E1、E2中,表达式E1被求值,其结果被丢弃(尽管如果它具有类类型,则在包含完整表达式的结尾之前不会被销毁),其副作用在表达式E2的求值开始之前完成(请注意,用户定义的运算符不能保证排序)(直到C++17)
最重要的是,它不会用任何东西填充逗号表达式中使用的所有变量。因此,您的变量保持未初始化状态(因为您没有在上面初始化它们)
实际上,您要做的是链接>
操作符,就像链接通常代码不应该有水平条一样,使用另一行在企鹅信息上继续>
通常代码不应该有水平条,使用另一行在企鹅信息上继续>