Help With Simple Program...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #31
    You realise that you can re-write

    Switch[atoi(argv[i]) - 1] = 1;

    as

    Switch[argv[i][0] - '1'] = 1;

    which removes the call to silly olfd atoi and is slightly more efficient.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #32
      Originally posted by Banfa
      You realise that you can re-write

      Switch[atoi(argv[i]) - 1] = 1;

      as

      Switch[argv[i][0] - '1'] = 1;

      which removes the call to silly olfd atoi and is slightly more efficient.
      You learn something new every day...well, I do...
      More so since I've been reading your posts!

      Comment

      • Narc0
        New Member
        • Jan 2007
        • 18

        #33
        That does the work but there's still a problem.

        There's a switch that is the main one. This switch MUST be on and any of the other ones to turn the light on. So that's the main switch. The program turns the light on with any combination of the switches, and the light can only turn on with this main switch and any of the other ones.

        Also, when I put, for example, ProjectName 5, since that switch doesn't exist the program tells me that the switch doesn't exist. That's correct and that's what I want but it ALSO tells me that the light is on.

        Any help?

        THANKS!

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #34
          Originally posted by Narc0
          That does the work but there's still a problem.

          There's a switch that is the main one. This switch MUST be on and any of the other ones to turn the light on. So that's the main switch. The program turns the light on with any combination of the switches, and the light can only turn on with this main switch and any of the other ones.

          Also, when I put, for example, ProjectName 5, since that switch doesn't exist the program tells me that the switch doesn't exist. That's correct and that's what I want but it ALSO tells me that the light is on.

          Any help?

          THANKS!
          Motoma has provided you with more than enough help to finish this. How about you try and make this modification yourself and post back the code if you have trouble.

          It is not a very hard modification as the provided code already reads all the switches for you and already determins the state of the light even if there are bad switches on the command line. So all you have to is alter the logic about how it works out if the light is on or off.


          It is time for you to read, understand and modify what has already been provided yourself rather than just keep posting back here with apparently no effort on your part.

          The if you modify the code but have trouble getting the modification to work you should post back here.

          Comment

          • Narc0
            New Member
            • Jan 2007
            • 18

            #35
            Code:
            #include <stdio.h>
            #include <stdlib.h>
            
            int main(int argc, char *argv[])
            {
              int Switch[4] = {0,0,0,0}, i;
              for(i = 1; i < argc; i++)
                if((argv[i][0] == '1'  && argv[i][1] == '\0') ||
                    (argv[i][0] == '2'  && argv[i][1] == '\0') ||
                    (argv[i][0] == '3'  && argv[i][1] == '\0') ||
                    (argv[i][0] == '4'  && argv[i][1] == '\0'))
                  Switch[argv[i][0] - '1'] = 1;
                else
                  printf("%s?!?  This is not a switch!\n", argv[i]);
                  
              if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1))
              printf("%s","Light is on.\n");
              
              
              else 
                   printf("%s","Light is off.\n");
              return 0;
            }



            I worked out the logic and now the program knows that switch 1 is the main one and without it the light will never turn on. But I have a problem. When I input a number greater than 4, it tells that that isn't a switch and that's perfect but it also tells me that the light is off. I'm stuck in making that part right.

            THANKS!

            Edit: I also would want another way to write this but with the same results:

            if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1))
            printf("%s","Li ght is on.\n");

            Thanks!
            Last edited by Banfa; Jan 26 '07, 06:53 PM. Reason: Added [code]...[/code] tags round the code

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #36
              Originally posted by Narc0
              #include <stdio.h>
              #include <stdlib.h>

              int main(int argc, char *argv[])
              {
              int Switch[4] = {0,0,0,0}, i;
              for(i = 1; i < argc; i++)
              if((argv[i][0] == '1' && argv[i][1] == '\0') ||
              (argv[i][0] == '2' && argv[i][1] == '\0') ||
              (argv[i][0] == '3' && argv[i][1] == '\0') ||
              (argv[i][0] == '4' && argv[i][1] == '\0'))
              Switch[argv[i][0] - '1'] = 1;
              else
              printf("%s?!? This is not a switch!\n", argv[i]);

              if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1))
              printf("%s","Li ght is on.\n");


              else
              printf("%s","Li ght is off.\n");
              return 0;
              }





              I worked out the logic and now the program knows that switch 1 is the main one and without it the light will never turn on. But I have a problem. When I input a number greater than 4, it tells that that isn't a switch and that's perfect but it also tells me that the light is off. I'm stuck in making that part right.

              THANKS!

              Edit: I also would want another way to write this but with the same results:

              if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1))
              printf("%s","Li ght is on.\n");

              Thanks!
              Could you reply to this simply writing how you called this on the command line?
              i.e:
              Code:
              MyProg.exe 1 3 44 2
              Such that it displays the behavior you don't want.

              Comment

              • Narc0
                New Member
                • Jan 2007
                • 18

                #37
                Edit:

                Oh. I write

                Whatever 5

                and since 5 doesn't exist, it tells me that same thing BUT also that the light is off.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #38
                  Originally posted by Narc0
                  I worked out the logic and now the program knows that switch 1 is the main one and without it the light will never turn on. But I have a problem. When I input a number greater than 4, it tells that that isn't a switch and that's perfect but it also tells me that the light is off. I'm stuck in making that part right.
                  Are you saying that when you input

                  ProgramName 1 2 5

                  It tells you the light is off? because that is not how I am reading the code (perhaps I should try running it).

                  Originally posted by Narc0
                  Edit: I also would want another way to write this but with the same results:

                  if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1))
                  printf("%s","Li ght is on.\n");
                  Exactly what about this line of code don't you like (or do you want to change). It seems slightly strange to reject a perfectly good line of code so it would be handy to know the reason before making another attempt.

                  Comment

                  • Narc0
                    New Member
                    • Jan 2007
                    • 18

                    #39
                    Also, if I input:

                    Projectname 1 2 5

                    It tells me the switch(5) doesn't exist but also tells me the light is on...

                    And I want another way simply because I am a curious person and want to know what other ways you could do the same.

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #40
                      Originally posted by Narc0
                      Edit:

                      Oh. I write

                      Whatever 5

                      and since 5 doesn't exist, it tells me that same thing BUT also that the light is off.
                      But the light IS off.

                      Perhaps what you want is this:

                      Code:
                      #include <stdio.h>
                      #include <stdlib.h>
                      
                      int main(int argc, char *argv[])
                      {
                      int Switch[4] = {0,0,0,0}, i;
                      for(i = 1; i < argc; i++)
                      if((argv[i][0] == '1' && argv[i][1] == '\0') ||
                      (argv[i][0] == '2' && argv[i][1] == '\0') ||
                      (argv[i][0] == '3' && argv[i][1] == '\0') ||
                      (argv[i][0] == '4' && argv[i][1] == '\0'))
                      Switch[argv[i][0] - '1'] = 1;
                      else
                      {
                        printf("%s?!? This is not a switch!\n", argv[i]);
                        return -1;
                      }
                      
                      if(Switch[0]==1 && (Switch[1]==1||Switch[2]==1||Switch[3]==1)) printf("%s","Light is on.\n");
                      else printf("%s","Light is off.\n");
                      return 0;
                      }

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #41
                        Originally posted by Narc0
                        It tells me the switch(5) doesn't exist but also tells me the light is on...
                        Isn't that what you want?
                        Originally posted by Narc0
                        And I want another way simply because I am a curious person and want to know what other ways you could do the same.
                        Equivalents of assuming Switch has been initialise to 0

                        if (Switch[0]==1 && (Switch[1]==1 || Switch[2]==1 || Switch[3]==1))

                        if (Switch[0] && (Switch[1] || Switch[2] || Switch[3]))

                        if (Switch[0]==1 & (Switch[1]==1 | Switch[2]==1 | Switch[3]==1))

                        if (Switch[0]==1 && (Switch[1]+Switch[2]+Switch[3]))

                        if (Switch[0]==1 && (Switch[1]+Switch[2]+Switch[3] > 0))

                        if (Switch[0]==1 && (Switch[1]+Switch[2]+Switch[3] >= 1))

                        if (Switch[0]*(Switch[1]+Switch[2]+Switch[3]) > 0)


                        That might not be all it's just an random selection, personally I think what you have is the best.

                        Comment

                        • Narc0
                          New Member
                          • Jan 2007
                          • 18

                          #42
                          Exactly!

                          Thanks man!

                          BTW, any other way to do the thing I put above?

                          Comment

                          • Narc0
                            New Member
                            • Jan 2007
                            • 18

                            #43
                            Originally posted by Banfa
                            Isn't that what you want?
                            No, what I want is the program to tell me the switch doesn't exist and that's all.

                            Comment

                            • Motoma
                              Recognized Expert Specialist
                              • Jan 2007
                              • 3236

                              #44
                              Originally posted by Narc0
                              No, what I want is the program to tell me the switch doesn't exist and that's all.
                              See my last post.

                              Comment

                              • Narc0
                                New Member
                                • Jan 2007
                                • 18

                                #45
                                Yeah, I saw it.

                                Guys, millions thanks to all of you!

                                Comment

                                Working...