C++ 作业编译错误与 if 语句

标签 c++

在第二个 if 语句中遇到问题,只能读取十个值。如果我删除程序将编译。我在作业中需要它我已经尝试了几次修改,所以这里的内容可能无法反射(reflect)我最后的尝试。 C++ 作业编译错误与 if 语句。详细说明在发布之前我还需要添加多少详细信息,更多详细信息

    #include <iostream>
    #include <fstream>
    using namespace std;

    /**********************************************************************
    * Get File
    * This function will prompt the user for the name of the file and
    * return it.
    *********************************************************************/

    void getFileName(char fileName[])
    {
        //prompt the user
        cout << "Please enter the filename: ";
        cin >> filename;

        ofstream fout(fileName);
    }

    /**********************************************************************
    * Read
    * This function will read the file and return the average score of the
    * ten values.
    *********************************************************************/

     float readFile(char fileName[])
     {
         cout.setf(ios::fixed);  //no scientific notation
         cout.precision(0);      //no decimals

         //In case of error
         ifstream fin(filename);
         if (fin.fail())
         {
             cout << "Error reading file \"" << fileName << "\"" << endl;
             exit(0);
         }

         float data;          //always need a variable to store the data
         float i = 0;         //don't forget to initialize the variable
         float gradeSum = 0;  
         float average;

         //Add up all the grades
         while (fin >> data)   //while there was not an error
         {
              gradeSum += data; //do the work
              i++;
         }

         // Hint: Make sure you only read ten values.
         if (i != 10)
         {
             //tell the user what happened
             cout < "Error reading file \"" << fileName << "\"" << endl;
             exit(0);
         }

         //Calculate average grade
         average = gradeSum / i;

         //Close file
         fin.close();

         return average;
     }

    /**********************************************************************
    * Display function
    * This function will display the average score to zero decimals of
    * accuracy.
    *********************************************************************/
    void display(float average)
    {
       cout << "Average Grade: " << average << "%" << endl; 
    }

    /**********************************************************************
    * main calls average grade and file name
    ***********************************************************************/
    int main()
    {
       char fileName[256];
       getFileName(fileName);

       int average = readFile(fileName);
       return 0;
    }

最佳答案

注意 if 语句相对于 while 循环的位置。你的 if 语句什么时候运行?你的 while 循环什么时候运行?

考虑到这些,您将如何调整 while 循环以考虑您的限制(while 循环在 i = 10 时停止执行)?

我希望这可以帮助您更好地理解您需要做什么,而不会破坏太多答案。

附言如果 i != 10,if block 的内容将立即退出程序,而不执行任何超出的内容。

if (i != 10)
     {
         //tell the user what happened
         cout < "Error reading file \"" << fileName << "\"" << endl;
         exit(0);
     }

关于C++ 作业编译错误与 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50673293/

相关文章:

c++ - gluPerspective()/glm::perspective() 中的近/远剪裁平面如何工作?

c++ - C++ 中的图形表示

c++ - 在 OO 编程中,继承对运行时有哪些负面影响?

c++ - Arduino XBee 发送数据 API

c++ - 使用 C++ 在 Arduino 上编写复杂的程序。如何正确使用对象或更好地使用普通 C?

c++ - Vulkan vkCreateWin32SurfaceKHR 加载失败

c++ - CMake 共享(或静态?)库无法链接

c++ - Hinnant 的 short_alloc 只在栈上

c++ - 将 1000x1000 复数矩阵的求逆从 Matlab 移植到 C++

c++ - 使用 QNetworkAccessManager->Post() 导致 SEGV 关闭应用程序