-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
  • 22,293
  • 7
  • 26
  • 44

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
  • 37,819
  • 6
  • 48
  • 88