Using char arrays in branching statements

This is a discussion on Using char arrays in branching statements within the C Programming forums, part of the General Programming Boards category; Code: int main() { printf(" Calculation Engine\n Ready...\n\n"); char buf[BUFSIZ]; char *p; while (1) { As given, implemented, and all ...

  1. #16
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Code:
    int main()
    {
    
        printf("  Calculation Engine\n  Ready...\n\n");
    char buf[BUFSIZ];
    char *p;
    
    
         while (1) {
    As given, implemented, and all if branches as contained after the squiggly bracket shown above.

    Code:
     if ( directory == 0 )
             {
                 printf("\n\nOperation Table -\n\n - 1: Addition etc");
             }
    The above if branch works perfectly, the loop will continue infinitely, providing the user inputs 'directory', and then presses 'y' to repeat.

    Code:
    else if ( variable == 0 )
      {
            float iadd1, iadd2;
            printf("\nOperation: Addition\n\n  >: First Number: ");
            scanf("%f", &iadd1);
            printf("  >: Second Number: ");
            scanf("%f", &iadd2);
            addition( iadd1, iadd2 );
    
    
        }
    This however, does not. Removing everything below the 'first number' printf() line allows it to work, as with all other branches. The scanf() is messing it up, but I'm not sure how...

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Remember, scanf leaves the enter-key behind; so if you use %f, then %c, that %c will pick up just the enter-key (and not y, or n, or whatever).

    Try:
    Code:
    scanf(" %c", &r);
    which will skip all enter-keys at the beginning of input.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hmanners View Post
    EDIT: I've found that by removing any user input from any of the 'if' branches, the loop works. As soon as there is input, the loop doesn't work, and simply repeats asking whether repeated operations are required...
    There are other little things (like that scanf, newline issue) that do not occur to most or any people when learning. Hopefully, this is just an observation you are making ("I've found that by removing any user input from any of the 'if' branches, the loop works.") and not a mistaken attempt to present a symptom as a cause. Anyway, sometimes the people who wrote the documentation you are working with will remember these issues and present them explicitly; other times, you will have to, as itCbitC says, "post what you have implemented" and very likely someone will recognize it right away.

    When all else fails, isolate everything you are doing into as small an element as you can. Find some "mirror" to hold up to confirm that what you think is going on is really going on (eg, print the variables out, check things conditionally, experiment with different possibilities to see if your predictions are consistent with reality).
    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

  4. #19
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Remember, scanf leaves the enter-key behind; so if you use %f, then %c, that %c will pick up just the enter-key (and not y, or n, or whatever).

    Try:
    Code:
    scanf(" %c", &r);
    which will skip all enter-keys at the beginning of input.

    Ah, I love you man. Program works perfectly now.


    Thanks very much for all of your help, you've been very patient and polite with my idiocity. I'll definately keep coding, although it's back to tutorials for a while before I'll bother you again...heh.

    Thanks again.

    - Harry Manners

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

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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