0

ユーザーがファイルに名前を付けて、そのファイルを開くか作成できるようにするプログラムをコーディングしています。次に、さまざまな方法でそのファイルから情報を読み取ったり、ファイルにさらに情報を書き込んだりできます。問題は、ファイルを読み取るオプションを選択すると、最初にファイルが正常に読み取られ、別の方法で読み取ろうとすると、ファイルを 2 回読み取っているように 2 セットの数値になることです。

問題を 1 つの関数に特定することができず、問題を説明するのが難しいため、プログラム全体を投稿しています。あなたがそれを助けることができればそれは素晴らしいことです. 前もって感謝します。

#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()配列に読み込むように呼び出したにもかかわらず、合計と平均を与える関数がファイル自体を読み取ることに気付きました。1 つの方法を選択して、それに固執することもできます。

于 2013-05-09T01:05:49.160 に答える