Advertisement Jump to content
Sign in to follow this  

Data Structure through Varaibles

This topic is 4478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a Data Structure through Variable, so that way I can organize my information easily, I am not sure if I am doing it right, or if I am doing it wrong, or if theres another way, please someone say say so. The idea.
struct brand { char Mname,Mnum,Medi; }; int main(){ std::string name; std::cin >> name; brand name; }
Inputting Suzuki into name, I wanted to get this Output:
Suzuki.Mname Suzuki.Mnum Suzuki.Medi
However, I am getting some errors which are multiple delcaration and such, is there anyway to get around that or some how reach my idea or goal?

Share this post

Link to post
Share on other sites
There are a number of problems or possible problems here, but I'll just start with two comments.

First, the line 'brand name;' confuses the compiler because name is already the name of a variable (you declared it in the first line of main()), and here you are attempting to declare a new variable (of type brand) with the same name. This is what the compiler error is referring to.

As for your desired output, the program produces no output of any sort so it's hard to guess at your intent.

The only thing I can guess at is that you're trying to create an identifier at run time based on user input, but this isn't possible in C++ (in any direct way at least).

In any case, perhaps you can provide some more info about what you're trying to do exactly.

Share this post

Link to post
Share on other sites
I am trying to organize Motocyles into their Series and Models.

These would be their structures:
Series + Numbers + Edition

GX + 1000 + E

Series = GX
Number = 1000
Edition = E

and then I have all these other models to input for other example.

So it would turn out something like this:
Series.Number.Edition.(Other Information)

and that what the User will be inputting.

Does that at least explain what I am trying to get at?

Share this post

Link to post
Share on other sites
Original post by Raiku
Does that at least explain what I am trying to get at?
Well, sort of. Maybe you could give an example of what the user might enter, and then the exact output that you would want to generate in response to this input. Perhaps you could also clarify what you mean by 'output' (to the console? to a file? to the member variables of an object?).

Share this post

Link to post
Share on other sites
Understand that the names you give to variables/types in your code simply don't exist in the final executable. You can't use run-time input to create or compare against those names. You will probably want to store these records in an STL container. Which container depends on how you want to be able to retrieve or update them. There are different methods for searching containers to retrieve a stored element. Incidentally, your structure using only single char variables is not going to work. Use strings if you want to store strings, ints if you want to store whole numbers.

Share this post

Link to post
Share on other sites
I wrote this up the other day but I wanted to run it through a compiler before posting just to be sure. Better late than never. If this is too confusing just save it and study up on the STL. Here I am using a std::map to store some simple records. Without any file I/O nothing is saved between runs of course.

#include <iostream>
#include <map>
#include <string>

using namespace std;

struct Thing
string name;
int weight;
int cost;

int main()

// This is a map. The first template parameter is the key type, used
// to retrieve the value. The second parameter is the value type, the
// type of object to be stored.

map<string, Thing> database;

// The following user interface does no input validation, but this is just
// an example. Normally you'd wrap this stuff into tidy little functions.

while (1)

// For this example I use a temporary that can be
// copied into the map.

Thing temp;

cout << "\nEnter a Thing name (or 'quit'): ";
cin >>;

if ( == "quit" ) break;

// Search the map using its find function which returns an iterator.
// (an iterator is like a special kind of pointer for containers)

map<string, Thing>::iterator iter = database.find(;

// iter now either points to the matching element,
// or it points to the end() if it found no match

if ( iter == database.end() )
cout << << " not found. A new database entry will be created.\n";

cout << "Enter a number for the weight: ";
cin >> temp.weight;

cout << "Enter a number for the cost: ";
cin >> temp.cost;

// Copy temp into the map, using the name as the key...

database[] = temp;

cout << "New data recorded. Thank you.";
cout << << " was found in the database. Here it is...";

// iter points to a std::pair object where first is the key and second is
// the value. We'll just create a local reference to second.

const Thing& found = (*iter).second;

cout << "\nName: " <<
<< "\nWeight: " << found.weight
<< "\nCost: " << found.cost;

cout << endl;


cout << "Goodbye." << endl;


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!