![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 10
Rep Power: 0
![]() |
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 ???;example: If the user typed in "John" for "name" then this would happen: test John; |
|
|
|
|
|
#2 |
|
Professional Programmer
|
in your code test is an integer and name is a character array. Therefore I don't think you can do that.
|
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Nov 2004
Posts: 10
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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 :/ |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
oh and to change the name is like
cin >> newTest.test cin.getline(newTest.name,20); |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2004
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2004
Posts: 10
Rep Power: 0
![]() |
Ok, I'll try the linked list suggestion.
Thanks for the help. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|