C++ 数组的输出错误 #包括 #包括 #包括 #包括 使用名称空间std; void make_数组(ifstream&num,int&array)[50]); int main(){ ifstream file;//控制文件的变量 char filename[100];///处理调用文件名的操作; int数组[50]; cout>文件名; cout=0;i--){ cout
您的代码向后输出数组,并且在读取完所有值后,它会递增两次C++ 数组的输出错误 #包括 #包括 #包括 #包括 使用名称空间std; void make_数组(ifstream&num,int&array)[50]); int main(){ ifstream file;//控制文件的变量 char filename[100];///处理调用文件名的操作; int数组[50]; cout>文件名; cout=0;i--){ cout,c++,C++,您的代码向后输出数组,并且在读取完所有值后,它会递增两次i。这就是为什么您在开始时会看到两个垃圾值。我怀疑您误报了输出,并且您实际看到了-6438230-293948 4 3 2 1 由于您使用的eof()是错误的,因此最终会得到额外的增量。出于某些原因,这是一个非常常见的错误。请改为按以下方式编写循环: #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream
i
。这就是为什么您在开始时会看到两个垃圾值。我怀疑您误报了输出,并且您实际看到了-6438230-293948 4 3 2 1
由于您使用的eof()
是错误的,因此最终会得到额外的增量。出于某些原因,这是一个非常常见的错误。请改为按以下方式编写循环:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void make_array(ifstream &num, int (&array)[50]);
int main(){
ifstream file; // variable controlling the file
char filename[100]; /// to handle calling the file name;
int array[50];
cout << "Please enter the name of the file you wish to process:";
cin >> filename;
cout << "\n";
file.open(filename);
if(file.fail()){
cout << "The file failed to open.\n";
exit(1);
}
else{
cout << "File Opened Successfully.\n";
}
make_array(file, array);
file.close();
return(0);
}
void make_array(ifstream &num, int (&array)[50]){
int i = 0; // counter variable
while(!num.eof() && i < 50){
num >> array[i];
i = i + 1;
}
for(i; i>=0; i--){
cout << array[i] << "\n";
}
}
要向前输出它们,需要两个变量(一个用于存储数组中的项目总数,另一个用于执行迭代)您的代码向后输出数组,并且在读取完所有值后,它会两次递增
i
。这就是为什么您在开始时会看到两个垃圾值。我怀疑您误报了输出,并且实际看到了-6438230-293948 4 3 2 1
由于您使用的eof()
是错误的,因此最终会得到额外的增量。出于某些原因,这是一个非常常见的错误。请改为按以下方式编写循环:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void make_array(ifstream &num, int (&array)[50]);
int main(){
ifstream file; // variable controlling the file
char filename[100]; /// to handle calling the file name;
int array[50];
cout << "Please enter the name of the file you wish to process:";
cin >> filename;
cout << "\n";
file.open(filename);
if(file.fail()){
cout << "The file failed to open.\n";
exit(1);
}
else{
cout << "File Opened Successfully.\n";
}
make_array(file, array);
file.close();
return(0);
}
void make_array(ifstream &num, int (&array)[50]){
int i = 0; // counter variable
while(!num.eof() && i < 50){
num >> array[i];
i = i + 1;
}
for(i; i>=0; i--){
cout << array[i] << "\n";
}
}
要向前输出它们,需要两个变量(一个用于存储数组中的项目总数,另一个用于执行迭代)您的代码向后输出数组,并且在读取完所有值后,它会两次递增
i
。这就是为什么您在开始时会看到两个垃圾值。我怀疑您误报了输出,并且实际看到了-6438230-293948 4 3 2 1
由于您使用的eof()
是错误的,因此最终会得到额外的增量。出于某些原因,这是一个非常常见的错误。请改为按以下方式编写循环:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void make_array(ifstream &num, int (&array)[50]);
int main(){
ifstream file; // variable controlling the file
char filename[100]; /// to handle calling the file name;
int array[50];
cout << "Please enter the name of the file you wish to process:";
cin >> filename;
cout << "\n";
file.open(filename);
if(file.fail()){
cout << "The file failed to open.\n";
exit(1);
}
else{
cout << "File Opened Successfully.\n";
}
make_array(file, array);
file.close();
return(0);
}
void make_array(ifstream &num, int (&array)[50]){
int i = 0; // counter variable
while(!num.eof() && i < 50){
num >> array[i];
i = i + 1;
}
for(i; i>=0; i--){
cout << array[i] << "\n";
}
}
要向前输出它们,需要两个变量(一个用于存储数组中的项目总数,另一个用于执行迭代)您的代码向后输出数组,并且在读取完所有值后,它会两次递增
i
。这就是为什么您在开始时会看到两个垃圾值。我怀疑您误报了输出,并且实际看到了-6438230-293948 4 3 2 1
由于您使用的eof()
是错误的,因此最终会得到额外的增量。出于某些原因,这是一个非常常见的错误。请改为按以下方式编写循环:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void make_array(ifstream &num, int (&array)[50]);
int main(){
ifstream file; // variable controlling the file
char filename[100]; /// to handle calling the file name;
int array[50];
cout << "Please enter the name of the file you wish to process:";
cin >> filename;
cout << "\n";
file.open(filename);
if(file.fail()){
cout << "The file failed to open.\n";
exit(1);
}
else{
cout << "File Opened Successfully.\n";
}
make_array(file, array);
file.close();
return(0);
}
void make_array(ifstream &num, int (&array)[50]){
int i = 0; // counter variable
while(!num.eof() && i < 50){
num >> array[i];
i = i + 1;
}
for(i; i>=0; i--){
cout << array[i] << "\n";
}
}
要向前输出它们,您需要两个变量(一个用于存储数组中的项目总数,另一个用于执行迭代)检查
!num.eof()
只会告诉您最后一次读取的内容不是eof。因此,如果您的文件是1234
,则检查只会在5
thnum>>array[i]
调用后生效。但是,对于该i
,array[i]
将填充一个无意义的值。处理eof
的唯一正确方法是检查对操作员>>
的每次调用的有效性。换句话说,正确的条件就是num>>数组[i]
。这是通过利用它,从C++11开始到bool
,再到void*
pre-C++11。检查!num.eof()
只会告诉您最后读取的内容不是eof。因此,如果您的文件是1 2 3 4
,那么检查只会在5
thnum>数组[i]之后开始
调用。但是,对于该i
,array[i]
将填充一个无意义的值。处理eof
s的唯一正确方法是检查对操作符>
的每次调用的有效性。换句话说,正确的条件就是num>>array[i]
。这是通过利用它,从C++11开始到bool
,再到void*
pre-C++11。检查!num.eof()
只会告诉您最后读取的内容不是eof。因此,如果您的文件是1 2 3 4
,那么检查只会在5
thnum>数组[i]之后开始
调用。但是,对于该i
,array[i]
将填充一个无意义的值。处理eof
s的唯一正确方法是检查对操作符>
的每次调用的有效性。换句话说,正确的条件就是num>>array[i]
。这是通过利用它,从C++11开始到bool
,再到void*
pre-C++11。检查!num.eof()
只会告诉您最后读取的内容不是eof。因此,如果您的文件是1 2 3 4
,那么检查只会在5
thnum>数组[i]之后开始
调用。但是,对于该i
,array[i]
将填充一个无意义的值。处理eof
s的唯一正确方法是检查对操作符>
的每次调用的有效性。换句话说,正确的条件就是num>>array[i]
。这是通过利用它来实现的,从C++11开始就是bool
,到void*
pre-C++11。是什么阻止人们输入100多个字符的文件名?使用std::string
并停止让缓冲区溢出变得如此容易。在使用数组输入之前,您也不能确保它们成功。您还应该g你的程序一步一步地运行,在这一点上你应该看到这些值的来源。根据条件使用eof
,并正确地解决检查输入是否成功的问题。是什么阻止人们输入100多个字符的文件名?使用std::string
并停止使其如此容易地产生缓冲区在使用数组输入之前,您也不能确保数组输入成功。您应该