ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The file can contain one or N number of these name-value pairs. The code needs to be able to do the following:
1) Read them all in.
2) Ensure each one has the variables NAME, AGE, and SEX all filled in.
into any web search engine. Start from plain old "C" 'index' function (probably a part of C++ standard library too) : http://linux.die.net/man/3/index .
Great idea with the awk, though it needs to be done in C++ as I'm learning it by trying to do different things of which this is one of them. Am thinking of reading the stuff into a vector of struct, which consists of the expected variables per set. Is this a good approach?
Hi, this is my attempt to do this, though am having a bunch of errors from the IDE in NetBeans as shown in the attachment.
Not sure why it's complaining about simple things like the if and while statements.
Also, not sure why it can't find the <iostream>, <ifstream>, or <ofstream> headers. I'm using remote build option btw and it is able to find the compilers, assemblers, and linkers on the remote Linux box.
I'm not sure about NetBeans, but to me it seems a few things are wrong:
Firstly, you're missing the line "#include <vector>". Also, C++ vectors are created empty, so you first need to add items to them before modifying them, e.g.
Code:
TypeStructConfig foo;
foo.name = "A N Other";
foo.age = 1;
foo.sex = 'M';
structConfig.push_back(foo);
Also, you use square brackets to access elements, so in lines like
Code:
structConfig(count).sex = value;
the round brackets are wrong. You can use the member function at() to access elements, instead of using array syntax (i.e. square brackets), e.g.
Code:
structConfig.at(count).sex = value;
I've only had a quick glance at the code, so there might be other things too.
Hi, this is my attempt to do this, though am having a bunch of errors from the IDE in NetBeans as shown in the attachment.
Not sure why it's complaining about simple things like the if and while statements.
This is not a valid C++ program. There's no main() function, and code is mixed up with global variables, and there are undeclared identifiers (NAME/AGE/SEX).
Also you don't use "typedef struct{} StructName" in C++. Make sure it compiles, fix all the errors, and try again - because until you do this, there's nothing to talk about (unless you're trying to understand compiler error). If you have trouble writing a basic C++ program, start from beginning - try any C++ book or tutorial.
Hi, reason there wasn't a "main" function was because I was thinking of putting this into one of the cpp files for the project, and use include this from the main source file.
Anyway, if I'm writing it as a stand alone program, it'd look like the following. However, am stuck on getting the vector of struct to work correctly. I'm stuck. Can someone please advise?
Code:
test.cpp:28: error: expected initializer before ‘<’ token
test.cpp:29: error: ‘TypeVectorStructConfig’ does not name a type
test.cpp: In function ‘int main(int, char**)’:
test.cpp:40: error: ‘VectorStructConfig’ was not declared in this scope
test.cpp:43: error: ‘VectorStructConfig’ was not declared in this scope
test.cpp:46: error: ‘VectorStructConfig’ was not declared in this scope
For one thing, you're missing the "vector" header, as I pointed out in my post above. Also, you still aren't adding any elements to the vector. Again, see my post above (you need to use the push_back() member function).
The following line is wrong:
Code:
VectorStructConfig[count].age = value;
value is a string, but the age member of your struct is an int. You can get from a string to an int with a stringstream:
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string s("123");
stringstream ss;
ss << s;
int x;
ss >> x;
cout << x << endl;
return 0;
}
Obviously, you need to be sure that what's being input is really a numeric value!
The line
Code:
VectorStructConfig[count].sex = value;
is wrong because value is a string, but the sex member is a char. You can either use string's at() member function or use array syntax to get the character:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.