Thread: creating variables on the fly

  1. #1
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48

    creating variables on the fly

    ok, so i need to have identical structures or classes (doesn't matter), and i need to have as many of them as the user wants to use. i could make a whole bunch of htem and call it good, but there is always that chance. so i want to have a counter, that creates a structure to put information to as its needed. but i keep getting really really weird syntax errors.

    first, is that posible?

    second, i created a dummy program just to see if it was posible, this is it, and this is whats giving me so much trouble.

    Code:
    #include <iostream>
    
    void main()
    {
    int n = 0;
    int again;
    	do{
    
    		struct n
    		{
    			int i;
    		}n;
    
    		cout << "gimmie a number: ";
    		cin >> n.i ;
    
    		cout << "go again? 1/0: " ;
    		cin >> again ;
    
    		n++;
    	}while( again == '1');
    	
    	while( n >= '0')
    	{
    		cout << "n.i" ;
    	}
    
    	return;
    }
    the errors that i get don't make sence, and when i double cliock on them it opens up 2 new files, cerrno, and cstdio, and says something is wrong with them.

    any idea what i'm doing wrong/ any idea how to do what i'm trying to do?

    any help is greatly apreciated

    ps: i'm using windows XP, with microsoft visual studio.net
    Last edited by Aalmaron; 04-10-2006 at 10:43 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You have an int n, and an instance of your struct n. You do not reference the std namespace with cout or cin. You put things in quotations or single quotes a lot when they are not meant to be. It's int main. You need to change the value of 'n' in the second while loop, or whatever you rename it too, so it is not infinite.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> any idea what i'm doing wrong?

    quite a lot.

    >> any idea how to do what i'm trying to do?

    you can use an array, std::vector, std::list, or any other suitable container.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    heh, i was using printf and scanf, becuase i don't like cout or cin, but changed it. thats why i have the double quotes there. guess i wasn't really paying atention. but the rest still stands.

    how do i make the instance of my struct the value of n? would i use &n? also, by me putting n as the name of the struct, is the struct named the value of n, or is it just named n?

    thanks, i hadn't noticed that there was no n--; in the second while loop.

    so no matter what i changed, i was getting hte same error, so i made a new file, and the erroes got back to stuff i could reletivly understand.

    i still don't understand how i would make the structure name the numerical value of n (if it helps i could make it a class), the only errors that i'm getting are:

    error C2676: binary '++' : 'main::n' does not define this operator or a conversion to a type acceptable to the predefined operator

    and
    error C2228: left of '.i' must have class/struct/union type
    type is 'int'

    here is my code after i cleaned it from my dumb mistakes.

    Code:
     #include <iostream>
    using namespace std;
    int main()
    {
    int n = 0;
    int again;
    
    
    do{
    		struct n
    		{
    			int i;
    		}n;
    		cout << "gimmie a number: ";
    		cin >> n.i ;
    		cout << "go again? 1/0: " ;
    		cin >> again ;
    
    		n++;
    	}while( again == 1);
    	
    	while( n >= 0)
    	{
    		cout << n.i ;
    		n--;
    	}
    
    	return 0 ;
    }
    Last edited by Aalmaron; 04-10-2006 at 11:14 PM.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Post new code, where you are trying to use the address-of n probably. Edit: I see your edit.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int iter = 0;
    	int again;
    
    	struct n
    	{
    		int i;
    	} n;
    
    	do {
    
    		cout << "gimmie a number: ";
    		cin >> n.i ;
    		cout << "go again? 1/0: " ;
    		cin >> again ;
    
    		iter++;
    	} while( again == 1);
    	
    	while( iter >= 0)
    	{
    		cout << n.i ;
    		iter--;
    	}
    
    
    	return 0 ;
    }
    Make sure your struct doesn't go out of scope from the loop. Renamed the other thingy.
    Last edited by Tonto; 04-10-2006 at 11:17 PM.

  6. #6
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    Quote Originally Posted by Sebastiani
    >> any idea what i'm doing wrong?

    quite a lot.

    >> any idea how to do what i'm trying to do?

    you can use an array, std::vector, std::list, or any other suitable container.
    the reason i'm trying to figure it out is becuase my homework requires that each thing has its own structure. so i can't just make them all arrays. :-/


    Quote Originally Posted by Tonto
    Post new code, where you are trying to use the address-of n probably.
    sorry, didn't think about that, but i edited my post with syntax errors and everything.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What are you incrementing if you don't mind me asking?
    I think you will find you are incrementing an object of type n, which is also incorrectly declared.

    This is hideous sticking everything in main but here goes:

    Code:
    #include <iostream>
    
    //Sorry, I detest the use of this mechanism
    //using namespace std;
    
    //Could use a list as well
    std::vector<UINT> vNumbers;
    
    
    int main()
    {
      //Loop variable
      int again=0;
    
      //Value from user
      UINT value=0;
    
      do 
      {
        cout << "gimmie a number: ";
        cin >> value;
    
        //Add value to the vector
        vNumbers.push_back(value);
    
        cout << "go again? 1/0: " ;
        cin >> again ;
    
    } while( again == 1);
    	
    //Print out all values entered
      for (UINT i=0;i<vNumbers.size();i++)
        cout << vNumbers[i];
    
    
    return 0 ;
    }
    Last edited by VirtualAce; 04-10-2006 at 11:45 PM.

  8. #8
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    the user can add types of food to this program that i'm suposed to be making. each item of food has a name, caliroes, etc etc, and is suposed to be in its own structure. when dealing with the food, teh user enters a coresponding number, aka, teh first item they added was 0, etc

    which is why i ahve to use classes or structures. otherwise i would just use arrays, which would be the most simple way posible.

    btw, that code isn't working. i've never heard of UINT, and my compiler doesn't seem to know what it is either. i'm also getting a syntax error that vector isn't part of std.

  9. #9
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    Quote Originally Posted by Tonto
    Post new code, where you are trying to use the address-of n probably. Edit: I see your edit.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int iter = 0;
    	int again;
    
    	struct n
    	{
    		int i;
    	} n;
    
    	do {
    
    		cout << "gimmie a number: ";
    		cin >> n.i ;
    		cout << "go again? 1/0: " ;
    		cin >> again ;
    
    		iter++;
    	} while( again == 1);
    	
    	while( iter >= 0)
    	{
    		cout << n.i ;
    		iter--;
    	}
    
    
    	return 0 ;
    }
    Make sure your struct doesn't go out of scope from the loop. Renamed the other thingy.
    thats great, and it worked and everything, only problem is that every time i input a new value to n.i, the old value is gone. what i wanted to do wad make it so taht it wasn't n.i, it was actualy 0.i, 1.i, 2.i, etc, and call it that way, with n being a counter, and my incrimenting, or decrimenting n, i would call a diferent struct.

    sorry about the double post, i'm worried that people wouldn't see an edit.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Then you would need a way to create several instances (other than arrays or STL containers which don't seem to be allowed) of the struct. Since you're using the same struct, writing a new value will overwrite the previous one. If you're really not allowed to use arrays, did your teacher suggest any other methods to store multiple instances of an object?




    By the way, you can't simply walk in and create a variable out of an arbitrary name in C++. You'll need to declare it with a valid name and provide your program with a good reference to it before you can use it at all.
    Last edited by jafet; 04-11-2006 at 12:33 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    which is why i ahve to use classes or structures. otherwise i would just use arrays, which would be the most simple way posible.
    Why cant you use an array of objects?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    ok, i decided thtat i'd be easeier to use a class, since i can just do this.

    Code:
    		class cls
    	{
    	public:
    
    		int i;
    	};
    
    	cls one
    	cls two
    and now i can use each of them as if they were compleatly diferent.

    and i figured out that you can't use a number, i have to use words, which is fine, i could convert every number to that many 'a's or something. only problem is how do i tell cls that i wasn't it to be known by the value of something, instead of just something?

    no my teacher didn't sugest anything else.

    so with classes, and kind of re done, this works
    Code:
    #include <iostream>
    using namespace std;
    
    int number;
    	class cls
    	{
    	public:
    		void seti(int i);
    		int geti();
    	private:
    		int i;
    	};
    void cls::seti(int i)
    {
    	this -> i = i;
    }
    int cls::geti()
    {
    	return i;
    }
    int main()
    {
    
    	cls one;
    	cls two;
    
    	cout << "enter a number: ";
    	cin >> number;
    	one.seti(number);
    
    	cout << "\nenter another number: ";
    	cin >> number;
    	two.seti(number);
    
    
    		cout << one.geti() << " " << two.geti();
    
    	return 0 ;
    }
    now i'm new to classes, and i know teh including namespace std is a noob thing to do. sorry about my ineptitude.

    so there is no way to instead of using "one" or "two" use a char string? i can't figure one out, but i'm still new, so maybe one of you know.

    Quote Originally Posted by laserlight
    Why cant you use an array of objects?
    i don't know how, or what that is :-/

  13. #13
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    It sounds to me like you are talking about using a linked list structure and using the number as a way to access the node you need. It would fit your criteria. Each time you add a node you can increment the count and store that number in that node. When you need to access the node, you would walk the list of nodes compareing the number to the one entered and then display that node once it is found.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  14. #14
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    Quote Originally Posted by manofsteel972
    It sounds to me like you are talking about using a linked list structure and using the number as a way to access the node you need. It would fit your criteria. Each time you add a node you can increment the count and store that number in that node. When you need to access the node, you would walk the list of nodes compareing the number to the one entered and then display that node once it is found.
    exactaly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  5. creating accelerators on the fly
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-15-2002, 11:19 PM