Thread: function problem

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

    function problem

    this is the code i am trying to run and i keep getting am error on the line
    if (values[i]>=50 && values[i] <=100)
    which says: subscript requires array or pointer type
    I am lost at what to do...
    Please help...
    here is the whole code
    thanks much

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    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();	
    	}
    
    	system("pause");
    }
    
    
    int isValid (int input) {
    	int result,i;
    int values ();
    	if (values[i]>=50 && values[i] <=100)
    
    		result= TRUE;
    
    
    	else 
    		result= FALSE;
    	return result;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    int values ();
    This line is your problem. This is taken as a function prototype declaration with limited scope.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1.
    main()

    should be
    int main

    2. indentation should be consistent
    3. function should be declared before use like

    int isValid(int * values, int index);

    4. to pass array you prototype the function as in 3 not as you did
    5. you actually - do not need to pass array, instaed - you could pass a value
    isValide(values[i]);
    in this case you prototype will be
    int isValide(int input);
    (prototype should be locate before main)
    6. your function should use the input parameter and check its value
    7. int values (); - this is prototype of the function that takes unknon number of parameters and returns int - this is definitely not what you want - it should be removed, as well as int i from your isValid function
    8. for the index better to use size_t type not int
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i tried doing what you said and i am still not getting it.
    this is my first time taking a c class so I am having a ilttle more problems
    here is the new code
    please help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(int number);
    
    
    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("&#37;i",&values[i]);
    	
    		valid =isValid(values, i);
    		//unique=isUnique();
    		printf("%i",result);
    	}
    
    	system("pause");
    }
    
    int isValid(values[i]{
    	int result;
    	if (values[i]>=50 && values[i] <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1
    main()
    should be
    int main(void)

    2
    isValid(values, i);
    shoulld be
    isValid(values[i]);

    3
    Code:
    int isValid(values[i]{
    should be
    Code:
    int isValid(int input){
    4
    DO NOT use values in your isValid function - use input variable

    5. fix your indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    this is what i have
    no luck though...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(values[i]);
    
    
    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",result);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }

  7. #7
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    Try This code....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TRUE 1
    #define FALSE 0
    #define SIZE 5
    //prototype
    int isValid(int *, int);
    int 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("&#37;i",&values[i]);
    	
    		valid =isValid(values, i);
    		//unique=isUnique();
    		printf("%i\n",valid);
    	}
    
    	system("pause");
    }
    
    int isValid(int* values, int i){
    	int result;
    	if ((values[i]>=50) && (values[i]<=100))
    		result= TRUE;
    	else 
    		result= FALSE;
    	return result;
    }

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i know you said that
    main()
    should be
    int main(void)
    but we still have not learned in class about it, so I can not use it
    Is there anything else that I might be able to use?

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    there was two error in code...
    1.Function declaration
    2.printf(“&#37;d”,result);----> it should be printf(“%d”,valid);

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this is what i have
    no luck though...
    Why do you take what is fine and change it, while leaving what is has to be fixed intact?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(int input);
    
    
    int main(void){
    
    	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("&#37;i",&values[i]);
    
    		valid =isValid(values[i]);
    		//unique=isUnique();
    		printf("%i",result);
    	}
    
    	system("pause");
    }
    
    int isValid(int input){
    	int result;
    	if (input>=50 && input <=100)
    		result= TRUE;
    
    	else 
    		result= FALSE;
    	return result;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    check your function declaration ..there should be two argumnets..
    int isValid(int *, int);
    because you are passing the address of the array not the value..
    valid =isValid(values, i);

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by krishna View Post
    check your function declaration ..there should be two argumnets..
    int isValid(int *, int);
    because you are passing the address of the array not the value..
    valid =isValid(values, i);
    It is opposite - it should be one argument and should be passed one value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    hiiii vart acctualy main problem in the..
    printf("&#37;i",result); statement because he/she has define it as 0 in main so it wll always print the same value whatever the value return by the function

  14. #14
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    yaa ..we can correct by both way either change in function or change in call declaration

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is clear is that the isValid function is used in a confusing way - goran00, you need to make sure that you decide what you want to do, either:
    - Give the function one value to check (e.g. value[i])
    - Pass it an array and a "current index".

    The current solution is trying to do both at the same time, and like most cases when someone mixes two things together, it doesn't quite work out right for either solution.

    I personally think the "give it one value" seem a better solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM