Like Tree1Likes
  • 1 Post By mike65535

Can't get a variable to be put in memory (or however that's supposed to be phrased)

This is a discussion on Can't get a variable to be put in memory (or however that's supposed to be phrased) within the C Programming forums, part of the General Programming Boards category; For the variable pettoremove, I can't get the scanf to work and the program doesn't work if I don't preset ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Can't get a variable to be put in memory (or however that's supposed to be phrased)

    For the variable pettoremove, I can't get the scanf to work and the program doesn't work if I don't preset pettoremove a number. That number never changes, and I don't know why and I can't make it work. Is it because it's in a look, I have to use pointers somehow?

    Code:
    void deletepet(pet petarray[], int numberofpets)
    {
    	int pettoremove=9;
    	int validentry=0;
    	while (validentry==0)
    	{
    		printf("Enter the number of the pet you want to remove from the archive: ");
    		fflush(stdin);
    		scanf("&d", &pettoremove);
    		printf("%d\n", pettoremove);
    		printf("%d\n", numberofpets);
    		if (pettoremove<=numberofpets)
    		{
    			printf("That's totally a valid entry\n");
    			validentry=1;
    		}
    		else
    		{
    			printf("That's not a valid entry\n");
    		}
    	}
    	printf("So I made sure bad stuff won't happen, now CODE MOAR STUFF HERE\n");
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    1) Congrats for posting in CODE tags. However, please post compilable code. A main(), includes, etc.
    2) When you compile, do you have warnings turned on? I don't think so. Do that. You'd see immediately that
    Code:
    scanf("&d", &pettoremove);
    has a problem.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    K, I found the problem. %d instead of &d. I do have warnings turned on, but I didn't realize I had made a mistake until you pointed it out, so thanks.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    If your compiler didn't complain about that line, get another. ;-)
    CommonTater likes this.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by mike65535 View Post
    If your compiler didn't complain about that line, get another. ;-)
    Nonono, what I meant was there was a warning for that problem, but when I couldn't find anything wrong I stopped looking at that and tried stupid stuff with pointers. Like if you're looking for a book you lost, you're looking all over your house, but it turns out it was in the bookshelf all along.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Compilers are not required to complain about such things anyway. In practice, some do and some do not.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Viewing variable memory...
    By xabhi in forum C Programming
    Replies: 3
    Last Post: 08-23-2010, 11:55 AM
  2. Printing memory from variable, is something wrong?
    By Subsonics in forum C Programming
    Replies: 14
    Last Post: 08-29-2009, 11:09 AM
  3. Is This Supposed To Happen? [Dynamic Memory]
    By Dark Dude in forum C Programming
    Replies: 3
    Last Post: 03-21-2009, 05:41 PM
  4. How to Restart or Clean all Variable in memory?
    By sergioms in forum C Programming
    Replies: 5
    Last Post: 01-08-2009, 12:59 PM
  5. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM

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