0

Hi I need some help with debugging my program: It should read from the Console, process the input and give it back out:

The error occures after while(scanf("%15s", input) != EOF) is called the 2nd time. Unfortunately I can't tell you what the error is, because the progam freezes and doesn't give me any Information. I think there is something wrong with the input var (it is passed multiple times)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

char* repeat(char c, int n);
char* drawLabel(char* label, int n);
char* drawBarnorm(char* label, int value);
char* drawBar(char* label, double value);

int main(void)
{
    char* input;
    double numIn;
    char buf[] = "";
    char* pOutput = &buf[0];

    while(scanf("%15s", input) != EOF)
    {
        scanf("%lf", &numIn);
        if (numIn > 1)
        {
            if (numIn > 30)
            {
                printf("num to big!\n");
                return 0;
            }

            strcat(pOutput, drawBarnorm(input, (int)numIn));
        } else
            {strcat(pOutput, drawBar(input, numIn));}


        printf("%s\n", pOutput);
    }

    printf("%s\n", pOutput);
    return 0;
}

char* repeat(char c, int n)
{
    char* out = (char*)malloc(sizeof(char)*50);
    int i, len;
    out[0] = '\0';

    for (i = 0; i < n; ++i)
    {
        len = strlen(out);
        out[len] = c;
        out[len+1] = '\0';
    }

    return out;
}

char* drawLabel(char* label, int n)
{
    if (strlen(label) > n)
    {
        char* newLabel = (char*)malloc(sizeof(char)*(n+1));
        newLabel[0] = '\0';
        strncpy(newLabel, label, n);
        newLabel[n] = '\0';
        return newLabel;
    } else if (strlen(label) < n)
    {
        strcat(label, repeat(' ', n-strlen(label)));
    }

    return label;
}

char* drawBarnorm(char* label, int value)
{
    char* bar = (char*)malloc(sizeof(char)*41);
    char* barPart;
    bar[0] = '\0';
    bar = drawLabel(label, 8);
    strcat(bar, "|");
    barPart = drawLabel(repeat('#', value), 30);
    strcat(bar, barPart);
    strcat(bar, "|");
    return bar;
}

char* drawBar(char* label, double value)
{
    int val = (int)(30.0*value);
    return drawBarnorm(label, val);
}

Thank you for helping me out with this.

8
  • 2
    You made the exact same mistake in this question as previous. Commented Jan 8, 2015 at 17:32
  • 1
    Those who cannot remember the past are condemned to repeat it. -- George Santayana. Commented Jan 8, 2015 at 17:38
  • this line: 'while(scanf("%15s", input) != EOF)' contains an error. What the code really wants to look for is 'was the input/conversion successful?' therefore, the correct way to write the line would be: 'while( 1 == scanf(" %15s", input))' I.E. 1 input conversion and leading ' ' in format string to skip over/absorb any leading white space, (like a newline). Commented Jan 8, 2015 at 18:01
  • the function: drawlabel() returns a pointer to a malloc'd area, so the line: 'bar = drawLabel(label, 8);' overlays the prior contents of 'bar', resulting in a memory leak because 'bar' was already set by a pointer to some malloc'd memory area Commented Jan 8, 2015 at 18:04
  • 3
    I rolled back your question as your edit make the answers invalid (you corrected what was wrong). When you get the answer that solves your problem you should mark it as accepted, don't edit your question. Commented Jan 8, 2015 at 18:16

2 Answers 2

4
char* input = malloc(size); /* Allocate memory of your wish */

Allocate memory to input You have not initialized your pointer.

The pointer should be pointing to some valid memory location to store the value through scanf()

Sign up to request clarification or add additional context in comments.

3 Comments

You know there is a free function right? And what should the value of size be?
in the main() function, 'input' is defined as 'char input[40]' so the scanf is ok. (other than needed to check the returned value to assure the input/conversion was successful
@user3629249 Why do I see char* input; in main()?
3

You have to initialize input or declare it as an array like this

char input[16];

also, you should notice that scanf does not return EOF it returns the number of arguments matched, so you have to change

while(scanf("%15s", input) != EOF)

to

while(scanf("%15s", input) == 1)

because while(scanf("%15s", input) != EOF is always true.

3 Comments

Thx, that helped a lot. after I exit with an EOF I get a: STATUS_ACCESS_VIOLATION.
Aha thats funny because I can exit the loop with EOF.
No you can't. The character sent is not what scanf returns, in that case it returns 0, but since 0 != EOF you enter the loop with input uninitialized, then when you trying to access input it's UNDEFINED BEHAVIOR which in your case is causing STATUS_ACCESS_VIOLATION apparently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.