输入的数据没有写入文件



我一直在试图找到错误,但代码仍然不起作用,我使用结构函数和文件来保存所有输入的数据,但是当我运行程序时,我发现从选项1 (case 1/void输入)输入的数据没有写入parking3文件。由于数据没有写入文件,因此从parking3文件读取数据的选项2和3无法工作,我不确定如何修复它,但这里是完整的代码,以便更好地理解:

#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream>
#include<cstdlib>
#include<process.h>
struct car
{
//public member function can access any private, protected and public member of it's class//
string VehicleNo; 
string area[4][4] = {{"A1","A2","A3","A4"},{"B1","B2","B3","B4"},{"C1","C2","C3","C4"},{"D1","D2","D3","D4"}};
float Hours;
char  DriverName[30],Time[50];
int VIP,NONVIP;
};
car Parking [10];
void input() //fetch data needed from the user
{ 

system("cls"); //to clear the screen
int i;
ofstream outfile;
cout<<"nnttEnter the name of driver : ";
cin>>Parking[i].DriverName;
cout<<"nnttEnter the car no : ";
cin>>Parking[i].VehicleNo;
cout<<"nnttEnter the no of hours of stay : ";
cin>>Parking[i].Hours;
cout<<"nnttParking area available : "<<endl;
cout<<"nntt_________________________________n";
cout<<"tt| Area  |  A  |  B  |  C  |  D  |n";
cout<<"tt|_______|_____|_____|_____|_____|n";
cout<<"tt| floor |  1  |  1  |  1  |  1  |n";
cout<<"tt|       |  2  |  2  |  2  |  2  |n";
cout<<"tt|       |  3  |  3  |  3  |  3  |n";
cout<<"tt|       |  4  |  4  |  4  |  4  |n";
cout<<"tt|_______|_____|_____|_____|_____|"<<endl<<endl<<endl;
cout<<"Enter parking area you want to park : ";
cin>>Parking[i].area[i][i];  

if(Parking[i].Hours<15)
{ 
if(Parking[i].area[i][i] =="A1" || Parking[i].area[i][i] =="A2"|| Parking[i].area[i][i] =="A3" || Parking[i].area[i][i] =="A4")
{
cout<<"nnttParking slot available. You can park your car.";
}

else if (Parking[i].area[i][i]=="B1"|| Parking[i].area[i][i]=="B2" || Parking[i].area[i][i]=="B3" || Parking[i].area[i][i]=="B4")
{

cout<<"nnttParking slot available. You can park your car.";
}

else if (Parking[i].area[i][i]=="C1"|| Parking[i].area[i][i]=="C2" || Parking[i].area[i][i]=="C3" || Parking[i].area[i][i]=="C4")
{

cout<<"nnttParking slot available. You can park your car.";
}

else if (Parking[i].area[i][i]=="D1"|| Parking[i].area[i][i]=="D2" || Parking[i].area[i][i]=="D3" || Parking[i].area[i][i]=="D4")
{

cout<<"nnttParking not available. Please try another slot";
}

}

else 
cout<<"Reached time limit for workers parking time"<<endl;


}
void calculate() //function to calculate parking fees
{
int i;  
cout<<"ntntAre you registered as a V.I.P?: (y/n) ";
cin>>Parking[i].VIP;
if(Parking[i].VIP=='y')
{
system("cls");
Parking[i].VIP=Parking[i].Hours*20;
cout<<"ntnt[VIP] The total of parking fees: ";
cout<<"RM "<<Parking[i].VIP; //calculate payment for vip member
}
else 
{
system("cls");
Parking[i].NONVIP=Parking[i].Hours*25;
cout<<"ntnt[NON-VIP] The total of parking fees: ";
cout<<"RM "<<Parking[i].NONVIP; //calculate payment for non-vip member

}
}   
void output() //details statement to the user
{
int i;
cout<<"nntThe Car position in parking lot : "<<Parking[i].area[i][i]<<endl;
cout<<"nnttThe Driver Name : "<<Parking[i].DriverName<<endl;
cout<<"nnttThe Car No : "<<Parking[i].VehicleNo<<endl;
cout<<"nnttThe Hours Of Stay : " <<Parking[i].Hours<<endl;
}

