Thread: Error with my CodeBlocks compiler

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    18

    Error with my CodeBlocks compiler

    Hello, I have been working on a problem in C (im teaching mysekf out of Kochans programming in C) and am running into a problem where my program does not print the output i desired.

    When it first started i figured id done something wrong programming wise, only ti discover i simply needed to restart the IDE. It is now doing the same thing (worked before, nothing really changed, not working now)

    Of course, there is always the chance i screwed up the code, though ive reviewed it a few times and cant find an issue.

    Again, the issue is that the output either does not print, or does not show in the output. if the code below works for you, then its probably my ide and i need help solving it. Thank you!

    Code:
    #include <stdio.h>
    
    void intToStr(int input)
    {
        int x, negative = 0;
    
        char string[81];
    
            if (input < 0)
            {
            string[0] = '-';
            negative = 1;
            input=-input;
            }
    
        for (x = negative; string[x] != '\0'; x++)
        {
            string[x] = input%10;
                if (input < 1)
                {
                string[x] = input;
                string[x+1] = '\0';
                }
            input = input/10;
            printf ("%i", input);
        }
    }
    
    int main (void)
    {
        int input;
        void intToStr(int input);
    
    printf ("Please enter your number\n");
    scanf ("%i", &input);
    
        intToStr(input);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't see anything wrong with your program. It's not printing anything because you're not telling it to print anything, the printf() in you function is probably never reached.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    I don't see anything wrong with your program. It's not printing anything because you're not telling it to print anything, the printf() in you function is probably never reached.

    Jim
    I must be missing something then.

    -If i input 2345 into the scanf command

    -its inputted to the function.

    -negative = 0 because the number is positive.

    -string[x] = 5 (remainder of 2345 divided by ten)

    -input divides by ten

    -the printf command should show "234"

    Thanks for looking at this.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I must be missing something then.
    Yes you are.

    The program probably isn't printing anything because that for loop is never being executed, you need to carefully check the for() condition section. When do you think that loop will ever execute?

    What does the string string contain?

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    Yes you are.

    The program probably isn't printing anything because that for loop is never being executed, you need to carefully check the for() condition section. When do you think that loop will ever execute?

    What does the string string contain?

    Jim
    the condition is that string[x] != '\0'.

    it should execute while there is no null character value in the string. when it hits that null character (which I add with the if statement below it) it should terminate the loop.

    though string is uninitialized, so i suppose its possible that its producing an undefined value that equals the (null) character. It was functioning fine before though, but i know that often doesn't mean it was correct.

    EDIT - the function is suppose to convert and integer to a string.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    it should execute while there is no null character value in the string.
    Yes and how do you know that the character in string[0] is not a '\0' prior to the start of the loop?

    (which I add with the if statement below it) it should terminate the loop.
    If the loop is never entered what is inside the body of the loop makes no difference.

    though string is uninitialized, so i suppose its possible that its producing an undefined value that equals the (null) character.
    Using an uninitialized value produces undefined behavior, so anything can happen. If string[0] happens to be '\0' the loop will not execute, which is what I'm seeing with my compiler.

    You need to always initialize all variables before you try to use them in a "calculation" or "comparison".


    Jim

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The functionname 'intToStr' and the variable 'string' suggests that this function should build a string from an integer.
    The first if-statement (handle the negative integer) is correct and you realy work with characters ( string[0] = '-'; ).

    But this line set the value from 0 to 9.
    Code:
    string[x] = input%10;
    All characters lower then 32 are control-characters.
    To set the right character (number-symbol) you should add '0'.
    Code:
    string[x] = input % 10 + '0';
    As the other mentioned befor, the contition of the if-statement is not realy good.
    But you know the right contition. You use it inside the for-body.
    Code:
    if (input < 1)
    {
    …
    Why you don't use:
    Code:
    for (x = negative ; input > 0 ; x++)
    {
    …
    or in short:
    Code:
    for (x = negative ; input ; x++)
    {
    …
    Remember: all values other then zero will evaluate to 'true', only zero will evaluate to 'false'.

    With this solution you don't need the if-statement inside the for-body.
    The for-loop will end if the input is zero.
    You can terminate the string after the loop with:
    Code:
    string[x] = '\0';
    You can check the result of the function with a last line inside the function.
    Code:
    printf("Result: '%s'\n", string);
    You will notice that the resulted string is in reverse order. This is the next problem for you to solve.
    Other have classes, we are class

  8. #8
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Ah thank you both. both answers helpful in different ways

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL2 Codeblocks error
    By russiancircles in forum Game Programming
    Replies: 2
    Last Post: 05-08-2014, 06:08 AM
  2. no error of compilation but my code makes codeblocks bug
    By funnydarkvador in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2013, 12:31 AM
  3. error with scanf statement in codeblocks
    By narendrav in forum C Programming
    Replies: 1
    Last Post: 03-19-2011, 11:25 AM
  4. codeblocks compilation error, C Tutorial
    By boarder428 in forum C Programming
    Replies: 8
    Last Post: 09-26-2010, 03:00 PM
  5. gcc vs CodeBlocks compiler
    By CDdotdot47 in forum C Programming
    Replies: 8
    Last Post: 08-13-2010, 12:43 PM

Tags for this Thread