i am using a c library from c++ application and i need to fill a c struct from c++ code, this is a c++ console application. it doesnt seem to work when i assign variable values using cin. what should i do ?

post representative code.

int Books::InsertBook()
{
	char txt[40];
    cout << "id_code :" << endl;
	gets(bookrec.id_code);
	cout << "info_title :" << endl;
	gets(bookrec.info_title);
	cout << "author :" << endl;
	gets(bookrec.author);
	cout << "publisher :" << endl;
	gets(bookrec.publisher);
	cout << "pubdate :" << endl;
	gets(bookrec.pub_date);
	cout << "infotype :" << endl;
	gets(txt);
    sscanf(txt, "%d", &bookrec.info_type);
	d_fillnew(INFO, &bookrec);
    cout << "successful" << endl;
	return 5;
}

i want to be able to use cin instead of gets(which is c command).

Post a short example which doesn't seem to work.

Did you remember the
extern "C"
bit around anything which needs to be compatible with C ?

Nevermind - it doesn't have code tags, so I lost interest.

Even if you use the C version of declaring struct instead of the C++ version, I still think it would work. However, post the declaration of bookrec just to be sure.

This should work fine:
cout << "id_code :" << endl;
cin >> bookrec.id_code;

assuming id_code has been declared with public access. If not, then you need a public accessor function in bookrec to get the information into the variable. Either way, code for bookrec would be helpful.

Instead of sscanf() you might want to look into using stringstreams, which would be reasonably equivalent C++ way to convert one type into another (text into numerical values at least).

struct is also defined in C header file as follows:

struct info {
   char  id_code[16];
   char  info_title[80];
   char  author[32];
   char  publisher[32];
   char  pub_date[12];
   int  info_type;
};

Assuming you have declared bookrec like this:

info bookrec;

Then using >> to input information into the member variables is an option. If none of the strings will contain embedded whitespace, then it may even be the best option. If the strings representing the member variables can have embedded whitespaces, and author and publisher, etc, they may well, then using getline() instead of >> seems to make a fair amount of sense. There is potential problem if you mix getline() and >> in the same program, so beware. But, before going into that we should see what type of information will be entered, as >> may be all you need.

Assuming you have declared bookrec like this:

info bookrec;

Then using >> to input information into the member variables is an option. If none of the strings will contain embedded whitespace, then it may even be the best option. If the strings representing the member variables can have embedded whitespaces, and author and publisher, etc, they may well, then using getline() instead of >> seems to make a fair amount of sense. There is potential problem if you mix getline() and >> in the same program, so beware. But, before going into that we should see what type of information will be entered, as >> may be all you need.

i declared bookrec like this :

struct info bookrec;

Actually i want to add a record to raima database as follows :

int Books::InsertBook()
{
    cout << "id_code :" << endl;
    cin >> bookrec.id_code;
	cout << "info_title :" << endl;
	cin >> bookrec.info_title;
	cout << "author :" << endl;
	cin >> bookrec.author;
	cout << "publisher :" << endl;
	cin >> bookrec.publisher;
	cout << "pubdate :" << endl;
	cin >> bookrec.pub_date;
	cout << "infotype :" << endl;
	cin >> bookrec.info_type;
	d_fillnew(INFO, &bookrec);
    cout << "successful" << endl;
	return 5;
}

here it is in code tags salem may be you gain your interest back!

i see that it works like above but it doesnt reflect the changes to the database until you quit the program and close the database. One thing i also noticed is that when i declare the the struct in c++ file i can use struct info bookrec and info bookrec, it doesnt seem to make any difference..

Use getline() instead of >> ..Agree..
Ex:

int varA=0;
// ...
cin>>varA;
char strA[15];
// ...
cin.getline(strA,15);

i don't know what should i say in English..Hope this can help u..

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.