Thread: New to C, need help understanding errors

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Question New to C, need help understanding errors

    I am new to C and I have to create a program that will take an array, input by the keyboard, and sort it in ascending order. I know there are easier ways but the way I have chosen will get me extra credit in the class which I need. I feel like I am getting close but I keep getting these two errors and I do not understand how to fix them.

    If anyone has any suggestions or help it would be much appreciated.

    Here are the two errors I am getting

    Error 1 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\me\my documents\visual studio 2005\projects\array\array\array.cpp 13

    and

    Error 2 fatal error C1004: unexpected end-of-file found c:\documents and settings\me\my documents\visual studio 2005\projects\array\array\array.cpp 63


    and here is my source code:

    Code:
    // Array.cpp : Defines the entry point for the console application.
    //
    
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    #define x 10
    
    int get_min_range(int list[], int first, int last);
    	
    {
    
    	first=0;
    	
    	for(last=1; last<x; ++last){
    		
    		if (arr[last,arr[first]){
    			first=last;
    	
    		return(first)
    }
    
    
    void sort_arr(int arr[], int n)
    {
    
    	int f,           
    	  temp,           
              index_of_min;  
    
          for (f = 0; f < x-1; ++f) {
              index_of_min = get_min_range(arr, f, x-1);
    
              if (f != index_of_min) {
                    temp = arr[index_of_min];
                    arr[index_of_min] = arr[f];
                    arr[f] = temp;
    }
    
    
    
    int main()
    {
    	
    int arr[x], b=0;
    
    	
    for(int n=0; n<x; n++)
    {
    	printf("Enter a number in the array:");
           scanf_s("%d", &arr[n]);
    }
    
    sort_arr(arr);
    
    for(b=0; b<x; b++)
    printf("%d", arr[b]);
    getch();
    return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    int get_min_range(int list[], int first, int last);
    Note the semi-colon that makes this a function prototype instead of the start of a function definition.
    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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    thank you so much I cant believe I over looked that.

    I fixed it and then I had a few simple syntax errors
    I fixed them and now I am getting these errors

    Error 1 error C2601: 'sort_arr' : local function definitions are illegal c:\documents and settings\me\my documents\visual studio 2005\projects\array\array\array.cpp 29

    Error 2 fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\me\my documents\visual studio 2005\projects\array\array\array.cpp(35)' was matched c:\documents and settings\me\my documents\visual studio 2005\projects\array\array\array.cpp 65

    Im not sure about the first one and I have no Idea on what the second one means.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indent your code properly and you will find that your braces do not match.
    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

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Thanks that fixed all the errors now the program is not sorting the numbers in the array it is just printing them in the order I input them. Any ideas why? Here is the new source code:

    Code:
    // Array.cpp : Defines the entry point for the console application.
    //
    
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    #define x 10
    
    int arr[x], n, temp;
    
    
    int get_min_range(int list[], int first, int last)
    	
    {
    
    	first=0;
    	
    	for(last=1; last<x; ++last)
    		
    		if (arr[last],arr[first])
    			first=last;
    	
    			return(first);
    }
    
    
    void sort_arr(int arr[], int n)
    {
    
    	int f, temp=0, index_of_min;  
    
          for (f = 0; f < x-1; ++f) 
              index_of_min = get_min_range(arr, f, x-1);
    
              if (f != index_of_min) 
                    temp = arr[index_of_min];
                    arr[index_of_min] = arr[f];
                    arr[f] = temp;
    }
    
    
    
    int main()
    {
    	
    	int f=0;
    
    	
    		for(int n=0; n<x; n++)
    		{
    				printf("Enter a number in the array:");
    				scanf_s("&#37;d", &arr[n]);
    		}
    
    		sort_arr( arr, n);
    
    			for(f=0; f<x; f++)
    				printf("%d", arr[f]);
    	getch();
    	return (0);
    }

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    here is my output window:

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your indentation is still a little off, and consequently it is misleading. It should be along these lines instead:
    Code:
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    #define x 10
    
    int arr[x], n, temp;
    
    int get_min_range(int list[], int first, int last)
    {
        first=0;
    
        for (last=1; last<x; ++last)
            if (arr[last],arr[first])
                first=last;
    
        return(first);
    }
    
    void sort_arr(int arr[], int n)
    {
    
        int f, temp=0, index_of_min;
    
        for (f = 0; f < x-1; ++f)
            index_of_min = get_min_range(arr, f, x-1);
    
        if (f != index_of_min)
            temp = arr[index_of_min];
    
        arr[index_of_min] = arr[f];
        arr[f] = temp;
    }
    
    int main()
    {
        int f=0;
    
        for (int n=0; n<x; n++)
        {
            printf("Enter a number in the array:");
            scanf_s("&#37;d", &arr[n]);
        }
    
        sort_arr( arr, n);
    
        for (f=0; f<x; f++)
            printf("%d", arr[f]);
        getch();
        return (0);
    }
    However, before we continue, turn your global variables into local variables.
    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

  8. #8
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    if (arr[last],arr[first])
    What are you trying to do here? This doesn't exactly do anything important, and it certainly doesn't do what you want it to.

    Also, in that same function, you seem to pass in a value 'first', and then you immediately set it to zero. Why bother passing it at all? The same goes with last, which is immediately set to 1 in your for loop.
    Last edited by JDGATX; 04-10-2008 at 01:23 PM.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Is this looking any better? I cleaned it up a little and changed the variables but im still having the same problem.

    Code:
    // Array.cpp : Defines the entry point for the console application.
    //
    
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    #define x 10
    
    int get_min_range(int list[], int first, int last)
    	
    {	
    	int arr[x];
    
    	first=0;
    	
    	for(last=1; last<x; ++last)
    		if (arr[last],arr[first])
    			first=last;
    	
    	return(first);
    }
    
    
    void sort_arr(int arr[], int n)
    {
    
    	int f, temp=0, index_of_min;  
    
        for (f = 0; f < x-1; ++f) 
    		index_of_min = get_min_range(arr, f, x-1);
    
        if (f != index_of_min) 
    		temp = arr[index_of_min];
        arr[index_of_min] = arr[f];
    	arr[f] = temp;
    }
    
    
    
    int main()
    {
    	
    	int f=0, n=0, arr[x];
    
    	
    	for(int n=0; n<x; n++)
    	{
    		printf("Enter a number in the array:");
    		scanf_s("&#37;d", &arr[n]);
    	}
    
    	sort_arr( arr, n);
    
    	for(f=0; f<x; f++)
    		printf("%d", arr[f]);
    	getch();
    	return (0);
    }

  10. #10
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    See my suggestions above.

    Also, you removed your global variables. But you're defining them in the wrong place. Your arr[x] should be defined in main() and passed to your get_min_range() as the list[] variable. Currently, you're not using list[] for anything in that function.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Ok this is the part I am really having trouble with:

    Code:
    int get_min_range(int arr[], int first, int last)
    	
    {	
    
    	
    	
    	for(last<x; ++last;)
    		if (arr[last],arr[first])
    			first=last;
    	
    	return(first);
    }

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    yeh I saw that and fixed it list was the example used in the book and and I guess I just had it on my mind then when I tried to build it it said arr[] was unidentified

  13. #13
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Pay attention to what I told you. That if statement in the above function doesn't do anything close to what you want it to. It's not logical C, and my compiler throws a warning on it because it's pointless. Think about what you're trying to do there and rethink the if condition.

    Go read the FAQ on for loops. This for loop isn't right at all.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Ok so I looked at the example and thanks to laserlights help with the indention problem I could see a few more problems this is my new get_min_range but it still doesn't do what I need it too:
    Code:
    int get_min_range(int arr[], int first, int last)
    	
    {	
    
    	first=0;
    	
    	for(last=1; last<x; ++last)
    		if (arr[last]<arr[first])
    			first=last;
    	
    	return(first);
    }

  15. #15
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    arr[f], when used outside your for loop, will always mean that f will be equal to x-1. This isn't what you want.

    And in your sort function, what is the purpose of the n parameter that you pass to it?

    Honestly, I don't think your problem is solely in get_min_range. It's in sort_arr too.
    Last edited by JDGATX; 04-10-2008 at 01:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 07:11 PM
  2. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM