【发布时间】:2018-09-12 04:31:03
【问题描述】:
这让我非常头疼,希望我能找到一些帮助。该程序应该读取 19 个整数的程序,然后将最小的(第 2 个整数)和最大的(第 5 个整数)输出到屏幕上。但是,我所有的结果都会产生垃圾。
#include iostream>
#include <fstream>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
//the goal of this program is to read in numbers from a file, then output the
//highest number and the lowest number to the screen
int main() {
ifstream fileInput;
int nOne, nTwo, nThree, nFour, nFive, nSix, nSeven, nEight, nNine, nTen, //there are 19 numbers in the file
nEleven, nTwelve, nThirteen, nFourteen, nFifteen, nSixteen, nSeventeen,
nEighteen, nNineteen;
cout << "Opening File" << endl;
fileInput.open("Lab12A.txt"); //the file is opened
if (fileInput.fail())
{
cout << "Input file opening failed. \n"; //the fail check doesnt pop up, so the file has been opened.
exit(1);
}
fileInput >> nOne >> nTwo >> nThree >> nFour >> nFive >> nSix >> nSeven >> nEight
>> nNine >> nTen >> nEleven >> nTwelve >> nThirteen >> nFourteen >> nFifteen //this is where they should be extracted
>> nSixteen >> nSeventeen >> nEighteen >> nNineteen;
cout << "The highest number is " << nTwo << endl;
cout << "The lowest number is " << nFive << endl;
fileInput.close();
system("pause");
return 0;
}
【问题讨论】:
-
这不是您编写代码来解决问题的方式...您了解数组了吗?
-
第一行的错字。
-
你得到了什么输出?
-
输入文件的格式和准确的输出是什么?我无法用空格分隔的输入数字重现这个。
-
随便猜测一下,您用来创建
Lab12A.txt的文本文件编辑器是否有可能在文件中插入了Unicode BOM 标记?然后,这可能导致标准库在尝试读取第一个数字时将输入流置于错误模式。
标签: c++ fileoutputstream garbage