c++ while循环读取输入
file io - C++ while loop reading input
我提交了这个程序,它工作得很好,但是我的老师说我的while
循环有问题,即使我得到了正确的答案。有什么建议或帮助吗?
当到达文件结束并且第43行上的read
无效时,在while
循环中会发生什么?你的程序是结构化的,你看不到问题,但问题确实存在。应该重新构建while
循环来考虑这一点。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream inputfile;
char choice;
int NumberOfIntegers = 0,
SumOfIntegers = 0,
Average = 0 ,
LargestValue,
SmallestValue,
integer;
inputfile.open("random.txt");
if(!inputfile)
{
cout << "the file could not be open" << endl;
}
inputfile >> integer;
//initialize smallest and largest
SmallestValue = integer;
LargestValue = integer;
while(inputfile)
{
NumberOfIntegers++;
SumOfIntegers = SumOfIntegers + integer;
inputfile >> integer;
if( integer > LargestValue || integer < SmallestValue)
{
if ( integer > LargestValue)
LargestValue = integer;
else
SmallestValue = integer;
}
}
if(NumberOfIntegers > 0 )
{
Average = SumOfIntegers / NumberOfIntegers;
}
//closing input file
inputfile.close();
do
{
//Display Menu
cout << "Make a selection from the list" << endl;
cout << "A. Get the largest Value" << endl;
cout << "B. Get the smallest Value" << endl;
cout << "C. Get the sum of the values" << endl;
cout << "D. Get the average of the values" << endl;
cout << "E. Get the number of values entered" << endl;
cout << "F. End this program" << endl << endl;
cout << "Enter your choice --> ";
cin >> choice;
cout << endl;
switch (choice)
{
case 'a':
case 'A': cout << "The largest value is " << LargestValue << endl;
break;
case 'b':
case 'B': cout << "The smallest value is " << SmallestValue << endl;
break;
case 'c':
case 'C': cout << "The sum of the values entered is " << SumOfIntegers << endl;
break;
case 'd':
case 'D': cout << "The average of the values entered is " << Average << endl;
break;
case 'e':
case 'E': cout << "The number of values entered is " << NumberOfIntegers << endl;
break;
case 'f':
case 'F': cout << "Program ending" << endl << endl;
cin.ignore();
cout << "nnPress Enter to end --> ";
cin.ignore();
return 0;
default:
cout << choice << " is an invalid value. " << endl;
}
cout << endl;
} while( choice != 'f' || choice != 'F');
return 0;
}
我看到的"问题"是您在读取之后和处理之前不检查流的bool
值。要做到这一点,应该将read作为while循环的条件。
if( ! inputfile >> integer )
{
// error code (no integer in file)
exit(0);
}
LargestValue = integer;
SmallestValue = integer;
NumberOfIntegers++;
SumOfIntegers = SumOfIntegers + integer;
while( inputfile >> integer )
{
NumberOfIntegers++;
SumOfIntegers = SumOfIntegers + integer;
//inputfile >> integer;
if( integer > LargestValue || integer < SmallestValue)
{
if ( integer > LargestValue)
LargestValue = integer;
else
SmallestValue = integer;
}
}
在这种情况下,结果应该与您的程序相同,因为如果inputfile >> integer
失败,我相信integer
保持与以前相同的值,因此它不会影响LargestValue
或SmallestValue
。然后检查流,使NumberOfIntegers
和SumOfIntegers
不会更新,这是正确的。程序给出未定义结果(对于LargestValue
和SmallestValue
)的唯一情况是,如果文件不是以整数开头,只需检查第一次读取并适当处理它,这将被修复。
相关文章:
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 套接字读取后,我在缓冲区中看到意外输入
- 从标准输入读取文件后读取用户输入
- C++ 从标准输入读取文件名
- 从标准输入读取多个类型时出现问题
- 使用 fgets() 从键盘读取时从以前的输入读取换行符
- 从标准输入读取时zmq_poll不侦听信号
- 从超强的AndroidAudioIO输入读取缓冲区
- 如何实现将用户输入读取到提供的所有变量中的可变参数模板
- 如何使用重定向的标准输入读取文件,即cin(注意:不使用ifstream)
- 将空格分隔的输入读取到C++中的数组中
- Win32 - 从标准输入读取超时
- 从标准输入读取输入,并准备接收来自服务器的广播
- 终止将输入读取到int中的内容
- 如何从标准输入读取输入,直到按下空格或选项卡
- DirectX 11输入读取方法
- 将输入读取到动态大小的数组中