Fucntion returns -1, Why?

This is a discussion on Fucntion returns -1, Why? within the C Programming forums, part of the General Programming Boards category; This function that I have returns '-1' whenever it enters the last else can anyone tell me why so I ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    12

    Question Fucntion returns -1, Why?

    This function that I have returns '-1' whenever it enters the last else can anyone tell me why so I can go about fixing it?? (Hope Indentation is correct)

    Perhaps the fucntion wants to return something to main before it calls itself again and defaults to -1 ??? Or is the problem with scanf? Does it not like reading in a value for 'note' again when it already has one stored (previously in main)???

    Code:
    int convert(const char *note){
    
    	if( strcmp(note,"C") == 0 || strcmp( note, "c")==0  ||strcmp( note, "B#" ) == 0 || strcmp( note, "b#")==0)
    		return 0;
    	else if( strcmp( note, "C#" ) == 0 || strcmp( note, "c#")==0||strcmp( note, "Db" ) == 0 || strcmp( note, "db")==0 )
    		return 1;
    	else if( strcmp( note, "D" ) == 0 || strcmp( note, "d")==0)
    		return 2;
    	else if( strcmp( note, "D#" ) == 0 || strcmp( note, "d#")==0||strcmp( note, "Eb" ) == 0 || strcmp( note, "eb")==0 )
    		return 3;
    	else if( strcmp( note, "E" ) == 0 || strcmp( note, "e")==0||strcmp( note, "Fb" ) == 0 || strcmp( note, "fb")==0 )
    		return 4;
    	else if( strcmp( note, "F" ) == 0 || strcmp( note, "f")==0||strcmp( note, "E#" ) == 0 || strcmp( note, "e#")==0 )
    		return 5;
    	else if( strcmp( note, "F#" ) == 0 || strcmp( note, "f#")==0||strcmp( note, "Gb" ) == 0 || strcmp( note, "gb")==0 )
    		return 6;
    	else if( strcmp( note, "G" ) == 0 || strcmp( note, "g")==0 )
    		return 7;
    	else if( strcmp( note, "G#" ) == 0 || strcmp( note, "g#")==0||strcmp( note, "Ab" ) == 0 || strcmp( note, "ab")==0 )
    		return 8;
    	else if( strcmp( note, "A" ) == 0 || strcmp( note, "a")==0 )
    		return 9;
    	else if( strcmp( note, "A#" ) == 0 || strcmp( note, "a#")==0||strcmp( note, "Bb" ) == 0 || strcmp( note, "bb")==0 )
    		return 10;
    	else if( strcmp( note, "B" ) == 0 || strcmp( note, "b")==0||strcmp( note, "Cb" ) == 0 || strcmp( note, "cb")==0 )
    		return 11;
    	
    	else 
    	{
    		printf("Not a Vaild Entry!!!\n");
    		printf("Please enter note AGAIN:");
    		scanf("%s",note);
    		convert(note);		
    		return 0;
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    More code please (like the caller). And bump up the warning level... you can't write to const objects,

    Code:
    scanf("%s",note);
    note is const
    Last edited by zacs7; 12-08-2008 at 03:17 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Yes, it's not possible to say without looking at the function that calls convert() (main() perhaps).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Ok I deleted my last post because i just realised that 'return 0;' should not be in the last else its part of my debugging.

    Even when I remove 'const' it still retruns -1


    here is the rest of the code:

    Code:
    #include<stdio.h>
    
    int convert(char *note);
    
    int main(void){
    
     	char note[40][40];
    	\\static int array[12];	
    	int dArray[12][12];
    	int i, j;
    	
    	for(i=0;i<12;i++)
    	{
    		printf("Please enter note %d in the series:",i+1);
    		scanf("%s",note[i]);
    		dArray[i][0]=convert(note[i]);
    	}
    	
    	printf("\n\t");
    	
    	for(i=0;i<=11;i++)
    	{
    		printf("%d\n\t",dArray[i][0]);
    	}
    	
    	for(i=0;i<=10;i++)
    	{ 
    		dArray[0][i+1]=(((dArray[0][i]+(dArray[i][0]-dArray[i+1][0])+12)%12));
    	}
    	
    	for(i=1;i<=11;i++) 
    	{
    		for(j=1;j<=11;j++) dArray[i][j]=(dArray[i][j-1] + (dArray[i-1][j]-dArray[i-1][j-1])+12)%12;
    	}
    		 
    	printf("\n***** Equivalent Serial Matrix *****\n");
    	for(j=0;j<12;j++)
    	{
    		printf("\n");
    		for(i=0; i<12; i++) 
    		printf("%d ",dArray[i][j]);
    	}
    
    printf("\n");
    
    }
    
    int convert(char *note){
    
    	if( strcmp(note,"C") == 0 || strcmp( note, "c")==0  ||strcmp( note, "B#" ) == 0 || strcmp( note, "b#")==0)
    		return 0;
    	else if( strcmp( note, "C#" ) == 0 || strcmp( note, "c#")==0||strcmp( note, "Db" ) == 0 || strcmp( note, "db")==0 )
    		return 1;
    	else if( strcmp( note, "D" ) == 0 || strcmp( note, "d")==0)
    		return 2;
    	else if( strcmp( note, "D#" ) == 0 || strcmp( note, "d#")==0||strcmp( note, "Eb" ) == 0 || strcmp( note, "eb")==0 )
    		return 3;
    	else if( strcmp( note, "E" ) == 0 || strcmp( note, "e")==0||strcmp( note, "Fb" ) == 0 || strcmp( note, "fb")==0 )
    		return 4;
    	else if( strcmp( note, "F" ) == 0 || strcmp( note, "f")==0||strcmp( note, "E#" ) == 0 || strcmp( note, "e#")==0 )
    		return 5;
    	else if( strcmp( note, "F#" ) == 0 || strcmp( note, "f#")==0||strcmp( note, "Gb" ) == 0 || strcmp( note, "gb")==0 )
    		return 6;
    	else if( strcmp( note, "G" ) == 0 || strcmp( note, "g")==0 )
    		return 7;
    	else if( strcmp( note, "G#" ) == 0 || strcmp( note, "g#")==0||strcmp( note, "Ab" ) == 0 || strcmp( note, "ab")==0 )
    		return 8;
    	else if( strcmp( note, "A" ) == 0 || strcmp( note, "a")==0 )
    		return 9;
    	else if( strcmp( note, "A#" ) == 0 || strcmp( note, "a#")==0||strcmp( note, "Bb" ) == 0 || strcmp( note, "bb")==0 )
    		return 10;
    	else if( strcmp( note, "B" ) == 0 || strcmp( note, "b")==0||strcmp( note, "Cb" ) == 0 || strcmp( note, "cb")==0 )
    		return 11;
    	
    	else 
    	{
    		printf("Not a Vaild Entry!!!\n");
    		printf("Please enter note AGAIN:");
    		scanf("%s",note);
    		convert(note);			
    	}
    }

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    main() is not returning an explicit value. Surely you are getting compiler warnings that you are not addressing.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    	else 
    	{
    		printf("Not a Vaild Entry!!!\n");
    		printf("Please enter note AGAIN:");
    		scanf("%s",note);
    		return convert(note);
    		}
    }
    recursion tip
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, it appears to work for me.

    A few warnings:
    implicit declaration of strcmp (you need to include <string.h>).

    Control reaches end of both non-void functions. Main should return 0 and well... convert probably shouldn't recurse potentially infinitely.

    I would suggest that you return the final else part and move it into main. It is not normally a task of functions to keep whining about bad input, that's what the caller should whine about.

    There may be other things to do: converting tolower/toupper would halve the comparisons. Storing the notes/return values in some kind of array (of structs) would let you use a loop for the lookup.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    No warnings at all ????????? I'm using gcc from the command line and notepad :-/ Can you explain what you just replied with sorry i'm a bit of a newbie to this C lark.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    try
    Code:
    gcc -Wall -Wextra -pedantic -ansi asdf.c -o asdf
    That should give you plenty of warnings.
    Last edited by cyberfish; 12-08-2008 at 04:12 PM. Reason: forgot this is the C forum

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    else 
    	{
    		printf("Not a Vaild Entry!!!\n");
    		printf("Please enter note AGAIN:");
    		scanf("%s",note);
    		return convert(note);
    		}
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This could also look like this:
    Code:
    else 
    	{
    		printf("Not a Vaild Entry!!!\n");
    		printf("Please enter note AGAIN:");
    		scanf("&#37;s",note);
    		x=convert(note);
                    return x;
    		}                 // hmmm, no return after this else
    }
    Don't worry, that's how it suppose to be. I think if you try -Wall gcc would have filled you in.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    For all the trouble i went to in installing gcc it hasn't impressed me so far i should know about these warnings!!!

    Question if i use Borland C++ will i be able to program in C normally and will it allow me to do things that I wouldn't normally be able to do in just a C compiler? Can i set it up only to allow me to program in C?

    The 'return convert(note)' tip works but I think I will do what anon says and keep that else in main.

    I will also need to prevent the user from entering the same note twice so an array of structs is a good idea.

    Thank you.

    EDIT:
    So what exactly should i be typing if my program is named 'note'

    'Wall - gcc -o note note.c' ?????????

    Is there any simple compiler that I can install and use that doesn't involve me learing off lists of commands?? ie If i want to compiler i should click a button if i want to run i should click run????? Why is C still in the dark ages, all this gcc lark is what prevents people from getting stared easily. For insatnce there is BlueJ for java.
    Last edited by Taper; 12-08-2008 at 04:30 PM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Taper! don't go crazy!

    try:

    gcc -Wall -Fix_my_code -awesome -o note note.c
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    The compiler comes first on the command line:
    Code:
    gcc -Wall -o note note.c

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Butt, joke, me...


    edit:
    Thanks rags

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

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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