int main() // main function for the menu 
{
int login();
login(); // using login function
int choice,i;
while(1) 
{
system("cls"); //clear the screen
//printing menu page statement
cout<<"tt                  _________________n";
cout<<"tt              _.-'_____  _________ _`.n";
cout<<"tt            .` ,'      ||         | `.`.n";
cout<<"tt          .` ,'        ||         |   `.`.n";
cout<<"tt        .`  /          ||         |  ,' ] `....___n";
cout<<"tt      _`__.'''''''''''''''''''''''`''''''''|..___ `-.._n";
cout<<"tt    .'                  [='                '     `'-.._`.n";
cout<<"tt ,:/.'''''''''''''''''''||'''''''''''''''''||''''''''''''' ";
cout<<"ttn  //||    _..._         ||                 ||   _..._  |)| n";
cout<<"tt /|//   ,',---.`.       ||                 || .',---.`, |";
cout<<"ttn(':/   //' .-. `\      \_________________////' .-. `\ |_)";
cout<<"ttn `-...'||  '-'  ||________,,,,,,,,,,,,,,,__.'||  '-'  ||-' n";
cout<<"tt       '.'.___.','                           '.'.___.',' n";
cout<<"tt         '-.m.-'                               '-.m.-'n";

cout << "nntt ====== CAR PARKING RESERVATION SYSTEM ======";
cout <<"nn                                          ";
cout<<" nttt======================";
cout << "nnttt  1. Arrival of a Car"; //input new data from new car
cout << "nnttt  2. Total no of cars Arrived"; //view total cars arrived
cout << "nnttt  3. Total parking charges of all cars with details"; //view payment data for all car
cout << "nnttt  4. Exit Program"; //exit the program
cout<<" nttt======================";
cout << "nn";
cout << "ttt Select Your Choice ::"; //admin input choice either 1,2,3,4 or 5
cin>>choice;
switch(choice) //using switch structure
{
case 1 : //choice 1: car arrival
{       
char proceed;
cout<<"nDo You Want to continue==Yes(y)/No(n) ";
cin>>proceed;
while(proceed=='y')
{   
ofstream outfile;
outfile.open("parking3.txt");
outfile<<"CAR PARKING SYSTEMn";
input();
outfile<<"nntThe Car position in parking lot : "<<Parking[i].area[i][i]<<endl;
outfile<<"nnttThe Driver Name : "<<Parking[i].DriverName<<endl;
outfile<<"nnttThe Car No : "<<Parking[i].VehicleNo<<endl;
outfile<<"nnttThe Hours Of Stay : " <<Parking[i].Hours<<endl;
outfile.close(); 

}

}

case 2 : //choice 2: viewing total car arrived
{
system("cls");
cout << "nntt== Viewing Record in the Parking Database ==";
cout << "n";
int record;
cout<<"How many record you want to see?";
cin>>record;
ifstream infile; //reading data from the file
infile.open("parking3.txt");
for(int i=0;i<record;i++)
{
system("cls");
cout << "n";
infile>>Parking[i].area[i][i];
infile>>Parking[i].DriverName;
infile>>Parking[i].VehicleNo[i];
infile>>Parking[i].Hours;
output(); // calling function output to print statement
system("PAUSE");
cout<<endl;


}
infile.close();

}   

case 3: //choice 3: details of parking charges of all cars
{
ifstream infile; //reading from the file
infile.open("parking3.txt");
int record;
cout<<"How many record you want to see?";
cin>>record;
for(int i=0;i<record;i++)
{
cout << "n";
calculate(); //call function calculate
output(); //call function output
cout<<"n";
cout<<endl;     
}
infile.close();

}

case 4: //exit the program
{
system("CLS");
exit(0);
break;
}

default : //default option if admin choose option other than 1,2,3,4 or 5
{
cout<<"nntt Invalid input";
cout<<"nnttPress Enter to continue";
}

}
getch();


}
}

请帮我修理一下

你将不得不扔掉这段代码,它是不可修复的。这里有一个例子(这个错误似乎重复了多次),来自函数input

int i;
ofstream outfile;
cout<<"nnttEnter the name of driver : ";
cin>>Parking[i].DriverName;

当你在cin>>Parking[i].DriverName;行中使用变量i时,你认为它的值在这里是什么?答案是没有。这对你的程序有什么影响?可能会崩溃

现在你已经写了很多代码,但似乎没有一个是有效的。这可能只是因为您编写了所有代码而没有进行任何测试。这是错误的软件编写方式。编写一些代码(实际上是几行),测试它们,直到您确信它们可以工作,然后再编写几行代码。这是编写软件的唯一方法。在没有任何测试的情况下编写大量代码总是会失败,如果你是一个初学者,你会不知道哪里出了问题。

相关内容

  • 没有找到相关文章

最新更新