Problem with variable

This is a discussion on Problem with variable within the C++ Programming forums, part of the General Programming Boards category; Hi, Im fairly new with programming. Im following up my last class on programming with c++. The question i have ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    20

    Problem with variable

    Hi,

    Im fairly new with programming. Im following up my last class on programming with c++.

    The question i have is very simple.

    Lets say you want to create 10 different variables and give them name hello1 hello2 etc...

    I want to use for(). But the problem I have is that i can't understand how you can use a variable for defining a variables name.

    example:
    Code:
    #include <iostream>
    using namespace std;
    
    int main (){
    
    int i;
    
    
    for(i = 0; i<10; i++){
    
    char hello(i);*
    cin >> hello(i);**
    cin.ingore(1000, '\n');
    }
    
    //this would give us the input for our hello0 and hello5 variable.
    cout << endl << endl << hello0 << hello5;
    
    return 0;
    }
    *(this is where I want i to be, so that for() can create 10 different hello variables with the name hello0 hello1 hello2 etc...)

    **(i.... same here, each time for() goes a lap i want the next variable to be used in the cin phrase)
    Last edited by AdampqmabA; 09-25-2008 at 07:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,638
    You probably want to use an array. Read the tutorial on arrays.
    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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    20
    I would not like to use an array. I just want to create 10 variables. by using a variable i for defining each variables name.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,638
    I would not like to use an array. I just want to create 10 variables. by using a variable i for defining each variables name.
    C++ does not have variable variables, so barring some unnecessary yet creative macro tricks (that I do not know of) this is not possible. Generally an array really is the correct solution when you are faced with such a problem.
    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

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't think what you want is possible, so the best you can do is stick with the array ^^

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    20
    strange it seems like such an easy thing to implement. Well then i will just read more about arrays then :/

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,638
    strange it seems like such an easy thing to implement.
    It is indeed an easy thing to implement... with arrays.
    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

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by AdampqmabA View Post
    strange it seems like such an easy thing to implement.
    If you think that, you don't understand the separation of compilation and execution yet.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AdampqmabA View Post
    strange it seems like such an easy thing to implement. Well then i will just read more about arrays then :/
    It would be relatively easy to make dynamic variables in a language where variables are created at runtime (when the application runs). In C and C++, ALL variables are created at compile-time [there are ways around that, but that involves pointers (which is a sort of variable that holds a reference to a lump of memory that can in turn hold other things), which is a couple of steps away from where you are at the moment, I suspect], so the compiler needs to know what value i has to make the variable for you. Since that is impossible for the compiler to know in most situations (including this one, as i changes throughout the runtime of the application), it doesn't work like you expect.

    The reason C and C++ are not createing variables at runtime has to do with the extra overhead that is involved with that. That is what makes C and C++ really fast to run. But it means some extra work at compile time and sometimes extra work for the programmer. You can't have it both ways, I'm afraid.

    Laserlight, I don't think macros would work. Templates perhaps, but even that would probably be streatching it in this case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,638
    Quote Originally Posted by matsp
    Laserlight, I don't think macros would work.
    Neither do I, but my lack of knowledge of the more creative aspects of the preprocessor is enough for me to give it the benefit of the doubt.

    Quote Originally Posted by matsp
    Templates perhaps, but even that would probably be streatching it in this case.
    Templates won't cut it, or we can claim that arrays are variable variables.
    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

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, macros would work. The code would be completely unreadable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. problem with variable passed as reference
    By crash88 in forum C Programming
    Replies: 1
    Last Post: 05-16-2006, 07:16 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. problem with the const variable
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 01-28-2005, 08:53 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 03:06 PM

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