Thread: What the for loop?

  1. #1
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31

    If printcond Is Greater Than My Understanding Of This Routine

    Hello! New problem here, same program. It used to be a different problem, but now it's that you can't die, or even get a button to become "#". Code is as follows (once I figure out what this looks like, I'll add line breaks so it doesn't stretch the forum so much):

    Code:
    int safestate [10]; // The safe; actually a 1d array of boolean values for whether this number was pressed yet, but for some reason or other 
    C doesn't have bools.
    char safe [10]; // The char representation of the safe.
    int num; // The number that you need to guess. I have no idea how to calculate the worst-case scenario.
    int pressed; // The number that you pressed. Global because it's used by both main and both printcond and exprintcond. <- Technically a 
    legal English statement.
    
    int printcond() // Reports whether the number you pressed is true. If it returns 1, it's not. If it's 2, you guessed right. If it's 3, you blew 
    up the safe.
    {
    	if (pressed == num) // If the number you pressed is right...
    	{
    		return 2; // It doesn't need to do anything else because you won, and it doesn't print safe again until after it was reset.
    	}
    	if ((pressed >= 0) && (pressed <= 9) && (pressed != num)) // On the other hand, if it's wrong...
    	{
    		safestate [pressed]++; // First, it adds one to the amount of times you pressed the button.
    		safe [pressed] = '#'; // Then, it changes the number you pressed into a hash sign. Sort of 
    looks like molten lava, if you use your imagination.
    		if (safestate [pressed] >= 2) // However, if that changes the amount of times you pressed it to more than 1...
    		{
    			return 3; // You die. This should lift you out of both while loops in the main procedure, causing you to die.
    		}
    		num++; // If you didn't die, then num moves over one.
    		if (num == 10) // Rolls num over to 0 if this causes it to become 10.
    		{
    			num = 0;
    		}
    		return 1; // This sufficiently low value should cause you to still be trapped in both while loops.
    	}
    	return 0; // This should allow you to enter the second while loop without getting your safe reset.
    }
    
    int main()
    {
    	char a; // Input for player.
    	setsafe(); // Sets the char representation of the safe to 0, 1, 2 ... 9. Also sets num to a random number.
    	setsafenum(); // Clears the bool(ish) table for how many times a number was pressed.
    	pressed = 11; // Sets pressed to something that won't react with printcond.
    	printf("<LOTS OF TEXT EXPLAINING RULES SNIPPED>");
    	scanf("%c", &a); // Gets the mode you want to play.
    	printf("\n\n"); // For good measure.
    	if (a == 'n') // There's another mode, but it's the exact same thing as this one except it uses exprintcond instead of printcond.
    	{
    		while (printcond() <= 2) // If you exit this, you die.
    		{
    			setsafe();    // These are for executing each
    			setsafenum(); // time you win; it "clears the
    			pressed = 11; // game table", so to speak.
    			while ((printcond() == 1) || (printcond() == 0)) // If you exit this, you either win or die depending on whether 
    printcond returns 2 or 3.
    			{
    				printf("Here's the safe:");
    				printsafe(); // Uses for loops to graphically display safe [0] to safe [9]. No bugs here.
    				printf("\n\nNow, what is the number you wish to pick?\n");
    				scanf("%d", &pressed); // Gets the number you want to put in.
    				if (pressed > 9) // This is to stay away from overflow errors. I only recently thought of this.
    				{
    					while (pressed > 9) // Enters you into a while loop until you press a number within the range.
    					{
    						printf("\nYou can't get ye flask.\n"); // This is for humor.
    						pressed = 11; // I forgot this last time. It wasn't pretty.
    						scanf("%d", &pressed);
    					}
    				}
    				if (printcond() == 1) // It's put in an if statement because if printcond returns 2 from the boot (impossible, 
    I know) you won't have to deal with this crap.
    					printf("\n\nSorry, you guessed incorrectly. Try again. ");
    			}
    			if (printcond() == 2)
    			{
    				printf("Hooray! You guessed it! Let's try again.\n\n"); // Should return you to the beginning of the first while 
    loop.
    			}
    			setsafe();    //
    			setsafenum(); // For good measure.
    			pressed = 11; // Actually, I added the top one when I forgot this was down here.
    		}
    		printf("Aww, man! The safe blew up. Since it killed you, you can't continue using this program. As such:\n\nreturn 0;"); // For 
    humour. I have only achieved this message once.
    		return 0; // Hopefully no bugs here.
    	}
    Last edited by mszegedy; 05-30-2010 at 04:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your for loops test conditions both fail, right off.

    for(i=0; i == 3

    When i=0, will i also == 3?

    NO. So your for loop is never entered.

    Perhaps you wanted:

    for(i=0; i < 3; i++) ??

    this for loop will set your inital array values:

    Code:
    for(i=0; i < 10; i++) {
      safe[i] = i - '0';
    }
    Which also shows how to work with char's, as numbers, if you remember your ASCII values, and your system uses ASCII. (most do).

    Please avoid long lines when inside code tags (it "breaks" the forum page width), and WELCOME to the forum.

  3. #3
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Ah crap. Yep, that's what I forgot. I haven't programmed in C for such a long time. I meant to say "i < 3". Totally forgot. C should have "until" loops…

  4. #4
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    New problem. See edited first post.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int safestate [10]; // The safe; actually a 1d array of boolean values for whether this number was pressed yet, but for some reason or other 
    C doesn't have bools.
    C99 has a boolean type. _Bool in stdbool.h


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Quote Originally Posted by quzah View Post
    Code:
    int safestate [10]; // The safe; actually a 1d array of boolean 
    values for whether this number was pressed yet, but for some reason or other C doesn't have bools.
    C99 has a boolean type. _Bool in stdbool.h


    Quzah.
    I'm using xcode's built-in C compiler. I'm not sure how to make it compile with C99. Plus, I'm not really using it as a bool anymore. I'm using it to store two bits (numbers 0-2), rather than a single true/false value.
    Last edited by mszegedy; 05-30-2010 at 07:25 PM.

  7. #7
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Wait. I fixed something in main. Now it looks like:

    Code:
    int main()
    {
    	char a; // Input for player.
    	setsafe(); // Sets the char representation of the safe to 0, 1, 2 ... 9. Also sets num to a random
    number.
    	setsafenum(); // Clears the bool(ish) table for how many times a number was pressed.
    	pressed = 11; // Sets pressed to something that won't react with printcond.
    	printf("\n\nThis program has just committed pressed = 11.\n\n");
    	printf("<SNIP>");
    	scanf("%c", &a); // Gets the mode you want to play.
    	printf("\n\n"); // For good measure.
    	if (a == 'n') // There's another mode, but it's the exact same thing as this one except it uses
    exprintcond instead of printcond.
    	{
    		while (printcond() <= 2) // If you exit this, you die.
    		{
    			while ((printcond() == 1) || (printcond() == 0)) // If you exit this, you either win or
    die depending on whether printcond returns 2 or 3.
    			{
    				printf("Here's the safe:");
    				printsafe(); // Uses for loops to graphically display safe [0] to safe [9]. No bugs
    here.
    				printf("\n\nNow, what is the number you wish to pick?\n");
    				scanf("%d", &pressed); // Gets the number you want to put in.
    				if (pressed > 9) // This is to stay away from overflow errors. I only recently
    thought of this.
    				{
    					while (pressed > 9) // Enters you into a while loop until you press a
    number within the range.
    					{
    						printf("\nYou can't get ye flask.\n"); // This is for humor.
    						pressed = 11; // I forgot this last time. It wasn't pretty.
    						scanf("%d", &pressed);
    					}
    				}
    				if (printcond() == 1) // It's put in an if statement because if printcond returns
    2 from the boot (impossible, I know) you won't have to deal with this crap.
    					printf("\n\nSorry, you guessed incorrectly. Try again. ");
    			}
    			if (printcond() == 2)
    			{
    				printf("Hooray! You guessed it! Let's try again.\n\n"); // Should return you to
    the beginning of the first while loop.
    				setsafe();    //
    				setsafenum(); // For good measure.
    				pressed = 11; // Actually, I added the top one when I forgot this was down
    here.
    			}
    		}
    		printf("Aww, man! The safe blew up. Since it killed you, you can't continue using this
    program. As such:\n\nreturn 0;"); // For humour. I have only achieved this message once.
    		return 0; // Hopefully no bugs here.
    	}
    Now the problem is that if I put in a number (the wrong number?) then not only does it give me the "sorry, try again" message but it also gives me the "Aww, man! The safe blew up!" message.
    Last edited by mszegedy; 05-30-2010 at 07:54 PM.

  8. #8
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Uh, forget it. I fixed everything. You can close this topic (can you forgive me for posting three times in a row? It's not to increase my postcount, but to alert people that something new has become available in this topic). I'm planning to post this on sourceforge, since I have nothing better to do with it (I did this on a whim).

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    int main() should be int main(void) in C.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by claudiu
    int main() should be int main(void) in C.
    I would recommend this for consistency, but otherwise it is unnecessary since an empty parameter list means the same as a parameter list with only void for a function definition, unless that function definition is also performing the declarative role that a forward declaration would normally perform, but this is almost certainly not the case for the main function.
    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

Popular pages Recent additions subscribe to a feed