What's wrong???

This is a discussion on What's wrong??? within the C Programming forums, part of the General Programming Boards category; Code: scanf("%d",i); switch(i) { case 1: text2code(); break; case 2: code2text(); break; default: printf("Not a valid input"); break; } I ...

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    What's wrong???

    Code:
    	scanf("%d",i);
    
    	switch(i)
    	{
    		case 1:
    		text2code();
    		break;
    
    		case 2:
    		code2text();
    		break;
    		
    		default:
    		printf("Not a valid input");
    		break;
    	}
    I try to input a number, put the program has an error and crashes.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm assuming i is an integer and as such scanf should be:
    Code:
    scanf("%d", &i);

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    new problem
    Code:
    	int i;
    	system("cls");
    	printf("What Code?\n\nA. Sphere Code\nB. New Code\n: ");
    	i = getc(stdin);
    I say to get a char, put the program skips that and automatically exits! I've checked this many times, and can't find an error.

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    My book uses intergers to store single characters, and getc() or getchar() to get them.

    Example, this works:
    Code:
    	int i;
    	system("cls");
    	printf("CODE CRYPTATION\nBy Samuel Fredrickson\n\n");
    	printf("A. Text -> Code\nB. Code -> Text\n\n: ");
    	i = getchar();

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Mixxing scanf and getchar can have undesirable results. Look into using fgets instead of scanf

  6. #6
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    I'm not using scanf anymore!

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    even this doesn't work!

    Code:
    void text2code()
    {
    	char i;
    	char *ptr_i = &i;
    	system("cls");
    	printf("What Code?\n\nA. Sphere Code\nB. New Code\n: ");
    	fgets(ptr_i,1,stdin);
    	/*
    	if(i == 'a')
    	{
    		text2sphere();
    	}
    	else if(i == 'b')
    	{
    		text2new();
    	}*/
    	printf("At End");
    	return;
    }
    It's like, for some reason, it's skipping ALL input functions! Why???

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    fgets(ptr_i,1,stdin);
    Read the man page for fgets. It will get size - 1 and append a null character to the end. So in that case you are getting 0 bytes from the input.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by kinghajj
    even this doesn't work!
    Code:
    void text2code()
    {
    	char i;
    	char *ptr_i = &i;
    	system("cls");
    	printf("What Code?\n\nA. Sphere Code\nB. New Code\n: ");
    	fgets(ptr_i,1,stdin);
    It's like, for some reason, it's skipping ALL input functions! Why???
    Probably because you've got stuff left over in your input stream. Next, don't read single characters with fgets. It defets the purpose of fgets. Fgets is used to read strings, which are null terminated. Thus, you need at least 2 characters (ie: an array), not one.

    Clear stdin first.

    while( fgetc(stdin) != '\n' );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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