Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. if/else, expression can not be used as a function. C program

if/else, expression can not be used as a function. C program

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cstudent1
    wrote on last edited by
    #1

    Hi everyone, I am taking my first programming class and am at a loss on this program. Any tips for making this wrk? Thanks! #include int main() { int month,day,opselect; printf("\nEnter a month between 1 and 12: \n "); scanf("%d",&month); while (month < 1 || month > 12) { printf("Error the month entered is not valid.\n"); printf("\nEnter a month between between 1 and 12: "); scanf("%d",&month); } printf("The month accepted is %d\n",month); printf("\nEnter a day between 1 and 31: \n "); scanf("%d",&day); scanf("%d",opselect); switch (opselect) if (month=2) (day < 1 || day > 28); { printf("Error the day entered is not valid.\n"); printf("\nEnter a day between 1 and 28\n"); scanf("%d,&day"); } else (month=4,6,9,11) (day < 1 || day > 30); { printf("Error the day entered is not valid.\n"); printf("\nEnter a day between 1 and 30\n"); scanf("%d,&day"); } return 0; } errors are "else w/o a previous if" and "expression can not be used as a function"

    J 1 Reply Last reply
    0
    • C cstudent1

      Hi everyone, I am taking my first programming class and am at a loss on this program. Any tips for making this wrk? Thanks! #include int main() { int month,day,opselect; printf("\nEnter a month between 1 and 12: \n "); scanf("%d",&month); while (month < 1 || month > 12) { printf("Error the month entered is not valid.\n"); printf("\nEnter a month between between 1 and 12: "); scanf("%d",&month); } printf("The month accepted is %d\n",month); printf("\nEnter a day between 1 and 31: \n "); scanf("%d",&day); scanf("%d",opselect); switch (opselect) if (month=2) (day < 1 || day > 28); { printf("Error the day entered is not valid.\n"); printf("\nEnter a day between 1 and 28\n"); scanf("%d,&day"); } else (month=4,6,9,11) (day < 1 || day > 30); { printf("Error the day entered is not valid.\n"); printf("\nEnter a day between 1 and 30\n"); scanf("%d,&day"); } return 0; } errors are "else w/o a previous if" and "expression can not be used as a function"

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      You code contains many errors: The scanf() call for opselect misses the ampersand &. There is an empty switch statement. C/C++ comparisons use two equal signs. This is a complete if sequence:

      if (month=2)
      (day < 1 || day > 28);

      Because there is other code following before the else, the compiler can not found any corresponding if and generates an error. The code following the if statement is an expression (day < 1 || day > 28). But the compiler expects a statement or a function call and generates another error. As already noted, comparisons use two equal signs. Your code assings the value 2 to month and the if condition is always true. I guess you want something like this:

      if (month == 2 && (day < 1 || day > 28))
      {
      // some code
      }
      else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
      {
      // some code
      }

      C 1 Reply Last reply
      0
      • J Jochen Arndt

        You code contains many errors: The scanf() call for opselect misses the ampersand &. There is an empty switch statement. C/C++ comparisons use two equal signs. This is a complete if sequence:

        if (month=2)
        (day < 1 || day > 28);

        Because there is other code following before the else, the compiler can not found any corresponding if and generates an error. The code following the if statement is an expression (day < 1 || day > 28). But the compiler expects a statement or a function call and generates another error. As already noted, comparisons use two equal signs. Your code assings the value 2 to month and the if condition is always true. I guess you want something like this:

        if (month == 2 && (day < 1 || day > 28))
        {
        // some code
        }
        else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
        {
        // some code
        }

        C Offline
        C Offline
        cstudent1
        wrote on last edited by
        #3

        Thanks, I am trying to implement your suggestions here is what I currently have.. #include #define MAXCOUNT 10 int main () { int opselect; int day, month; printf("Enter a month between 1-12 :"); scanf("%d", &month); printf("Enter a number for the day of the month"); scanf("%d", &day); scanf("%d", &opselect); switch (opselect) { case 1: if (month==2 && (day < 1 || > 28)) {printf("\nEnter a day between 1 and 28: \n "); scanf("%d",&day); } case 2: else if (month==4 || month==6 || month==9 || month==11)&&(day < 1 || day > 30)) {printf("\nEnter a day between 1 and 30: \n "); scanf("%d",&day); } case 3: else if (month==3 || month==5 || month==7 || month==10 || month==12)&&(day < 1 || day > 31)) {printf("\nEnter a day between 1 and 31: \n "); scanf("%d",&day); } printf("The day accepted is %d\n",day); return 0; } I am getting a ton of errors here, expected primary-expression before else, expected; before else. What I am trying to do is based on the month input prompt for a the proper number of days in the month(2=28, 1=31 etc)and get in error message if you enter the wrong days(29 in Feb for example)

        J T 2 Replies Last reply
        0
        • C cstudent1

          Thanks, I am trying to implement your suggestions here is what I currently have.. #include #define MAXCOUNT 10 int main () { int opselect; int day, month; printf("Enter a month between 1-12 :"); scanf("%d", &month); printf("Enter a number for the day of the month"); scanf("%d", &day); scanf("%d", &opselect); switch (opselect) { case 1: if (month==2 && (day < 1 || > 28)) {printf("\nEnter a day between 1 and 28: \n "); scanf("%d",&day); } case 2: else if (month==4 || month==6 || month==9 || month==11)&&(day < 1 || day > 30)) {printf("\nEnter a day between 1 and 30: \n "); scanf("%d",&day); } case 3: else if (month==3 || month==5 || month==7 || month==10 || month==12)&&(day < 1 || day > 31)) {printf("\nEnter a day between 1 and 31: \n "); scanf("%d",&day); } printf("The day accepted is %d\n",day); return 0; } I am getting a ton of errors here, expected primary-expression before else, expected; before else. What I am trying to do is based on the month input prompt for a the proper number of days in the month(2=28, 1=31 etc)and get in error message if you enter the wrong days(29 in Feb for example)

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          You are mixing a switch statement with if - else statements. That is not allowed. What is the opselect for? It seems unnecessary. A more sophisticated solution would use an own function that checks if a day/month combination is valid:

          if (!CheckDate(month, day))
          {
          printf("\nInvalid day/month combination. Please enter a valid day:\n");
          scanf("%d",&day);
          }

          C 2 Replies Last reply
          0
          • J Jochen Arndt

            You are mixing a switch statement with if - else statements. That is not allowed. What is the opselect for? It seems unnecessary. A more sophisticated solution would use an own function that checks if a day/month combination is valid:

            if (!CheckDate(month, day))
            {
            printf("\nInvalid day/month combination. Please enter a valid day:\n");
            scanf("%d",&day);
            }

            C Offline
            C Offline
            cstudent1
            wrote on last edited by
            #5

            This assignment calls for a switch statement, I thought you needed opselect when using a switch? I tired using case:1, case 2 etc..., but I couldn't make it work. How do I do the switch w/o the if statements? Thank you so much for your help.

            1 Reply Last reply
            0
            • J Jochen Arndt

              You are mixing a switch statement with if - else statements. That is not allowed. What is the opselect for? It seems unnecessary. A more sophisticated solution would use an own function that checks if a day/month combination is valid:

              if (!CheckDate(month, day))
              {
              printf("\nInvalid day/month combination. Please enter a valid day:\n");
              scanf("%d",&day);
              }

              C Offline
              C Offline
              cstudent1
              wrote on last edited by
              #6

              Thank you!!! Removed opselect and it works!

              1 Reply Last reply
              0
              • C cstudent1

                Thanks, I am trying to implement your suggestions here is what I currently have.. #include #define MAXCOUNT 10 int main () { int opselect; int day, month; printf("Enter a month between 1-12 :"); scanf("%d", &month); printf("Enter a number for the day of the month"); scanf("%d", &day); scanf("%d", &opselect); switch (opselect) { case 1: if (month==2 && (day < 1 || > 28)) {printf("\nEnter a day between 1 and 28: \n "); scanf("%d",&day); } case 2: else if (month==4 || month==6 || month==9 || month==11)&&(day < 1 || day > 30)) {printf("\nEnter a day between 1 and 30: \n "); scanf("%d",&day); } case 3: else if (month==3 || month==5 || month==7 || month==10 || month==12)&&(day < 1 || day > 31)) {printf("\nEnter a day between 1 and 31: \n "); scanf("%d",&day); } printf("The day accepted is %d\n",day); return 0; } I am getting a ton of errors here, expected primary-expression before else, expected; before else. What I am trying to do is based on the month input prompt for a the proper number of days in the month(2=28, 1=31 etc)and get in error message if you enter the wrong days(29 in Feb for example)

                T Offline
                T Offline
                Trace1995
                wrote on last edited by
                #7

                I donot know your C program. Please talk me you realize the function. I come from China.My English is not good.I hope you can understand my replay.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups