Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 8th, 2006, 4:13 PM   #1
mfo6463
Newbie
 
Join Date: Nov 2004
Posts: 10
Rep Power: 0 mfo6463 is on a distinguished road
create struct from value of a variable

Is there a way to make a struct from a variable's content?

So if I have this:
char name[20];
cin >> name;
cout << name;

struct test
{
     int test;
};

test ???;
Those three question marks... I want to replace that with the value of "name" but I don't know how.

example:
If the user typed in "John" for "name" then this would happen:
test John;
I hope that makes sense...
mfo6463 is offline   Reply With Quote
Old Feb 8th, 2006, 4:16 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
in your code test is an integer and name is a character array. Therefore I don't think you can do that.
Wizard1988 is offline   Reply With Quote
Old Feb 8th, 2006, 4:25 PM   #3
mfo6463
Newbie
 
Join Date: Nov 2004
Posts: 10
Rep Power: 0 mfo6463 is on a distinguished road
Quote:
Originally Posted by Wizard1988
in your code test is an integer and name is a character array. Therefore I don't think you can do that.
I'm not talking about the integer in the struct, I'm talking about the struct itself.
mfo6463 is offline   Reply With Quote
Old Feb 8th, 2006, 4:45 PM   #4
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
you would need the char name[20] in the struct man like so:

struct test
{
     int test;
     char name[20]
};

test newTest;

unless im reading you wrong :/
Seif is offline   Reply With Quote
Old Feb 8th, 2006, 4:47 PM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
oh and to change the name is like

cin >> newTest.test
cin.getline(newTest.name,20);
Seif is offline   Reply With Quote
Old Feb 8th, 2006, 5:00 PM   #6
mfo6463
Newbie
 
Join Date: Nov 2004
Posts: 10
Rep Power: 0 mfo6463 is on a distinguished road
I think you're reading me wrong.

Ok I'll use your code as the example to try to explain a little better.

You put "test newTest;" to create a test struct called "newTest"

I want "newTest" to be whatever the user types in, so the user is actually selecting the name of the struct instead of it being "newTest"

So the user would be pretty much creating the struct.
mfo6463 is offline   Reply With Quote
Old Feb 8th, 2006, 5:07 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
No.....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 8th, 2006, 5:13 PM   #8
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
ah right i see. dont think you can do that. if you want it so you can find the struct, keep the name field in the new struct and create a linked list, where you can find it by performing a strcmp on a user input.
Seif is offline   Reply With Quote
Old Feb 8th, 2006, 5:34 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Think about it like this: input comes at runtime. What are you running? Something that doesn't exist until you run it? There are some things that can be manipulated at runtime. The groundwork has to be laid in advance.

A wise gardener once said, "It is better to leave a person flowerless than to sling shit at a person asking for roses."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 8th, 2006, 5:44 PM   #10
mfo6463
Newbie
 
Join Date: Nov 2004
Posts: 10
Rep Power: 0 mfo6463 is on a distinguished road
Ok, I'll try the linked list suggestion.

Thanks for the help.
mfo6463 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC