hi there i m a little confused about this problem, here is the question:
Prompt the user for a descriptor and value, and add these to the array. Print the descriptors and values, including the new one. so my programs ask the user for the file name(usually one with names and some kind of score or grade next to the name) goes through it and make sure its able to read all the info on the file and the prints whatever is in the file. this is working so far now i have to be able to get a new name and value from the user and add it to whatever is on the file. there is where i have a problem, not really sure how to do that! here is my code:

#include <iostream>
#include <fstream>
using namespace std;
const int MAX=100;
struct record
{
char name[MAX];
float number;
};
bool trytoread(ifstream *pf, record *pr)
{
*pf>>pr->name;
*pf>>pr->number;
if(!*pf)
return false;
return true;
}
int main ()
{

record data[100];
int count=0;
char name[100];
char nombre[20];
char filename[80];
float newvalue;
cout<<"Enter file name:"<<endl;
cin>>filename;
ifstream input;
input.open(filename);
if(!input)
{
cout<<"bad open"<<endl;
exit(1);
}
while(trytoread(&input,&data[count]))
count=count+1;
input.close();
bool found=false;

cout<<"type a name: ";
cin>>name.record??????;//HOW TO I ADD THESE NAME TO THE ARRAY ON THE FILE???
cout<<"Enter a value:";
cin>>count.record??????;//HOW TO ADD THESE VALUE TO THE ARRAY ON THE FILE??

for(int i=0;i<count;i++)
cout<<data[i].name<<"::"<<data[i].number<<endl;
return 0;
}

i know this is something simple but i can't figure it out, so i was thinking that the input from the user needs to be added to the strut right???
thankss

First, please spend a bit of money on some indenting, it's cheap.

Simple solution is to allocate single record object. Input to it, then assign it to the next spot in the array.

//after line 21
record temp;

//replace line 42 & 44
cin >> temp.name;
cin >> temp.number;

//insert new data
data[count] = temp;
count++;

//or you can input directly to the array
cin >> data[count].name;
cin >> data[count].number;
count++;

Of course, you should first check count to ensure there's actually room in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.