0

我正在编写一个程序,该程序允许用户命名文件并打开或创建该文件。然后他们能够以不同的方式从该文件中读取信息或将更多信息写入文件。问题是,当我选择读取文件的选项时,它首先读取它很好,然后当我尝试以不同的方式读取它时,它有两组数字,就好像它正在读取文件两次一样。

我无法将问题定位到任何一个功能,而且很难解释问题,所以我发布了整个程序。如果您能提供帮助,那就太好了。先感谢您。

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

int main(), f1(), f2(), f3(), f4(), f5(), f6(), f7();
string file;
const int NUM_DATA = 50;
double data[NUM_DATA], temp;
int num = 0;

int main()
{
    int choice;
    do
    {
        system ("cls");
        cout << " ** MENU ** \n\n";
        cout << "Current Data File: " << file << "\n\n";
        cout << "(1) Select/Create data file (.txt file extension will be added           automatically)\n";
        cout << "(2) Display all the numbers, total and average\n";
        cout << "(3) Display all the numbers from smallest to the largest\n";
        cout << "(4) Select a number and display how many times it shows up\n";
        cout << "(5) Display the largest number\n";
        cout << "(6) Append random number(s)\n";
        cout << "(7) Exit the program\n\n";

        do 
        {
            cout << "Choice: ";
            cin >> choice;
            cout << endl;
        } 
        while (choice < 1 || choice > 7);


        switch (choice)
        { 
           case 1: f1();
              break;
           case 2: f2();
              break;
           case 3: f3();
              break;
           case 4: f4();
              break;
           case 5: f5();
              break;
           case 6: f6();
              break;
       }
   }
   while (choice != 7);

   return 0;
}
// Select file
int f1()
{
   cout << "Name of data file: ";
   cin >> file;
   file = file + ".txt";
   ifstream fileI;
   fileI.open(file.c_str());

   if(!fileI)
   {
       cout << "\nFile not found, creating file. \n\n";
       ofstream fileO;
       fileO.open(file.c_str());
       fileO.close();
   }
   else
   {
       cout << "\nFile successfully read. \n\n";
       fileI.close();
   }
   system("pause");
   return 0;
}
// Display all the numbers, the total and the average
int f2()
{
   f7();
   ifstream fileI;
   fileI.open(file);
   double total = 0;
   double average;

    for (int count = 0; count < num; count++)
    {
        total += data[count];
        cout << data[count] << endl;
    }
    average = total / num;
    cout << "Total: " << total << endl;
    cout << "Avearage: " << average << "\n\n";

    fileI.close();

    system("pause");
    cin.ignore();
    return 0;
}
// Display all the numbers from smallest to largest
int f3()
{
    f7();
   ifstream fileI;
   fileI.open(file);
   for(int i = 0; i < num; i++)
       {
           for(int j = 0; j < num; j++)
           {
               if(data[i] < data[j])
               {
                   int temp = data[i];
                   data[i] = data[j];
                   data[j] = temp;
               } 
           }
       }

for (int count = 0; count < num; count++)
{
        cout << data[count] << "\n\n";
}
fileI.close();
system("pause");
return 0;
}
// Display how many times a number shows up
int f4()
{
   f7();
   ifstream fileI;
   fileI.open(file);
   int numb, times = 0;

   cout << "Search number: ";
   cin >> numb;
   cout << endl;

   for (int count = 0; count < num; count++) 
   {
        if (numb == data[count])
        {   
           times ++;
        }
   }
   cout << numb << " ocurrs " << times << " times." << "\n\n";
   fileI.close();
   system("pause");
   return 0;
}
// Display the largest number
int f5()
{
   f7();
   ifstream fileI;
   fileI.open(file);
   int large = data[0];
   for (int count = 0; count < num; count++)
   {
       if (large < data[count])
       {
            large = data[count];
       }
   }
   cout << "Larget number: " << large << "\n\n";
   fileI.close();
   system("pause");
   return 0;
}
// Append one or more random numbers
int f6()
{
   ofstream fileO;
   fileO.open(file, ios::app);
   int rndm, numbs;
   srand(time(NULL));
   cout << "Add how many numbers: ";
   cin >> rndm;
   cout << endl;

   for (int count = num + 1; count <= num + rndm ; count++)
   {
        numbs = (rand()%50+1);
        fileO << numbs << endl;
   }
   cout << "Data succesfully written.\n\n";
   fileO.close();
   system("pause");
   return 0;
  }
//Array function
int f7()
{
   ifstream fileI;
   fileI.open(file);
   fileI >> temp;


   while (num < NUM_DATA && !fileI.eof())
   {
       data[num] = temp;
       ++num;
       fileI >> temp;
   }
   fileI.close();
   return num;
}
4

1 回答 1

2

这是因为,在 中f7(),您使用num它的最后一个已知值,而不是重置它。

这意味着您每次只需将文件的另一个副本附加到数组的末尾。

并且,如果您重视可能必须维护您的代码的人的理智,请不要使用像这样的函数名称fN(),它们应该是可读的:-)


有趣的是,这只影响使用数组的函数。f7()我注意到,尽管调用将其读入数组,但为您提供总和平均值的函数会读取文件本身。您可能想选择一种方法并坚持下去。

于 2013-05-09T01:05:49.160 回答