簡體   English   中英

輸入的數據沒有寫入文件

[英]The inputted data is not written to the file

我一直在嘗試查找錯誤,但代碼仍然無法正常工作,我正在使用 struct function 和文件來保存所有輸入的數據,但是當我運行程序時,我發現選項 1 中的輸入數據(案例1/void 輸入)沒有寫入parking3 文件。 由於數據未寫入文件,因此用於從 park3 文件讀取數據的選項 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<<"\n\n\t\tEnter the name of driver: "; cin>>Parking[i].DriverName; cout<<"\n\n\t\tEnter the car no: "; cin>>Parking[i].VehicleNo; cout<<"\n\n\t\tEnter the no of hours of stay: "; cin>>Parking[i].Hours; cout<<"\n\n\t\tParking area available: "<<endl; cout<<"\n\n\t\t_________________________________\n"; cout<<"\t\t| Area | A | B | C | D |\n"; cout<<"\t\t|_______|_____|_____|_____|_____|\n"; cout<<"\t\t| floor | 1 | 1 | 1 | 1 |\n"; cout<<"\t\t| | 2 | 2 | 2 | 2 |\n"; cout<<"\t\t| | 3 | 3 | 3 | 3 |\n"; cout<<"\t\t| | 4 | 4 | 4 | 4 |\n"; cout<<"\t\t|_______|_____|_____|_____|_____|"<<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<<"\n\n\t\tParking 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<<"\n\n\t\tParking 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<<"\n\n\t\tParking 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<<"\n\n\t\tParking 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<<"\n\t\n\tAre you registered as a VIP?: (y/n) "; cin>>Parking[i].VIP; if(Parking[i].VIP=='y') { system("cls"); Parking[i].VIP=Parking[i].Hours*20; cout<<"\n\t\n\t[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<<"\n\t\n\t[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<<"\n\n\tThe Car position in parking lot: "<<Parking[i].area[i][i]<<endl; cout<<"\n\n\t\tThe Driver Name: "<<Parking[i].DriverName<<endl; cout<<"\n\n\t\tThe Car No: "<<Parking[i].VehicleNo<<endl; cout<<"\n\n\t\tThe 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<<"\t\t _________________\n"; cout<<"\t\t _.-'_____ _________ _`.\n"; cout<<"\t\t.`,' || | `.`.\n"; cout<<"\t\t.`,' || | `.`.\n"; cout<<"\t\t.` / || |,' ] `....___\n"; cout<<"\t\t _`__.'''''''''''''''''''''''`''''''''|..___ `-.._\n"; cout<<"\t\t.' [=' ' `'-.._`.\n"; cout<<"\t\t,:/.'''''''''''''''''''||'''''''''''''''''||''''''''''''\' "; cout<<"\t\t\n //|| _..._ || || _..._ |)| \n"; cout<<"\t\t /|//,',---.`. || ||.',---.`, |"; cout<<"\t\t\n(':/ //'.-. `\\ \\_________________////'.-. `\\ |_)"; cout<<"\t\t\n `-...'|| '-' ||________,,,,,,,,,,,,,,,__.'|| '-' ||-' \n"; cout<<"\t\t '.'.___.',' '.'.___.',' \n"; cout<<"\t\t '-.m.-' '-.m.-'\n"; cout << "\n\n\t\t ====== CAR PARKING RESERVATION SYSTEM ======"; cout <<"\n\n "; cout<<" \n\t\t\t======================"; cout << "\n\n\t\t\t 1. Arrival of a Car"; //input new data from new car cout << "\n\n\t\t\t 2. Total no of cars Arrived"; //view total cars arrived cout << "\n\n\t\t\t 3. Total parking charges of all cars with details"; //view payment data for all car cout << "\n\n\t\t\t 4. Exit Program"; //exit the program cout<<" \n\t\t\t======================"; cout << "\n\n"; cout << "\t\t\t 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 SYSTEM\n"; input(); outfile<<"\n\n\tThe Car position in parking lot: "<<Parking[i].area[i][i]<<endl; outfile<<"\n\n\t\tThe Driver Name: "<<Parking[i].DriverName<<endl; outfile<<"\n\n\t\tThe Car No: "<<Parking[i].VehicleNo<<endl; outfile<<"\n\n\t\tThe Hours Of Stay: " <<Parking[i].Hours<<endl; outfile.close(); } } case 2: //choice 2: viewing total car arrived { system("cls"); cout << "\n\n\t\t== 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<<"\n\n\t\t Invalid input"; cout<<"\n\n\t\tPress Enter to continue"; } } getch(); } }

請幫我修復它

您將不得不丟棄此代碼,它是無法修復的。 這是一個示例(此錯誤似乎重復多次),來自 function input

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

當您在cin>>Parking[i].DriverName;行中使用它時,您認為變量i的值在這里是什么? ? 答案是它沒有。 這會對您的程序產生什么影響? 應該會崩潰吧。

現在你寫了很多代碼,但似乎沒有一個工作。 這只能是因為您已經編寫了所有代碼而沒有進行任何測試。 這是編寫軟件的錯誤方法。 寫一點代碼(字面意思是幾行),測試它們直到你確定它們可以工作,然后再寫幾行。 這是編寫軟件的唯一方法。 在沒有任何測試的情況下編寫大量代碼總是會失敗,如果你是初學者,你會陷入不知道哪里出了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM