Jump to content
  • Advertisement
Sign in to follow this  
Raiku

Data Structure through Varaibles

This topic is 4385 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.
Quote:
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:
Quote:
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
Advertisement
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

Example:
GX + 1000 + E

Result:
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
Quote:
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 >> temp.name;

if ( temp.name == "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(temp.name);

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

if ( iter == database.end() )
{
cout << temp.name << " 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.name] = temp;

cout << "New data recorded. Thank you.";
}
else
{
cout << temp.name << " 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: " << found.name
<< "\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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!