如何使用C 文件IO修复程序



我在C 中的文件I/O上有这个编程项目。我已经读过,但还不了解很多。无论如何,我制作了该程序,该程序允许用户将一个人的配置文件(名称,种族等(存储到文本文件中并能够检索它。但是,我正在遇到一些问题的问题。

它在开始时的工作是询问用户是否要查看先前创建的文件或创建新文件。

如果选择了以前的文件,则程序将吐出整个文本文件

如果选择了创建新文件,则程序将要求人数,然后继续要求分配的人数的名称,年龄,性别,物种,种族和运输方式,并将其保存到文本上文件。

问题是我无法设置人数,因为该程序忽略了变量(即我设置了5个人,它忽略了它(该程序忽略了通过跳过它输入的名字。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char names[100];
    char age[100];
    char gender[100];
    char species[100];
    char ethnicity[100];
    char transport[100];
    char decision[0];
    string getcontent;
   cout << "Would You Like to Open The Previous File Or View Your Profiles?" << endl << endl;
   cout << "Enter 1 For Previous File" << endl;
   cout << "Enter Anything Else To View Your Profiles:  " << decision;
   cin.getline(decision,5);
   cout << endl << "==============================================================" << endl;
   if(decision[0] == '1')
   {
       ifstream infile;
       infile.open("File of names.txt");
       while(! infile.eof())//eof stand for end of file
        {
            getline(infile, getcontent); //getline requires a string
            cout << getcontent << endl;
        }
        cout << "==============================================================" << endl;
        infile.close(); //closes the opened file - good practice
   }
   else
   {
       int a;
       cout << "Enter The Amount Of People You Would Like To Store: ";
       cin >> a;
       ofstream namefile;
       namefile.open("File of names.txt");
       cout << "Please Set Your Team Profile." << endl;
       for (int i=0; i<a; i++)
        {
            cout << "==============================================================" << endl;
            cout << "Enter Student " << i+1 << " : ";
            cin.getline(names,100);
            namefile << names << endl;
            cout << "==============================================================" << endl;
            cout << "Enter The Age: ";
            cin.getline(age,100);
            namefile << age << endl;
            cout << "Enter The Gender: ";
            cin.getline(gender,100);
            namefile << gender << endl;
            cout << "Enter The Species: ";
            cin.getline(species,100);
            namefile << species << endl;
            cout << "Enter The Ethnicity: ";
            cin.getline(ethnicity,100);
            namefile << ethnicity << endl;
            cout << "What Is The Mode Of Transport: ";
            cin.getline(transport,100);
            namefile << transport << endl << endl;
        }
namefile.close();

   }
}

这是文件的输出:

Would You Like to Open The Previous File Or View Your Profiles?
Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g
==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1:==============================================================
Enter The Age:

这是预期的输出:

Would You Like to Open The Previous File Or View Your Profiles?
Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g
==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1: John
==============================================================
Enter The Age:

问题是,例如,当您输入类似的值时, string str; cin>>str;然后您插入" John",您不会将" John"分配给Str,而是" John n"(注意 n,在键盘上输入Enter时创建的(。一种可能忽略它的解决方案是使用cin.ignore();但是,您的作业可能是要制作一个非常简单的"数据库",因此您必须以有序的方式记住数据,然后我建议您使用" struct"(对于初学者来说很容易,并不难(。

最新更新