Thread: Why program does not enter else-cascade?

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    Why program does not enter else-cascade?

    Hi!

    When the user-input is 0, it should imo enter the else-cascade and print out " 1 1" ?
    But this does not happen - can anyone see why not?

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main() {
       int zahl, original, stellen = 0, s, umgekehrt = 0;
       printf("Bitte Zahl eingeben: ");
       
       scanf("%d", &zahl);
       original = zahl;
    
    
       if (original != 0) {
            for (stellen = 0; zahl > 0; stellen++) {
            s = zahl % 10;
            printf("%d\n", s);
    
    
            umgekehrt += s;
            umgekehrt *= 10;
    
    
            zahl /= 10;
        }
        umgekehrt /= 10;
    
    
       printf("Die Zahl %d hat %d Stellen.\n", original, stellen);
       printf("Die umgekehrte Zahl ist %d\n", umgekehrt);
    } else {
        stellen = stellen + 1;
        printf("%d\t%d", stellen, stellen);
    }
       
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check the return value of scanf to ensure that it succeeded before continuing.

    Use a debugger to check what's happening, as in where does control flow and what are the values of the variables. If you cannot use a debugger, insert debug print statements.

    Indent your code properly: your indentation is inconsistent.

    Declare your variables near first use, thereby limiting their scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you sure it doesn't? Maybe the console window closes before you see it.
    Devoted my life to programming...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Better indentation would help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int zahl, original, stellen = 0, s, umgekehrt = 0;
      printf("Bitte Zahl eingeben: ");
    
      scanf("%d", &zahl);
      original = zahl;
    
      if (original != 0) {
        for (stellen = 0; zahl > 0; stellen++) {
          s = zahl % 10;
          printf("%d\n", s);
          umgekehrt += s;
          umgekehrt *= 10;
          zahl /= 10;
        }
        umgekehrt /= 10;
        printf("Die Zahl %d hat %d Stellen.\n", original, stellen);
        printf("Die umgekehrte Zahl ist %d\n", umgekehrt);
      } else {
        stellen = stellen + 1;
        printf("%d\t%d", stellen, stellen);
      }
      return 0;
    }
    Also, your printf in your else lacks a \n.
    That may prevent you seeing the output until the moment the process exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Hey, oh indeed now it works.
    Thanks anyways for all the input and hints.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program : enter a password
    By slyer4ever in forum C Programming
    Replies: 16
    Last Post: 10-08-2013, 02:11 PM
  2. Cascade of Fir filters using c
    By coolboy03 in forum C Programming
    Replies: 1
    Last Post: 02-21-2013, 11:03 AM
  3. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  4. program doesnt enter function
    By avisik in forum C Programming
    Replies: 5
    Last Post: 12-17-2008, 11:06 PM
  5. can't get my program to let me enter anything into it
    By allthekandi in forum C++ Programming
    Replies: 9
    Last Post: 02-12-2003, 12:15 PM

Tags for this Thread