[英]How can i fix my program using c++ file 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”(它很容易,對初學者來說並不難)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.