This is my problem!

This is a discussion on This is my problem! within the C Programming forums, part of the General Programming Boards category; i use scanf("%d",var) to get integer from the user OK? the problem is if I use scanf("%d",var); another time the ...

  1. #1
    c--
    c-- is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    43

    Angry This is my problem!

    i use scanf("%d",var) to get integer from the user OK? the problem is if I use scanf("%d",var); another time the result (the value of (var) ) will not be the lastest input but all the inputs!!!
    I tried to reset the (var) like this {.... var=0;...} but the problem continued. how can I solve it!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    Post some more of your code and what the desired output is as well as the actual output. What you have now isn't enough to know what's wrong.
    C code. C code run. Run code, run...please!

  3. #3
    c--
    c-- is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    43
    this is the code that have problem.

    int nbeam()
    {
    int nfield=0,counter;
    char sure;
    textcolor(5);
    for (counter=1; counter<161;counter++)
    printf("");


    gotoxy(35,3);
    textcolor(1);
    cprintf("New Beam Menu");

    textcolor(3);
    printf("\n");
    for (counter=1; counter<161;counter++)
    cprintf("");
    textcolor(9);
    printf("Enter number of field: \n");

    scanf("%d",&nfield);/*I think HERE is THE PROBLEM*/
    while(1)
    {
    printf("Are you sure %d fields \n",nfield);
    sure=getch();
    sure=tolower(sure);
    switch (sure)
    {
    case 'y': break;
    case 'n': {clrscr();gotoxy(0,0); nbeam();break;}
    default : {printf("Y OR N Please!\n"); continue;}
    }/*end of switch*/
    break;
    } /*end of while*/
    printf("%d",nfield);/* <----------- it print's all the inputs that the user try*/

    return (0);
    }/*end of function(nbeam)*/

    The Output I want is : the lastest number that user hit. but I get all the numbers that the user hit by wrong.
    Last edited by c--; 12-23-2001 at 09:52 AM.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    The problem is with the scanf, when you use it with numbers the '\n' is left in the keyboards buffer, so the next time you call it, it won't read anything.
    Use fgets followed by sscanf to avoid this problem
    Code:
    char temp[5];
    int var;
    
    printf("\nEnter a number ");
    fgets(temp, sizeof(temp)-1, stdin);
    sscanf(temp, "%d", &var);
    It may seem like extra hassle, but it's better than using scanf
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    if your nbeam function is called 10 times, then you have 10 nfields, each will be printed. Do you have to use recursion?

    It could all be done in 1 loop.

    Code:
    while(1)
    {
        printf("Enter number of field: \n"); 
        fgets(temp, sizeof(temp)-1, stdin);
        sscanf(temp, "%d", &nfield);
    
        printf("Are you sure %d fields \n",nfield); 
        sure=getch(); 
        sure=tolower(sure); 
    
        if(sure == 'n')
            continue;     /* start loop again */
        else
            break;         /* leave loop */
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    c--
    c-- is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    43

    Smile

    thank u C_coder...

    But if the user put by wrong 'b' instead Y or N.. it will be as he hits Y ???

    sorry but I'm new In C..

  7. #7
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    But if the user put by wrong 'b' instead Y or N.. it will be as he hits Y ???
    I'm sure you could figure that out???

    Code:
    while(1)
    {
        printf("Enter number of field: \n"); 
        fgets(temp, sizeof(temp)-1, stdin);
        sscanf(temp, "%d", &nfield);
    
        printf("Are you sure %d fields \n",nfield); 
        
        while((sure = tolower(getch())) != 'y' && sure != 'n');
    
        if(sure == 'n')
             continue;     /* start loop again */
         else
             break;         /* leave loop */
    }
    The inner while loop won't stop unless a y or n is input, it does everthing on one line as well, which is cool.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  8. #8
    c--
    c-- is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    43

    Talking

    THANK U VERY MUCH I JUST NOW KNEW THAT I'M stupid.


    THANK U FOR THIS - B I G L E S S O N

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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