-3

The code is glitching out when i'm trying to do a simple character exchange. The code is listed down below along with screen shots of the error.

#include <stdio.h>

int main()
{
    char Ghost[12] = "SimonGhost";
    int ghostdeathdate = 5;
    char Price;
    int Soap;
    while (ghostdeathdate > 0)
    {
        scanf("%c %d", &Price, &Soap);
        Ghost[Soap] = Price;
        printf("%s\n", Ghost);
        --ghostdeathdate;
    }
    return ghostdeathdate;
}

anastaciu
  • 23,467
  • 7
  • 28
  • 53

1 Answers1

1

The problem is the new line left in the buffer in each call to scanf, you need to consume each new line with a space:

scanf("%c %d", &Price, &Soap);

should be

scanf(" %c %d", &Price, &Soap); // Notice a space before %c
David Ranieri
  • 39,972
  • 7
  • 52
  • 94