Thread: Skipping

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Skipping

    This has happened more than once now.... perhaps I should show you some code:

    Code:
            char choice;
    	float number1, number2;
    	printf("Type in \'+\' for addition, \'-\' for subtraction,");
    	puts(" \'/\' for division, \'*\' for multiplication, and \'^\' for power");
    
    	printf("Please enter your choice: ");
    	scanf("%c",&choice);
    	printf("Now enter the two numbers IN ORDER e.g. 5 then 2 for 5-2: ");
    	scanf("%f", &number1);
    	scanf("%f", &number2);
    For some reason. This bit of code was fine when it was in main() and when my entire program was in one file. But now, whenever the function containing this code is called. Instead of doing this:

    Type in '+' for addition, '-' for subtraction, '/' for division, '*' for multiplication, and '^' for power

    Please enter your choice: pause for entry here

    etc...
    It is falling through and outputting this:

    Type in '+' for addition, '-' for subtraction, '/' for division, '*' for multiplication, and '^' for power
    Please enter your choice: No pause for choice entry, instead it continues to this: Now enter the two numbers IN ORDER e.g. 5 then 2 for 5-2: NOW it pauses for entry but everything is messed up because it skipped the first scanf for some reason
    If someone could please explain what the deal is, I'd GREATLY appreciate it. It was/is the only error in my newly made calculator program.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're running into the old familiar scanf problem. It leaves the newline in the buffer, so it reads that for the next scanf call. You can use parameters to sort of fix this. Or you can use better methods, such as using fgets in combination with things like sscanf. Search the board for scanf fgets sscanf, or read the FAQ, specificly the section on reading a line or number.

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

  3. #3
    Hey the way I usually fix this is putting another printf() call behind it. Example:

    Code:
    // stuff
    
    printf("");  // extra
    // then
    printf("Please enter your choice: ");
    scanf("%c",&choice);
    But thats just an implemntation to use if you want to keep using scanf();


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Well, what's odd is that I did the fgets-sscanf combo and it did the same thing. I'll try Stack Overflow's approach....


    Still did it
    Last edited by Padawan; 04-03-2004 at 10:16 PM.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    try adding this line beneath every occurance of scanf
    while((temp = getchar) != EOF && temp != '\n'); // <--- must have semicolon
    This should clean out the input buffer.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    then you could just add to tzuchan's suggestion and write your own function to replace scanf:
    Code:
    int mygetchar()
    {
       int c,t;
       c=getchar();
    
       while((t=getchar()) != EOF && t != '\n')
                 ;
    
        return c;
    }

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You might want to look at this and this for some thoughts on getting user input, both text and numeric.

    ~/

  8. #8
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    I just tried the function on the page and used it how he did on the 2nd page (thanks for those btw) But it's STILL skipping over the first input spot. Let me show you what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void selection();	
    
    
    
    void selection()
    {
    	char temp, choice[10], number1[10],number2[10];
    	int n,n2,ch;
    	printf("Type in \'+\' for addition, \'-\' for subtraction,");
    	puts(" \'/\' for division, \'*\' for multiplication, and \'^\' for power");
    	printf("Please enter your choice: ");
    	getline(choice, 10);
    	ch = atoi(choice);
    	printf("Now enter the two numbers IN ORDER e.g. 5 then 2 for 5-2: ");
    	getline(number1, 10);
    	n = atoi(number1);
    	getline(number2, 10);
    	n2 = atoi(number2);
    /*#####################################*/
    /*#####################################*/
    /*Switch statement uses ASCII codes*/
    /*#####################################*/
    /*#####################################*/
    	switch (ch)
    	{
    		case 43:
    		puts("Calculating...");
    		addition(n,n2);
    		break;
    		case 45:
    		puts("Calculating...");
    		subtraction(n,n2);
    		break;
    		case 47:
    		puts("Calculating...");
    		division(n,n2);
    		break;
    		case 42:
    		puts("Calculating...");
    		multiplication(n,n2);
    		break;
    		case 94:
    		puts("Calculating...");
    		power(n,n2);
    		break;
    		default:
    		puts("Please enter one of the operations listed ONLY");
    		break;
    	}
    }
    The getline function definition is in a header file included by the main file of the program. I don't think that is the problem anyway. It keeps skipping over just like I posted in the first posting in this thread.
    Last edited by Padawan; 04-04-2004 at 01:47 PM.

  9. #9
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    atoi converts a character array to an integer if possible, it does not return an ascii value
    what you want is an ascii value

    you must use a loop and skip over the ascii values that are irrelevent

    then, like a fox, pluck out that pimento from the character cherry-array

    then your switch will begin to work

    but that is only the beginning
    Last edited by ggs; 04-04-2004 at 01:49 PM. Reason: fixed spelling like a cheetah - was referring to first atoi btw
    .sect signature

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    show us getline(). I see nothing really wrong with what you've shown.

    Also you should not use
    Code:
    while((t=getchar()) != EOF && t != '\n');
    unless you know there is something to flush, otherwise you will cause the program to wait until the user hits enter again.

  11. #11
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Code:
    int getline(char line[], int max)
    {
    int nch = 0;
    int c;
    max = max - 1;			/* leave room for '\0' */
    
    while((c = getchar()) != EOF)
    	{
    	if(c == '\n')
    		break;
    
    	if(nch < max)
    		{
    		line[nch] = c;
    		nch = nch + 1;
    		}
    	}
    
    if(c == EOF && nch == 0)
    	return EOF;
    
    line[nch] = '\0';
    return nch;
    }

  12. #12
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    printf("Please enter your choice: ");
    getline(choice, 10);
    ch = atoi(choice);

    CH DOES NOT CONTAIN A SINGLE ASCII VALUE AFTER THIS

    PLEASE TO BE REPAIRING IT!!!!! ??!
    .sect signature

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The problem lies outside the function.

    However has ggs pointed out the use of atoi on the first selection is incorrect. use a simple getchar() and use '+', '-', etc instead of the ascii numbers.

    Also there is a problem with how you are wording
    Code:
     printf("Now enter the two numbers IN ORDER e.g. 5 then 2 for 5-2: ");
    where in you are asking them to enter the two numbers but you did not specify that the user must enter one number, hit enter, and then enter the other number.

    CH DOES NOT CONTAIN A SINGLE ASCII VALUE AFTER THIS
    Actually ch should contain 0 which is a valid ascii character. It may not be useful but its valid

  14. #14
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Quote Originally Posted by ggs
    printf("Please enter your choice: ");
    getline(choice, 10);
    ch = atoi(choice);

    CH DOES NOT CONTAIN A SINGLE ASCII VALUE AFTER THIS

    PLEASE TO BE REPAIRING IT!!!!! ??!
    haha I just realized that. Sorry, I'm just kinda waking up. I'll try the getchar thing that Thantos said.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void selection();	
    
    
    
    void selection()
    {
    	char choice, number1[50], number2[50];
    	float n, n2;
    	printf("Type in \'+\' for addition, \'-\' for subtraction,");
    	puts(" \'/\' for division, \'*\' for multiplication, and \'^\' for power");
    	printf("Please enter your choice: ");
    	choice = getchar();
    	printf("Now enter the two numbers IN ORDER e.g. 5 then 2 for 5-2.");
    	printf("Seperate each number with a newline (press enter)\n");
    	getline(number1, 50);
    	n = atof(number1);
    	getline(number2, 50);
    	n2 = atof(number2);
    	switch (choice)
    	{
    		case '+':
    		puts("Calculating...");
    		addition(n,n2);
    		break;
    		case '-':
    		puts("Calculating...");
    		subtraction(n,n2);
    		break;
    		case '/':
    		puts("Calculating...");
    		division(n,n2);
    		break;
    		case '*':
    		puts("Calculating...");
    		multiplication(n,n2);
    		break;
    		case '^':
    		puts("Calculating...");
    		power(n,n2);
    		break;
    		default:
    		puts("Please enter one of the operations listed ONLY");
    		break;
    	}
    }
    How does that look? I still get the skipping though.
    Last edited by Padawan; 04-04-2004 at 06:40 PM.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    do this as a test:
    before the first getline() or getchar() add:
    Code:
    do
    {
      temp = getchar();
      printf("Got character %c value of %d\n", temp, temp);
    }while ( temp != '\n' && temp != EOF);
    This will tell you if there is anything in the input buffer prior to this function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Formatting HD Skipping Master Boot Record.?
    By jawwadalam in forum Tech Board
    Replies: 16
    Last Post: 05-12-2005, 07:33 AM
  3. MSVC Skipping Compile stage
    By nickname_changed in forum Windows Programming
    Replies: 1
    Last Post: 07-22-2003, 05:14 AM
  4. Starting out by skipping chapters !
    By Jim(U) in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-23-2002, 06:16 AM
  5. Skipping scanf()
    By Ican'tC in forum C Programming
    Replies: 4
    Last Post: 09-12-2002, 02:49 AM