You are here:

C/update to previous question

Advertisement


Question
QUESTION: Hi,
I am not sure what is wrong with my program. My assignment is to write a program that will return the next day given a certain starting day.It is my first time using "enum"... maybe I am using it incorrectly. From what I read from the textbook, I seem to be using it correctly, but I keep on getting the same error messages:

test.c:3: error: syntax error before numeric constant
test.c: In function `main':
test.c:18: error: `next_day' undeclared (first use in this function)
test.c:18: error: (Each undeclared identifier is reported only once
test.c:18: error: for each function it appears in.)
test.c: In function `find_next_day':
test.c:25: error: parameter name omitted
test.c:29: error: syntax error before "day"
test.c:32: error: break statement not within loop or switch
test.c:33: error: case label not within a switch statement
test.c:35: error: break statement not within loop or switch
test.c:36: error: case label not within a switch statement
test.c:38: error: break statement not within loop or switch
test.c:39: error: case label not within a switch statement
test.c:41: error: break statement not within loop or switch
test.c:42: error: case label not within a switch statement
test.c:44: error: break statement not within loop or switch
test.c:45: error: case label not within a switch statement
test.c:47: error: break statement not within loop or switch
test.c:48: error: case label not within a switch statement
test.c:50: error: break statement not within loop or switch
test.c: At top level:
test.c:53: error: syntax error before "return"

My code is:
#include <stdio.h>

enum day {sun=1, mon, tue, wed, thur, fri, sat};

typedef enum day day;

day find_next_day (day);

int main(void)
{
  day x, next_day;
  
  printf("\nEnter a day. ");
  scanf("%d", &x);
  
  find_next_day (x);
  
  printf ("%d", next_day);
  
  return 0;
}



day find_next_day (day)
{   
  day next_day;
  
  switch (day) {
  case sun:
     next_day = mon;
     break;
  case mon:
     next_day = tue;
     break;
  case tue:
     next_day = sun;
     break;
  case wed:
     next_day = thur;
     break;
  case thur:
     next_day = fri;
     break;
  case fri:
     next_day = sat;
     break;
  case sat:
     next_day = sun;
     break;
  }
  
  return next_day;
  
}

Thanks!

ANSWER: Hello Lingjun

It is a good effort. There are just a couple of problems. I've corrected the syntax and left you with comments in the code for you to read.

Best regards
Zlatko

#include <stdio.h>


enum day {sun=1, mon, tue, wed, thur, fri, sat};

/*
Lets change the typedef from day, to Day, with a capital D.
Types are usually specified with a capital letter.
Plus you are using the word day as a variable name in the
switch of find_next_day
*/
typedef enum day Day;

/*
Below I changed all places where day was being used as a type
to be Day with a capital D.
*/
Day find_next_day (Day);

int main(void)
{
   Day x, next_day;

   /* Here you are expecting the user to enter a number
   Is that correct?
   Only a number can be entered for scanf %d
   */
   printf("\nEnter a day. ");
   scanf("%d", &x);

   /* Here you should check if the user entered a valid day
   before calling find_next_day.

   What will the function find_next_day return if it gets an invalid
   day ?
   */


   /* Here you must accept the return value from the function
   The next_day is not automatically set by the function
   You must write
   next_day = ...
   */
   next_day = find_next_day (x);

   printf ("%d", next_day);

   return 0;
}


/*
When accepting a parameter, a function
needs to specify the type and a parameter name.
Here Day is the type and day is the parameter name
*/
Day find_next_day (Day day)
{
   /* If the day passed in does not match one of the cases,
   the function will return an undefined value.
   Undefined means it could be anything.
   It is undefined because next_day is not initialized to anything.
   Perhaps you should initialize next_day to -1 and let that indicate
   an error.
   Even better would be to check for a valid day before calling this
   function.
   */
   Day next_day;

   switch (day) {
      case sun:
         next_day = mon;
         break;
      case mon:
         next_day = tue;
         break;
      case tue:
         next_day = sun;
         break;
      case wed:
         next_day = thur;
         break;
      case thur:
         next_day = fri;
         break;
      case fri:
         next_day = sat;
         break;
      case sat:
         next_day = sun;
         break;
   }

   return next_day;

}


---------- FOLLOW-UP ----------

QUESTION: Hi,
An update. I played around with the code, and was able to reduce the error message to one line:

testing.c:3: error: syntax error before numeric constant

My code is the following:

#include <stdio.h>

enum day {sun=0, mon, tue, wed, thur, fri, sat};


typedef enum day Day;


Day find_next_day(Day day);

int main(void)
{
  int error;
  Day x, next_day;
  
  do {
      printf("\n Please enter an integer for the starting day.  ");
      scanf("%d", &x);
      if (error = ( x> 6 || x < 0))
         printf ("\n ERROR: The value must be within the range of 0 to 6.");
      } while (error);


  next_day = find_next_day (x);

  printf ("%d", next_day);

  return 0;
}



Day find_next_day(Day day)

{

  return (Day) ((1 + (int)day) %7);

}

I tried writing another code that is similar but with the purpose of finding the next month. It actually compiles, but it doesn't return the correct month. All it returns is "-7"... no matter what input I enter. Each time I enter a value I made sure its within the contraints. The two codes are the exact same exact for month/day. I am really confused why one compiles and the other doesn't. And why the one that compiles always returns -7. The code is the following:

#include <stdio.h>

enum month {jan=0, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};

typedef enum month Month;

Month next_month(Month month);

int main(void)
{
Month x, nextm;

printf("Enter. ");
scanf("%d", x);

nextm = next_month(x);

printf("%d", nextm);

return 0;
}


Month next_month(Month month)
{
return (Month)( (1 + (int)month) % 12 );
}

Thanks for your help. Sorry for bothering you so many times.

Answer
Hello Lingjun

Don't worry, you are not bothering me with your questions.

On my system, your day program compiles with no errors. Try changing the names of the days, perhaps make them start with capitals. If one of the day names is already defined as something in your compiler system then it will be like putting a number into your enum list. Try changing sun to Sun, mon to Mon, etc. Which compiler are you using? I am using Visual Studio.

There is an error in your month code. The scanf("%d", x); shoud be scanf("%d", &x); You need to pass the address of x into scanf using &x. That is probably the cause of the wrong output.

Finally, I agree that the modulo method is compact, but it goes against the spirit of using an enum. One point of the enum is to not tie the program to particular values. You should be able to change the values of the constants without breaking the program. With the modulo calculation, you are forced to use particular numbers for the days. For this particular problem, I also would use the modulo operator, but your instructor may be trying to teach a particular point about enums. Unfortunately your instructor chose a problem that is easily solved in other ways.

Best regards
Zlatko  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.