PDA

View Full Version : سوال: مشکل این برنامه چیه؟



farbovad
پنج شنبه 19 تیر 1393, 20:20 عصر
#include"iostream"#include"cstdlib"
#include"fstream"
#include"cstring"
using namespace std;
int get_int (int default_value);
int main()
{
char model[20];
char sazande[20];
char year[5];
int km;
int recsize=sizeof(model)*2+sizeof(year)+4;
char filename[MAX_PATH+1];
cout<<"enter file name: ";
cin.getline(filename,MAX_PATH);
fstream fbin(filename,ios::binary|ios::out|ios::in);
if(!fbin){
cout<<"boooooogh";
system("pause");
return -1;
}
int a=0;
while (true){
cout<<"choose menu num:\n1.write a rec\n2.read a rec\n3.exit";
a=get_int(3);
if(a==3){
fbin.close();
system("pause");
return -1;
}
cout<<"\nenter file rec num: ";
int n;
n=get_int(0);
if(a==2){
fbin.seekp(n*recsize);
fbin.read(model,sizeof(model)-1);
cout<<"\nmodel is: "<<model;
fbin.read(sazande,sizeof(model)-1);
cout<<"\ncompany name is: "<<sazande;
fbin.read(year,sizeof(year)-1);
cout<<"\nproduct date is: "<<year;
fbin.read((char*)(&km),sizeof(int));
cout<<"\nkm is: "<<km;
}
else if(a==1){ cout<<"enter model name: ";
cin.getline(model,19);
cout<<"enter producer name: ";
cin.getline(sazande,19);

cout<<"enter product year: ";
cin.getline(year,4);

cout<<"enter km: ";
km=get_int(0);
fbin.seekp(n*recsize);
fbin.write(model,sizeof(model)-1);
fbin.write(sazande,sizeof(sazande)-1);
fbin.write(year,sizeof(year)-1);
fbin.write((char*)(&km),sizeof(km));




}
}
}




int get_int(int default_value){
char s[81];
cin.getline(s,80);
if(strlen(s)==0)
return default_value;
return atoi(s);
}

کامبیز اسدزاده
پنج شنبه 19 تیر 1393, 21:06 عصر
با وارد کردن هیدر لازم به صورت زیر :


#include <limits.h>


و تغییر MAX_PATH به MAX_PATH_اصلاح شد.


#include"iostream"
#include"cstdlib"
#include"fstream"
#include"cstring"
#include <limits.h>


using namespace std;


int get_int(int default_value);
int main()
{
char model[20];
char sazande[20];
char year[5];
int km;
int recsize = sizeof(model) * 2 + sizeof(year) + 4;

char filename[_MAX_PATH + 1];
cout << "enter file name: ";
cin.getline(filename, _MAX_PATH);
fstream fbin(filename, ios::binary | ios::out | ios::in);
if (!fbin){
cout << "boooooogh";
system("pause");
return -1;
}
int a = 0;
while (true){
cout << "choose menu num:\n1.write a rec\n2.read a rec\n3.exit";
a = get_int(3);
if (a == 3){
fbin.close();
system("pause");
return -1;
}
cout << "\nenter file rec num: ";
int n;
n = get_int(0);
if (a == 2){
fbin.seekp(n*recsize);
fbin.read(model, sizeof(model) - 1);
cout << "\nmodel is: " << model;
fbin.read(sazande, sizeof(model) - 1);
cout << "\ncompany name is: " << sazande;
fbin.read(year, sizeof(year) - 1);
cout << "\nproduct date is: " << year;
fbin.read((char*)(&km), sizeof(int));
cout << "\nkm is: " << km;
}
else if (a == 1){
cout << "enter model name: ";
cin.getline(model, 19);
cout << "enter producer name: ";
cin.getline(sazande, 19);


cout << "enter product year: ";
cin.getline(year, 4);


cout << "enter km: ";
km = get_int(0);
fbin.seekp(n*recsize);
fbin.write(model, sizeof(model) - 1);
fbin.write(sazande, sizeof(sazande) - 1);
fbin.write(year, sizeof(year) - 1);
fbin.write((char*)(&km), sizeof(km));








}
}
}








int get_int(int default_value){
char s[81];
cin.getline(s, 80);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}

farbovad
پنج شنبه 19 تیر 1393, 21:54 عصر
با تشکر از شما اما همچنان هم کار نمی کند در هنگام گرفتن km برنامه از اول می شود و به منو می رود.