Assigning a name to a variable

This is a discussion on Assigning a name to a variable within the C++ Programming forums, part of the General Programming Boards category; I'm trying to have the user enter a word or two (specifically a name), and then be able to recall ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    40

    Assigning a name to a variable

    I'm trying to have the user enter a word or two (specifically a name), and then be able to recall that name later in the program as a variable (or some other type of memory storage that I've yet to hear of). How do I store it?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In a std::string? You should use fgets, I guess. (Elysia does the fgets proselytizing, so you should find one of her posts.)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    This is the C++ programming forum though, so we tend to do C++ I/O stream proselytising instead.

    PAragonxd, what have you learnt about reading in user input?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Well, you may remember that I'm very new around here, and to C++ in general... so assume that I know minimum.

    EDIT: But please, mind your time. I really don't need a huge lengthy explanation, so I don't want you to spend your time writing one.
    Last edited by PAragonxd; 12-11-2007 at 10:41 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    If the variables which you want to name at run-time are of the same type, I would picture this as a job for an std::map<> container.

    Code:
    std::map<std::string, int> myVariables;
    
    void AddVariable(std::string name, int data)
    {
       myVariables[name] = data;
    }
    
    void GetVariableName()
    {
       std::string name;
       std::cin >> name;
       myVariables[name] = 0;
    }
    
    void SetVariableData(const std::string& name)
    {
       int data;
       std::cin >> data;
       myVariables[name] = data;
    }
    
    // ...
    Edit: This code is quite over simplistic yet it clearly shows the concept I wanted to show you.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    I didn't imagine a simple process would require all that, maybe I didn't ask my question clear enough.

    I'm aiming to store a name in the same way you would store a numeral variable. Is it even possible?

    Thanks for the help though desolation, even if I didn't quite understand it.
    Quote Originally Posted by Desolation View Post
    Edit: This code is quite over simplistic yet it clearly shows the concept I wanted to show you.
    That does a nice job of making me feel like a moron :P.
    Last edited by PAragonxd; 12-11-2007 at 11:13 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by tabstop View Post
    In a std::string? You should use fgets, I guess. (Elysia does the fgets proselytizing, so you should find one of her posts.)
    Oh you mean Elysia is a 'her'?

    I'd go for the first-hand knowledge if I were you, and go straight to the FAQ. (hey that ryhmes! )
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Quote Originally Posted by iMalc View Post
    I'd go for the first-hand knowledge if I were you, and go straight to the FAQ.
    That's a great quote :P.

    Yeah, kinda hasty of me to ask here without checking there first. Sorry about that one >_>.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    
    int main()
    {
        std::string name1;
        std::string name2;
    
        std::cout << "Enter a name:\n";
        std::cin >> name1 >> name2;
    
        std::cout << "You printed: " << name1 << ' ' << name2 << '\n';
    
        return 0;
    }
    Last edited by robwhit; 12-12-2007 at 04:38 AM.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Awesome, thanks robwhit

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by iMalc View Post
    Oh you mean Elysia is a 'her'?
    I don't actually know that and am guessing. If I'm wrong, all apologies.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh... Sorry I thought you wanted to actually name a variable and not have a variable contain a string.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Yeah I'm sorry about that, I didn't think before I asked :P.

    Thanks a lot for your attempt though.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    Just saw your edit. I clearly didn't intend to make you feel like a moron and I'm sorry if I looked like so.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Lol, don't worry about it :P. I figured it wasn't on purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Assigning Binary Value to a Variable
    By JAYMAN in forum C Programming
    Replies: 2
    Last Post: 11-06-2005, 12:24 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Variable assigning Functions
    By emus21 in forum C Programming
    Replies: 4
    Last Post: 11-09-2003, 12:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21