• Notice regarding image hosts and attachments. See here.

Help with C++

Hey there,

I was wondering if anyone is any good with C++, doing my project for my HNC in computing and having a problem.

I've made a struct into which data is being written to, I now need to change the data in the particual record held in the struct. I have done this by doing a search of the struct first via the app.no. in the struct. This appears to be working and the data is being changed in the record I have searched for. The problem is when i'm now displaying the records it appears the program is writting a 0 into the first field of the next record, ie the next app.no

Here is the part of the code i'm trying to fix...

void amend_record(mob_app app[], int row)
{
clrscr();
cout << "this is Search\n";

int no;
cout << "Enter Customer Number : ";

Input(no);

for (int r=0;r<row;r++)
{
if ( no == app[r].no )
{
clrscr();
cout << app[r].no <<endl;
cout << app[r].name <<endl;
cout << app[r].url <<endl;
cout << app[r].description <<endl;
cout << app[r].price <<endl;
cout << app[r].category <<endl;
cout << app[r].price <<endl;
cout << app[r].rating <<endl;

cout << "please input new app no. : " ;
Input(app[r].no);
cout << "please input new app name : " ;
Input(app[r].name,20);
cout << "please input new app url : " ;
Input(app[r].url,20);
cout << "please input new app description : " ;
Input(app[r].description,20);
cout << "please input new app price : " ;
Input(app[r].price);
cout << "please input new app category : " ;
Input(app[r].category,20);
cout << "please input new app price : " ;
Input(app[r].price);
cout << "please input new app rating : " ;
Input(app[r].rating,20);

cin.ignore(1);





write_file(app,row);

row++;


}
}
input();
}


I have tried putting in cin.ignore(1) but to no avail

If anyone could help it'd be greatly appreciated.

Thanks,

K
 
I think it would be easier to debug if we could see the contents of the Input() and write_file() functions. Also if you are regularly accessing the app via the app.no then it might be a good idea to use a std::map instead of array and have the no as the key for each record. Also arrays are generally a bad idea in c++ so I would advise using vectors instead.

I am also not sure why you have row++ at the end. After accessing and amending the record don't you want to exit the loop? You can do this using break; Also not sure what the last input() is for.

Also you seem to be writing to app.price twice. Is this intentional?
 
I didn't mean to write to app.price twice, thanks for pointing that out. I'm v new to C++ and finding it pretty hard to be honest. We havn't been taught what vectors are so won't be using them. I have also taken out the row++, still not sure how to stop it writing this 0 to next record. The write file is...

void write_file (mob_app app[], int rows)
{
int r=0;
ofstream myfile(TXTFILE,ios::binary);
if (myfile.is_open())
{
while ( r < rows )
{
myfile.write((char *)&app[r], sizeof(mob_app));
r++;
}
myfile.close();
cout << "Data written to " << TXTFILE << "\n";
}
else
cout << "Unable to open file: " << TXTFILE << endl;
}


Thanks for you reply,

k
 
Is it the textfile record which has the extra 0 or the app array or both? Can't see anything wrong with the write_file function atm.
 
Back
Top Bottom