c++ - 如何使用 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,这是在您在键盘)。忽略它的一种可能的解决方案是使用cin.ignore();
但是您的作业可能是制作一个非常简单的“数据库”,因此您必须有序地记住数据,然后我建议您使用“结构”(这很容易,初学者不难)。
推荐阅读
- r - r将具有模式的列名从宽转换为长
- python - Python:使用正则表达式在大文本中选择文本
- java - 使用 springboot 和 hibernate 从 mysql db 检索的日志数据出现问题
- reactjs - 如何在我的组件中导入资源文件夹以避免“../../../resources/images/..”路径?
- c - 链表分段错误
- d3.js - 如何将文本添加到 d3.js 强制定向圆圈?
- python - 如何在我的程序中同时移动两只海龟?
- python - 无法从 newegg.com 的 price-save-percent 容器中提取文本
- python - python中的2D和3D频谱图
- r - R:阶乘的尾随零数