C++ 困惑于从命令行获取文件名并将其转换为C++;

C++ 困惑于从命令行获取文件名并将其转换为C++;,c++,file-io,C++,File Io,我的问题是,我无法从用户的命令行获取文件名,然后使用该文件名写入中值、模式和平均值。IM新的C++,所以任何提示或代码修复将是伟大的,如果你们看到任何其他错误请让我知道,这是我有,IM 99%完成它,只是这个文件写这给了我的问题。多谢各位 #include <iostream> #include <fsteam> #include <string> using namespace std; double Median(int [], int); doubl

我的问题是,我无法从用户的命令行获取文件名,然后使用该文件名写入中值、模式和平均值。IM新的C++,所以任何提示或代码修复将是伟大的,如果你们看到任何其他错误请让我知道,这是我有,IM 99%完成它,只是这个文件写这给了我的问题。多谢各位

#include <iostream>
#include <fsteam>
#include <string>

using namespace std;

double Median(int [], int);
double Average(int [], int);
double Mode(int [], int);

int main(int argc, char *argv[])
{

  ofstream outFile;
  string filename = argv[1];

  outputFile.open(filename.c_str());

  if(!outFile)
  {
    cerr << "Error with the file.";
  }
  else
  {
    continue;
  }


  int *array;
  int array_size;
  cout << "How many students were surveyed? " << endl;
  cin >> array_size;

  if(array_size < 1)
  {
    cout << "Number of students surveyed must be greater than 1." << endl;
    return(1);
  }
  else
  {
    array = new int [array_size];
  }


  cout << "Enter the number of movies each studen saw." << endl;
  for(int i = 0; i < array_size; i++)
  {
      cout << "Student " << i+1 << ": " << endl;
      cin >> array[i];
  }
  for(int i = 0; i < array_size; i++)
  {
    for(int j = i+1; j < array_size-1; j++)
    {
      if(array[i] > array[j])
      {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
  }

  double median = Median(array, array_size);
  double average = Average(array, array_size);
  double mode = Mode(array, array_size);
  outFile << "Median: " << median << endl;
  outFile << "Average: "<< average << endl;
  outFile << "Mode: " << mode << endl;

 return 0;
}

double Median(int arr[], int size)
{
  double middle;
  if(size%2 == 0)
    middle = (arr[size/2] + arr[size/2-1])/2;
  else
    middle = arr[size/2];
  return middle;
}
double Average(int arr[], int size)
{
  double ave = 0;
  for(int i = 0; i < size ; i++)
    ave += arr[i];
  ave = ave/size;
  return ave;
}
double Mode(int arr[], int size)
{
  int count, mode = 0;
  for(int i = 0; i < size; i++)
  {
    count = 1;
    while(arr[i] == arr[i+1])
    {
      count++;
      i++;
    }
    if(count > mode)
      mode = arr[i];
    if(count > 1)
    i--;
  }
  return mode;
}
#包括
#包括
#包括
使用名称空间std;
双中位数(int[],int);
双倍平均值(int[],int);
双模式(int[],int);
int main(int argc,char*argv[])
{
出流孔的直径;
字符串文件名=argv[1];
open(filename.c_str());
如果(!outFile)
{

cerr您可能会在编译器中看到一些关于这方面的信息,但无论如何,我会让您知道
#include

我不明白你为什么要把这个

      else
  {
    continue;
  }
而不是什么都不做,因为
continue;
只是跳到当前迭代的末尾,这里似乎没有必要这样做

剩下的看起来不错。它的格式很容易阅读。如果你在测试后有任何错误,请告诉我


编辑:很抱歉,我还不能添加评论,但作为对您评论的回应,这可能是因为我在上面提到了
。这只是一个输入错误。

我用下面的补丁尝试了它

--- orig.cpp    2014-05-17 12:39:37.000000000 +0800
+++ new.cpp 2014-05-17 12:38:28.000000000 +0800
@@ -1,5 +1,5 @@
 #include <iostream>
-#include <fsteam>
+#include <fstream>
 #include <string>

 using namespace std;
@@ -16,13 +16,13 @@

   outputFile.open(filename.c_str());

-  if(!outFile)
+  if(!outputFile)
   {
     cerr << "Error with the file.";
   }
   else
   {
-    continue;
+//    continue;
   }


@@ -64,9 +64,9 @@
   double median = Median(array, array_size);
   double average = Average(array, array_size);
   double mode = Mode(array, array_size);
-  outFile << "Median: " << median << endl;
-  outFile << "Average: "<< average << endl;
-  outFile << "Mode: " << mode << endl;
+  outputFile << "Median: " << median << endl;
+  outputFile << "Average: "<< average << endl;
+  outputFile << "Mode: " << mode << endl;

  return 0;
 }
——orig.cpp 2014-05-17 12:39:37.000000000+0800
+++new.cpp 2014-05-17 12:38:28.000000000+0800
@@ -1,5 +1,5 @@
#包括
-#包括
+#包括
#包括
使用名称空间std;
@@ -16,13 +16,13 @@
open(filename.c_str());
-如果(!outFile)
+如果(!outputFile)
{

cerr最好检查count命令行参数

    int main(int argc, char *argv[])
    {
    if(argc <2 )
    {
    cout << "Specify out file name as command line argument";
    return 0;

    }
        . . . . . 
        . . . . .

    }
intmain(intargc,char*argv[])
{

if(argc)您给出了什么命令行参数?当您在
输出文件.open
行设置断点时,
argv[1]
的值是多少?换句话说,您得到了什么具体错误?g++-c movie_stats.cpp movie_stats.cpp:2:18:致命错误:fsteam:没有终止此类文件或目录编译。make:**[电影_stats.o]错误1///这是我得到的错误啊,所以你得到了编译时错误?描述听起来好像你有运行时困难。正如用户3210680在他的回答中指出的,你拼写错误了
fstream
。是的,他是正确的,但也感谢你克里斯花时间帮助我。请参考我的补丁。哇!!!我不能相信是这样,所以我做得很好,只是拼错了我不敢相信我没看到。非常感谢,我删除了那个else语句并继续,我还能在那个里放些什么来检查错误?@xiaodongjie把你们包括在内。我并没有注意到
outputFile
有任何不同,但她注意到了。你们有一个打字错误。我没有注意到就是这样。我很惊讶编译器在找不到“fsteam”时没有出现错误在include路径中。但同样适用于continue。在作用域中没有do、while或for。编译器应该有一个中风。是的,我几天前写了它的一部分,我忘记了我将outputFile变量命名为什么,但在被告知fstream拼写错误后,我发现了这些,但感谢您的帮助