c++ 变量未在范围错误和不匹配类型警告中声明的问题

c++ issue with variables not being declared in scope error and mismatched types warning

我 运行 我的程序有问题。在宏伟的计划中,该程序的基础是从文件中读取有关学生的输入。这包括他们的姓名、身份证、家庭作业分数和考试分数。然后应该计算所有分数并产生字母等级。我目前只在打印姓名和 ID。我的问题是我的所有变量都出错,说它们没有在此范围内声明。我还收到关于我的字符串类型不匹配的警告(不知道那是什么意思)。 not declared in scope 错误示例:

part1.cpp:38:2: error: ‘infile’ was not declared in this scope
  infile.open (out);
  ^
part1.cpp:38:15: error: ‘out’ was not declared in this scope
  infile.open (out);

类型不匹配警告:


part1.cpp:11:15: note:   mismatched types ‘std::basic_ostream<_CharT, _Traits>’ and ‘int’
   outfile <<  name << "  "
               ^
In file included from /usr/include/c++/4.8.2/string:52:0,
                 from /usr/include/c++/4.8.2/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8.2/bits/ios_base.h:41,
                 from /usr/include/c++/4.8.2/ios:42,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from part1.cpp:1:

这里是整体代码:

#include <iostream>
#include <fstream>
#include <string>




void printOutput (std::string name,  std::string Id, float HPfinalscore, float testFinalscore, 
          float overallScore, string LetterGrade, ostream& outfile)
{
  outfile <<  name << "  "
      <<   Id << "  ";
      /*
      << HPfinalscore << "  "
      << testFinalscore << "  "
      << overallScore << "  "
      << LetterGrade;
      */
}



int main()
{
  ofstream outfile;
  ifstream infile; 
  
  int num;
  std::string file_name;
  std::string name; 
  std::string Id;
  std::string LetterGrade;

  float  HPfinalscore = 0, testFinalscore=0, overallScore=0;

    std::cout<<"How many files are you processing: ";
    cin>>num;
    infile.open (out);
    for(int i=0;i<num;i++)
    {
        int count[10];
        for(int i=0;i<10;i++)
            count[i]=0;
          char c,s[1000];
          std::cout<<"Please input the name of the file: ";
          cin>>file_name;
          infile.open(file_name.c_str());
        if ( !infile)
            {
                continue;
                return 0;
            }


            outfile.open("Result");
                if ( !outfile)
                    {
                        std::cout << "Could not open output file \n";
                        return 0;
                    }
    
  

  infile >> name; 
  while(!infile.eof())
    {
      infile >> Id;
      /*
      HPfinalscore = getHPScores(infile); 
      testFinalscore = getTestscores(infile); 
      overallScore = EndingScore(HPfinalscore, testFinalscore); 
      LetterGrade= Grade(overallScore);
    */
    

      printOutput (name, Id, HPfinalscore, testFinalscore, overallScore, 
           LetterGrade, outfile);
      infile >> name; //try to read another name
     
     
    }
  infile.close();
  outfile.close();
  return 0;
}
}

所以我的问题是如何正确声明我的变量?也将不胜感激任何关于警告的信息以及如何处理它。

将 ifstream、ostream 和 ofstream 更改为 std::ifstream、std::ostream 和 std::ofstream。另外,去掉这一行:

infile.open(out);