c++ - C++ “Using Uninitialized Memory.. (variable name) ”

标签 c++ file memory file-io

该程序从文本文件“penguins.txt”中读取行,并将数据复制到“FeedingOutput.dat”中。在另一台PC上运行正常,但是在笔记本电脑上运行时出现以下错误:

 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;


}

我相信这可能是一个范围问题,但是我对C++还是很陌生,所以我不确定什么地方出错了。

最佳答案

给定

penguinInfo >> zName, zID, zFeeding, zPercent, dName, dID, dFeeding, dPercent, wName, wID, wFeeding, wPercent;

根据operator precedenceoperator >>的优先级高于operator,,并且与
(penguinInfo >> zName), zID, zFeeding, zPercent, dName, dID, dFeeding, dPercent, wName, wID, wFeeding, wPercent;

即只有zName设置为penguinInfo >> zName

您可以将其更改为
penguinInfo >> zName >> zID >> zFeeding >> zPercent >> dName >> dID >> dFeeding >> dPercent >> wName >> wID >> wFeeding >> wPercent;

关于c++ - C++ “Using Uninitialized Memory.. (variable name) ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59315357/

相关文章:

c++ - 文件流将十六进制文件读入二进制缓冲区

c++ - OOP——让部分了解它们的整体

c++ - VS2013 : Can't create vector from one size_t element

java - .wav 声音无法在 Java 中播放

java - MaxDirectMemorySize 和 sun.misc.unsafe 的任何影响

c++ - std::string 分配策略

c++ - 将 remove_if 与 C 空终止字符串一起使用

java - 删除行后无法将 temp.txt 重命名为original.txt

java - 在 Java 中使用并发在磁盘上创建键值存储

hadoop - 在没有 mapred-site.xml 的情况下设置 hadoop mapreduce 大小