使用指针编译代码后,.cpp文件将变为随机字符



我在一个初学者c++课堂上,我们正在学习指针和指针算术。我们被要求使用一个函数来创建一个新的动态数组,并让该函数输出其指针。然而,每当我进行编译时,它都会将.cpp文件变成一大堆随机字符。我确信有一个简单的解决方案,我只是不知道,但我不知道如何解决这个问题。如有任何帮助,我们将不胜感激。

#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;
int* create_array(int);
int main()
{
int *dyn_array;
int students;
float avrg;
do
{
cout << "How many students will you enter? ";
cin >> students;
}while ( students <= 0 );
dyn_array = create_array( students );
//this function creates a dynamic array
//and returns its pointer
//        enter_data( dyn_array, students );
//use 'pointer' notation in this function to
//access array elements
//accept only numbers 0-100 for movie seen
//        avrg = find_average( dyn_array, students );
//use 'pointer' notation in this function to
//access array elements

//        cout << "The array is:" << endl << endl;
//        show_array( dyn_array, students);
//        cout << endl;
//        cout << "The average is " << avrg << ".n";
delete [] dyn_array;
return 0;
}
int* create_array(int f_students)
{
int *dyn_array = new int[f_students];
return dyn_array;
}

这就是我的.cpp文件在编译后的样子:

@^@^@^@^@^@^@^D^@^@^@^O^@^@^@^C^@^@^@^P^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^F^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^K^@^@^@^@^@^@^@^M^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@N^@^@^@?$
^@^@ě^D^H^G^L^@^@ț^D^H^G^M^@^@̛^D^H^G^O^@^@U??^H?^@^@^@<^A^@^@G^C^@^@?^@5?^D^H%?^D^H^@^@^@^@%?^D^Hh^@^@^@^@???%?^D^Hh^H^@^@^@???%?^D^Hh

我修复了它。我把它编译为c++students_vies.cpp-o students_videos.cpp。最后额外的cpp完全把我搞砸了。非常感谢你指出这可能是我如何编译它的问题。

最新更新