Search code examples
cscanfterminate

Program is terminating without recognizing scanf


I don't know why my program terminates before confirmStats(); is called. I included everything related in main() in case the issue is ocurring somewhere else in my program.

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

int str, intel, charis;
int totalPts =5;

/* Returns a number.
   User must input an integer.
*/
int getNumber(int number){
  while(scanf("%d", &number) != 1){
    printf("You did not enter a valid number\n");
    scanf("%*s");
  }
  return number;
}
/* Individual stat points */
int stat(int number){
  number = getNumber(number);
  while(number > totalPts){
    printf("You only have %d stat points left\n", totalPts);
    printf("Enter a number less than or equal to %d:\t", totalPts);
    number = getNumber(number);
  }
  totalPts -= number;
  printf("Points remaining:\t%d\n", totalPts);
  return number;
}


/* Player stat points */
void getStats(){
  printf("You're alotted %d stat points to spend in Strength, Intellect, and Charisma\n", totalPts);

  printf("Intellect:\t");
  intel = stat(intel);

  printf("Strength:\t");
  str = stat(str);

  printf("Charisma:\t");
  charis = stat(charis);

  printf("\nIntellect: %d\t Strength: %d\t Charisma: %d\n", intel, str, charis);

}

void confirmStats(){
  char ans;
  scanf("%c", &ans);
  while(ans  == 'n'){
    str = 0;
    intel = 0;
    charis = 0;
    getStats();
    printf("Are these correct?:\ty/n: ");
    scanf("%c", &ans);
  }
}
void main(){

  printf("\nSafe choice...");
  printf("\n");
  printf("Alright, how old are you?\n");
//  int age, str, intel, charis;
  int age;
//  int totalPts = 5;
  age = getNumber(age);
  getStats();
  printf("Are these correct? ");
  printf("\n");
  printf("y/n:\t");
  printf("\n");
  confirmStats();




}

Solution

  • The problem is that scanf("%c", &ans); scans the newline character left over by the previous scanf.

    The fix is easy. Simply add a space before %c in the two scanfs in confirmStats. The space is a whitespace character and a whitespace character in the format string of scanf tells scanf to scan any number of whitespace characters, if any, until the first non-whitespace character.

    Improved code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int str, intel, charis;
    int totalPts = 5;
    
    /*
       Returns a number.
       User must input an integer.
    */
    int getNumber(){
      int number;
      while(scanf("%d", &number) != 1){
        printf("You did not enter a valid number\n");
        scanf("%*s");
      }
      return number;
    }
    
    /* Individual stat points */
    int stat(){
      int number;
    
      number = getNumber();
    
      while(number > totalPts){
        printf("You only have %d stat points left\n", totalPts);
        printf("Enter a number less than or equal to %d:\t", totalPts);
        number = getNumber();
      }
    
      totalPts -= number;
      printf("Points remaining:\t%d\n", totalPts);
    
      return number;
    }
    
    
    /* Player stat points */
    void getStats(){
      printf("You're alotted %d stat points to spend in Strength, Intellect, and Charisma\n", totalPts);
    
      printf("Intellect:\t");
      intel = stat();
    
      printf("Strength:\t");
      str = stat();
    
      printf("Charisma:\t");
      charis = stat();
    
      printf("\nIntellect: %d\t Strength: %d\t Charisma: %d\n", intel, str, charis);
    
    }
    
    void confirmStats(){
      char ans;
      scanf(" %c", &ans);
      while(ans  == 'n'){
        str = 0;
        intel = 0;
        charis = 0;
        getStats();
        printf("Are these correct?:\ty/n: ");
        scanf(" %c", &ans);
      }
    }
    int main(void){
      printf("\nSafe choice...");
      printf("\n");
      printf("Alright, how old are you?\n");
      age = getNumber();
    
      getStats();
    
      printf("Are these correct? ");
      printf("\n");
      printf("y/n:\t");
      printf("\n");
      confirmStats();
    
      return 0;   
    }