Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically, what I'm trying to do is to read and load a tree structure from a file to an unordered_map.
Example of a tree structure:
<index> <attribute> <operator> <value> <no> <yes> %optional comment
0 height > 180.0 1 basketball %first node
1 weight > 50.0 athletics basketball

For now, I've written code that would display the tree structure: (with no comments, currently also working on it ignoring comments)

C++
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>

struct Node {
	int index;
	std::string attribute;
	char oper;
	double value;
	std::string no, yes;
};

int main()
{
	std::ifstream filetree(">
The problem is, I was instructed to use the following variable types:
<pre>int index;
	std::string attribute;
	bool oper;
	double value;
	int no, yes;

And, using these variable types, nothing is displayed.
While debugging, it is shown that node.oper is false (which is understandable, because of it being a '>' or a '<'), node.value is equal to -9.2559631349317831e+61, while both node.no and node.yes are equal to -858993460.
If I put the
std::cout<<node.index...
outside of the
C++
while(filetree>>node.index...

The previously mentioned values are displayed(-85899...), but only in 1 line (what shouldve been 2 lines)

My main problem is handling the bool and int type variables, in that case int being a word ("basketball" for example) and bool being one of two characters ('>' or '<')
Later on, I will have to use the tree structure to classify data, but I have to tackle this problem first.

What I have tried:

Reading about variable type conversions - due to being a huge beginner of a programmer I couldnt think of any solution using conversions
Posted
Updated 1-Jan-21 8:48am
v3

Firstly, this is C++ so you can initialize your members and I highly recommend that you do.
C++
struct Node {
    int index    { -1 };
    bool oper    { false };
    double value { 0 };
    std::string no;
    std::string yes;
    std::string attribute;
};
strings are initialized to be empty in their constructor.

Secondly, what's wrong with using a boolean value there? If there can be only two possible values, '<' or '>', then it fits appropriately. Designate one of them to be true, like the greater than, and the other, the less than, will be false.

To clarify - if you encounter a '>' then assign oper the value true. That's all there is to it. Personally, I would not do things that way but I did not write the assignment.
 
Share this answer
 
v2
You are mismatching the types and it confuses, a bool can only be true or false. So write "clean code" which is more understandable.

Dont use the same variable names for different types.
C++
char cOperator = '<';
// check for less
if( cOperator == '<' ) {
} else if( cOperator == '>' ) {
} else if( cOperator == '=' ) {
} else {
// some error handling like a message
}
Invest some times in reading some good tutorial like Learn C++ to enhance your skills. In the starting phase of learning coding it is important to learn for avoid stupid errors in the lack of knowledge.
 
Share this answer
 
If you are instructed to use bool and that's clearly not appropriate, then you need to go back to your tutor and explain your concern. It's possible that he had an idea he wanted you to get, or mistyped the instructions, or that you have misread them.
We don't know: we weren't there when your assignment was given, and we won't be marking your work!

Talk to your teacher.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900