Thread: Not sure why this doesn't work...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Not sure why this doesn't work...

    Basically I'm trying to make this program so if you change the value of cupOfMarbles it will count up and print 5...6...7...8...9...10 and then stop (in this example) or if it is over 10 it will count down until the loop reaches 10. Nothing happens though, no errors on the compile either so my logic must be off or values aren't being passed. Any suggestions on what I need to do to fix this (without getting too complicated) I'm still learning

    Thanks


    Code:
    #include <stdio.h>
    
    void UnderTen( void );   // cupOfMarbles is under 10
    void OverTen( void );    // cupOfMarbles is over 10
    
    int cupOfMarbles;
    
    int main (int argc, const char * argv[]) {
    
    	cupOfMarbles = 5;	 // change this value to get different results
    	
    	if ( cupOfMarbles == 10 )
    		printf( "You already have 10 marbles in this cup!\n" );
    	
    	if ( cupOfMarbles < 10 )
    		UnderTen();
    		
    	if ( cupOfMarbles > 10 )
    		OverTen();
    
        return 0;
    }
    
    
    void UnderTen( void ) {
    	int i;
    for ( i = cupOfMarbles ; i == 10; i++ )
    	printf( " There are currently %d marbles in the cup!\n", i );
    }
    
    
    void OverTen( void ) {
    	int i;
    for ( i = cupOfMarbles ; i == 10; i-- )
    	printf( "There are currently %d marbles in the cup!\n", i );
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lucidrave View Post
    values aren't being passed

    Code:
    #include <stdio.h>
    	if ( cupOfMarbles < 10 )
    		UnderTen(COMPLETE LACK OF VALUE HERE);
    		
    	if ( cupOfMarbles > 10 )
    		OverTen(COMPLETE LACK OF VALUE HERE);
    No kidding.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    well

    there is no value in there because i'm just calling a function... if its under 10 then i call the function UnderTen();

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lucidrave View Post
    there is no value in there because i'm just calling a function... if its under 10 then i call the function UnderTen();
    And has that worked for you for any other functions? Do you just call printf() and not put anything in the parentheses?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    I even simplified the code as much as I could (no function calls) and it only works if the value is 10... anything lower or higher doesn't work. Is my logic flawed here? Please help

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
    
    	int cupOfMarbles;
    
    	cupOfMarbles = 9;	 // change this value to get different results
    	
    	if ( cupOfMarbles == 10 ) {
    		printf( "You already have 10 marbles in this cup!\n" );
    	}
    	
    	
    	if ( cupOfMarbles < 10 ) {	
    		for ( cupOfMarbles ; cupOfMarbles == 10; cupOfMarbles++ )
    			printf( "There are currently %d marbles in the cup!\n", cupOfMarbles );
    	}
    		
    	
    	if ( cupOfMarbles > 10 ) {
    		for ( cupOfMarbles ; cupOfMarbles == 10; cupOfMarbles-- )
    			printf( "There are currently %d marbles in the cup!\n", cupOfMarbles );
    	}
    
        return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For loops only work while the middle statement is true.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    And has that worked for you for any other functions? Do you just call printf() and not put anything in the parentheses?
    What are you talking about? There are functions that don't take parameters... his don't.

    lucidrave: the reason your original program doesn't work the way you expect is that your for loops are wrong. UnderTen() gets called if cupOfMarbles is under 10, but the loop is set to go if cupOfMarbles is equal to 10. That wont work. Your for loop in UnderTen() should be:
    Code:
    for ( i = cupOfMarbles ; i <= 10; i++ )
    I'll let you figure out the OverTen() function.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bithub View Post
    What are you talking about? There are functions that don't take parameters... his don't.
    It hadn't occurred to me that he would use a global variable to avoid passing a parameter to a function.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It hadn't occurred to me that he would use a global variable to avoid passing a parameter to a function.
    From the difficultly level of his problem, I'd say it's quite possible he hasn't even learned about passing parameters yet.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    Yeah! It works now! Thanks for the info guys, bithub's your tip got it working. But tabstop reminded me that for only works when a statement is true.
    Code:
    There are currently 5 marbles in the cup!
    There are currently 6 marbles in the cup!
    There are currently 7 marbles in the cup!
    There are currently 8 marbles in the cup!
    There are currently 9 marbles in the cup!
    There are currently 10 marbles in the cup!

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    yeah, the next chapter in the book i'm reading talks about pointers and parameters... i haven't quite got there yet. i'm trying to master the different types of loops and understand when it is best to use them before i move on.

    thanks again.

    any extra advice is always helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM