true/false

This is a discussion on true/false within the C Programming forums, part of the General Programming Boards category; i am trying to make the following code work, but it always returns 0. what am i doing wrong? Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    true/false

    i am trying to make the following code work, but it always returns 0.
    what am i doing wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(input);
    
    
    main(){
    
    	int values [SIZE]={0};
    	int count;
    	int i;
    	int valid;
    	int result;
    	count=0;
    	result=0;
    
    
    
    	for (i=0; i<SIZE; i++){
    		printf("Enter an integer (50-100):\n");
    		scanf("%i",&values[i]);
    
    		valid =isValid(values[i]);
    		//unique=isUnique();
    		printf("%i\n",result);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    As was said several times

    Code:
    valid =isValid(values[i]);
    printf("&#37;i\n",result);
    Red variable should be the same.
    You update one var - and print another

    remove result var from main
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thank you
    i am still very new to this so it's taking me a little longer to figure certain things out.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    also i am trying to figure out how to make the program run until it has 5 valid numbers
    how would i go about doing that

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    one possible way - when entered number is not valid - decrease i
    Code:
    valid =isValid(values[i]);
    if(!valid)
       i--;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    int main(){
    
       int values [SIZE]={0};
       int i = 0;
    
       while (i < SIZE) {
          printf("Enter an integer (50-100): ");
          scanf("%i",&values[i]);
    
          if (isValid(values[i]))
             ++i;
          else
             printf("Invalid input\n");
       }
    
       for (i = 0; i < SIZE; ++i)
       {
          printf("Result %d: %d\n", i, values[i]);
       }
    
       getchar();
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    if i put that in the bottom part of the program wouldnt that assume i to be a different variable

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    rags_to_riches i know how to make the program work that way, but for my assignmen we need to make two seperate functions so i am not able to do it the way that you did it.

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by goran00 View Post
    also i am trying to figure out how to make the program run until it has 5 valid numbers
    how would i go about doing that
    So you want to make the program get 5 valid values using two seperate functions. The one that checks whether the input is valid is done. And the other should signal that 5 valid values have been entered? Try using a static int as a counter inside a function.
    Hint:
    Code:
    void foo()
    {
        static int called = 0;
        called++;
        printf("I have been called &#37;d times.\n", called);
    }
    
    int main()
    {
        /* We are going foo here.... */
        foo(); foo(); foo();
        foo();/******/foo();
        foo(); foo(); foo();
       /* The last line should say something about 8 times. */
       return 0;
    }
    How to adopt this construction to your code? When the call to isValid is true, call your
    function, let's say it is called fiveValid which increases the counter, and returns FALSE if it is not 5, and TRUE otherwise. Would that go as a logical solution to your program?
    Last edited by xuftugulus; 03-07-2008 at 04:02 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by vart View Post
    one possible way - when entered number is not valid - decrease i
    Code:
    valid =isValid(values[i]);
    if(!valid)
       i--;
    That won't work when i is zero.

    Maybe loop until you get 5 valids. You'd need a counter for the valids.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    10

    Lightbulb

    In order to make sure you have 5 valid numbers try something like this:
    Code:
          int valid; 
          for (i=0; i<SIZE; i++){
    		printf("Enter an integer (50-100):\n");
                    valid = getValid();
                    values[i] = valid;
    	}
    
    int getValid(){
        int retNum;
        
       scanf("&#37;d",&retNum);
       while(retNum < 50 || retNum > 100){
          printf("Enter a valid number: ");
          scanf("%d",&retNum);
       }
       return retNum;
    }
    -Dustin
    http://www.theCprogrammer.com

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by DaveH View Post
    That won't work when i is zero.
    And why not? What is so different about 0?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by vart View Post
    And why not? What is so different about 0?
    If i is zero and the array element at i is not valid, i gets decreased to -1. I'm not exactly sure what happens then. Since i is out of bounds of the loop parameters does the loop exit? If the loop doesn't exit it would try to access the array element at -1 index. Not good.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by DaveH View Post
    If i is zero and the array element at i is not valid, i gets decreased to -1. I'm not exactly sure what happens then. Since i is out of bounds of the loop parameters does the loop exit? If the loop doesn't exit it would try to access the array element at -1 index. Not good.
    there is no such thing as bounds of loop parameter. There is loop condition that is checked before loop is entered
    Code:
    for(i=0;i<10;i++)
    {
       i--;
    }
    is equivalent to
    Code:
    i=0;
    while(i<10)
    {
       i--;
       i++;
    }
    so it will not exit only due to i somethere in the loop getting to be negative
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by vart View Post
    there is no such thing as bounds of loop parameter. There is loop condition that is checked before loop is entered
    Code:
    for(i=0;i<10;i++)
    {
       i--;
    }
    is equivalent to
    Code:
    i=0;
    while(i<10)
    {
       i--;
       i++;
    }
    so it will not exit only due to i somethere in the loop getting to be negative
    Thats what I thought, but wasn't sure. So i would be -1 and it would try to access the array. That would be attempting array access with an out of bounds index.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 09:10 AM
  2. FMOD implimentation, how'd you do it?
    By Jeremy G in forum Game Programming
    Replies: 10
    Last Post: 06-12-2004, 12:04 AM
  3. evaulate strcmp true/false
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-13-2002, 04:24 PM

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