0

String initialization:

main(){
char monthName[20];
int days;

printf("Enter the month: ");
fgets(monthName, 20, stdin);

days = monthCalc(monthName);
}

Function:

int monthCalc(char monthName[20]) {
int days = 0;

if (strcmp(monthName, "January") == 0)
    days = 31;
else if (strcmp(monthName, "February") == 0)
    days = 28;
else if (strcmp(monthName, "March") == 0)
    days = 31;
else if (strcmp(monthName, "April") == 0)
    days = 30;

For Example, if I enter January, the char array does collect the string "January". But when compared using strcmp in the monthCalc function, days will not get 31 like it should. Thanks for any help!!!

Lee
  • 142,018
  • 20
  • 234
  • 287
hax
  • 81
  • 1
  • 7
  • 2
    Is this in C? There isn't a language tagged to this question. – Spencer Wieczorek Apr 29 '17 at 19:10
  • 2
    Remove the trailing `newline` because `fgets` retains it. – Weather Vane Apr 29 '17 at 19:14
  • Is this all of your code? Because clearly you don't return anything from your `monthyCalc` function nor is there a closing brace. – Spencer Wieczorek Apr 29 '17 at 19:17
  • @WeatherVane That's not a duplicate of this question, that's isn't the issue the OP has. – Spencer Wieczorek Apr 29 '17 at 19:19
  • @SpencerWieczorek it is not the same question, i.e. duplicate, but the answers duplicate what could be answered here. What is the issue if it is not this? Not an incomplete function - enough was given to illustrate the problem. – Weather Vane Apr 29 '17 at 19:20
  • @SpencerWieczorek please give a good reason why this question should be re-opened. – Weather Vane Apr 29 '17 at 19:26
  • @WeatherVane *"enough was given to illustrate the problem"*, your assuming correct code. The OP might not even be returning `days` from the function, the question is off-topic for that reason until the OP provides working runnable code. – Spencer Wieczorek Apr 29 '17 at 19:33
  • Oh for heaven's sake. It is quite clear what the problem is. If using `strcspn` does not solve it OP can ask again. For example "why does my function not return a value, I do not understand the compiler warning". – Weather Vane Apr 29 '17 at 19:34
  • @JohnBillingham you say the input was correct, but if you print it like this `printf("<%s>", monthName);` that should expose the `newline` in the input string. – Weather Vane Apr 29 '17 at 19:47

0 Answers0