-2

I am having trouble getting this program to print the strings I enter properly. It keeps telling me that I have not entered data, even when I have. I also can't get the strings to compare to run my if statement. Thank for any help.

#include <stdio.h>

//function prototype
void enterPerson();
void enterChoice();

//global variables
char person[30];
char choice;

int main(void) {
    enterPerson();
    enterChoice();


    printf("Please try the Precipitation Program again.\n");

    return 0;
}

void enterPerson(){
    // Ask for person name
    printf("Please enter name:\n");
    scanf("%s", &person);
    //-------------------------------------------
    printf("person is %s\n", person);
    //-------------------------------------------
}

void enterChoice(){
    //initialize choice
    choice = "M";
    //ask what they choose
    printf("Do you choose test or rate? (Enter T for test R for rate)\n");
    scanf("%c", &choice);
    printf("Xchoice is: %c\n", choice);

    if ((choice == 'T')||(choice == 'R')){
        printf("choice is: %c\n", choice);
    }
    else{
        printf("Incorrect or no data was input at this time\n");
    }
}
John Smith
  • 496
  • 1
  • 6
  • 20
Paul
  • 3
  • 3

2 Answers2

3

As mentioned in comments, there are at least 3 problems:

  1. scanf("%s", person); - do not take the address of char array.
  2. scanf(" %c", &choice); - insert space to ignore whitespace.
  3. choice = 'M'; - "M" is a string literal, while choice is char.
Schtolc
  • 1,036
  • 1
  • 12
  • 19
0

There is a linefeed (0xa) character left in the input buffer. You can see it by printing the choice variable after your scanf line with:

 scanf("%c", &choice);
 printf("c: %x\n", choice);

There are several options to get rid of this. Easiest is explained here.

Also there is a problem in:

 scanf("%s", &person);

Character array name in C points to the first character, so you should fix this with:

 scanf("%s", person);   
Community
  • 1
  • 1
pbn
  • 2,406
  • 2
  • 26
  • 39