0

The program works i allocate and reallocate data properly.

I wanted to break if c== -1, so i tried to compare c to "-" and data[i-1] == '1' but i got an error. So i tried to just print it to see what comes up.

   for (i=0;;i++) {
        c=getchar(); /* put input character into c */
        if (c== '1'  ) {               // need to find a way to change it to -1
            printf("n-1 is %c",data[i]); // ask
            break;
            //&& (data[i-1] == '-')
        }

The output is

n-1 is ?

*The ? is upside down

Any ideas. Thank you.

Edit:

char *getInput()
{

    char *data,*temp;
    data= malloc(sizeof(char));
    char c; /* c is the current character */
    int i; /* i is the counter */
    printf ("\n Enter chars and to finish push new line:\n");
    for (i=0;;i++) {
        c=getchar(); /* put input character into c */
        if (c== '1'  ) {               // need to find a way to change it to -1
            printf("n-1 is %c",data[i]); // ask
            break;
            //&& (data[i-1] == '-')
        }
        data[i]=c; /* put the character into the data array */
        temp= realloc(data,(i+1)*sizeof(char)); /* give the pointer some memory */
        if ( temp != NULL ) {
            data=temp;
        } else {
            free(data);
            printf("Error allocating memory!\n");
            return 0 ;
        }
    }
6
  • 1
    You don't assign data[i] in the code shown. What were you expecting to have printed out? Commented Jan 4, 2014 at 13:04
  • How is memory for data allocated? dynamically or statically? Can you show where you take inputs to data ? Commented Jan 4, 2014 at 13:06
  • Does not reproduce here. Commented Jan 4, 2014 at 13:11
  • Edited. The whole method is shown Commented Jan 4, 2014 at 13:12
  • what are you trying to achieve? Try detecting the '-' char and then raise a flag to detect a trailing 1 and halt, but this is poor design from my experience. Commented Jan 4, 2014 at 13:19

3 Answers 3

1

If I understand it correctly, you want to test the specific input "-1" in your input loop. Then you need to test two characters, not only 1. To prevent data underrun, first test if i > 0 -- if you press 1 right away you are still in position #0 and so you are testing some random memory contents before your actual string:

if (i > 0 && c== '1' && data[i-1] == '-')
{
     data[i-1] = 0; /* Terminate string at correct position */
     break;
}

Don't forget to adjust your prompt.

Meta-question: why use '-1' to terminate input, as Enter is a much more logical choice?

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

Comments

0

You haven't initiated data[] previously. And during execution if the first input user gives is '1' then it need to output some garbage. And that's the garbage, an inverted '?' .

Comments

0

You have not assigned any char to data[i] when executing printf. Use following code

     if (c== '1' && i>0 ) { 
          printf("n-1 is %c",data[i-1]); 
          break;
     }       

instead of

      if (c== '1'  ) {               // need to find a way to change it to -1
        printf("n-1 is %c",data[i]); // ask
        break;
        //&& (data[i-1] == '-')
      }

Comments

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.