-2

I am new here and need help with this code of mine. I have to calculate the average mean temperature for the month of March and July. The arrays are in a separate folder. Here is my code. Thanks

float sumofArray = 0;
int j = 0;
char Month;

float AverageMeanMarch = 0;
float AverageMeanJuly = 0;

printf("Please type the month for average mean temperature. (March/July) \n");
scanf_s("%c", &Month);

if (Month == "March" && Month == "march") 
{
    for (j = 0; j < 31; j++)
        sumofArray = sumofArray + MeanMarch[j];
    AverageMeanMarch = sumofArray / 31;


    printf("The average mean temperature for the month of March is %.2f. \n", AverageMeanMarch);
} 

else if (Month == "July" && Month == "july")
{
    for (j = 0; j < 31; j++)
        sumofArray = sumofArray + MeanJuly[j];
    AverageMeanJuly = sumofArray / 31;

    printf("The average mean temperature for the month of March is %.2f. \n", AverageMeanJuly);
}

else
{
    printf("Invalid Month \n");
}
Dom
  • 1,687
  • 6
  • 27
  • 37
ZackLee
  • 1
  • 2
  • There is no way the code compiles as posted. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – dxiv Dec 19 '15 at 07:12
  • 1
    1) `char Month;` --> `char Month[16/* your need size*/];` 2) `scanf_s("%c", &Month);` --> `scanf_s("%s", Month, 16);` – BLUEPIXY Dec 19 '15 at 07:14
  • 1
    It's not C++. Why am I here? – Ivan Aksamentov - Drop Dec 19 '15 at 07:14
  • 1
    @Drop comparing a string to a char array literal would be C++... – Mr Lister Dec 19 '15 at 07:17
  • @Drop Once the obvious issues are fixed, the code could conceivably compile as either C or C++. – dxiv Dec 19 '15 at 07:18
  • 1
    @divx Yeah, C and C++ just happen to share same linker. Many of us don't believe in backward compatibility. C++ is no more C than Java Native Interface or P/Invoke are. Why there are no questions manifesting C/C# or C/Java (with disgusting slash syntax)? – Ivan Aksamentov - Drop Dec 19 '15 at 07:26
  • 3
    @ZackLee There are so many defects in your code that it's obvious that you need to learn C from the very beginning. There is a terrific topic that will help you: [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Dec 19 '15 at 07:29
  • @Drop The posted code is amenable to both C and C++ once the obvious issues are fixed. Not sure where the linker comes into the picture. Also not sure what your point is about C# or Java, since those have fundamental syntax and semantics differences. – dxiv Dec 19 '15 at 08:13
  • Alternatively, if you want to program in C++ rather than C, [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Dec 19 '15 at 10:48

2 Answers2

2

You're using a wrong conditional operator, instead of &&, you want to use || - Since a string can't be both "String" and "string" in a case-sensitive ecosystem.

And also, you can't do string comparisons with the '==' operator in c, by employing that you only do a pointer comparison, what you want is "strcmp(str1, str2)", and the output for that is 0 if strings match.

So, your code should be:

if (strcmp(Month,"March") == 0 || strcmp(Month,"march") == 0) 

and do the same changes for July.

And, as someone mentioned in the comments, you declared 'Month' as a single character, you either want to initialize it to a char[] big enough for holding a month, or char *, and allocate memory accordingly with malloc.

Further more, you used scanf with %c as parameter, meaning that only one character will be retrieved from the user input.

For additional info, see: strcmp

NadavL
  • 390
  • 2
  • 12
0

You are not taking month as an array .. You Are taking month as a char variable that will store single value...