Help With Simple Program...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #16
    You may have to declare the integer i outside the for loop:
    Code:
    int i;
    for(i = 0; i...

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #17
      Originally posted by Narc0
      I need a source code because I don't know how to code it.
      Allow me to make the following suggestion:
      Read the C/C++ tutorials by Banfa and Gannon11.
      Not only are these great pieces of writing, but they cover exactly the type of program you are trying to write.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #18
        Originally posted by Narc0
        Bloodshed's Dev-C++. I need a source code because I don't know how to code it.
        How much of the C/C++ language do you know?

        Can you write a program that prints "Hello World"?

        Comment

        • Narc0
          New Member
          • Jan 2007
          • 18

          #19
          I tried something but I want to do it differently. Here's the code:

          #include <stdio.h>
          #include <stdlib.h>

          int main(int argc, char *argv[])
          {
          int Switch1=0;
          int Switch2=0;
          int Switch3=0;
          int Switch4=0;
          printf("To turn a switch on, press 1, otherwise press 0\n");

          printf("Switch1 =");
          scanf("%d",&Swi tch1);

          printf("Switch2 =");
          scanf("%d",&Swi tch2);

          printf("Switch3 =");
          scanf("%d",&Swi tch3);

          printf("Switch4 =");
          scanf("%d",&Swi tch4);


          if (Switch1>1 && (Switch2>1||Swi tch3>1||Switch4 >1) ||(Switch1<0 && (Switch2<0||Swi tch3<0||Switch4 <0)))

          printf("ERROR: 'Numbers have to be 1 and 0'\n");


          if(Switch1==1 && (Switch2==1||Sw itch3==1||Switc h4==1))

          printf("\nLight is on\n");

          else

          printf("\nLight is off\n");
          return 0;
          }


          Is there another way I could do the same...?

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #20
            Code:
            #include <stdio.h>
            #include <stdlib.h>
            
            int main(int argc, char *argv[])
            {
              int Switch[4], i;
              for(i = 1; i <= argc; i++)
                if(argv[i] == '1' || argv[i] == '2' || argv[i] == '3' || argv[i] == '4')
                  Switch[atoi(argv[i]) - 1] = 1;
                else
                  printf("%s?!?  That isn't one of the switches!", argv[i]);
              if(Switch[0] + Switch[1]+Switch[2]+Switch[3] > 1) printf("%s","Let there be light.");
              else printf("%s","Let there be night.");
              return 0;
            }

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #21
              Holy Mackerel! I am in actual physical pain from reading this thread. You guys have the patience of a saint, I bailed out a long time ago.

              Comment

              • Narc0
                New Member
                • Jan 2007
                • 18

                #22
                Thanks RedSon for being so helpfull. Remember, you started like me.

                Anyways, I compiled that last code and I got an error. Line8 "[Warning] comparison between pointer and integer"

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #23
                  Originally posted by Narc0
                  Thanks RedSon for being so helpfull. Remember, you started like me.

                  Anyways, I compiled that last code and I got an error. Line8 "[Warning] comparison between pointer and integer"
                  While it is true that my skill levels starting out were like yours I can assure that I never started like you. I respected the knowledge and lessons that my instructors were trying to impart on me and I never cut-and-pasted a homework question to a forum and expected someone else to do it for me. Before I post a question I always do an extensive google search on both the web and google groups. After that I search the forum in which I am posting to see if another person has posted something similar.

                  I also, never ask anyone to download anything from any website espically something that is an executible or in binary. Do you know how easy it is to write a virus? One that is not in any catalog nor can be detected easily by virus scanning heuristics. Next time do a little bit of leg work on your own and then show us that you have done the proper amount of work on your own by saying what you tried, how it failed, and why you think it doesn't work.

                  Furthermore you should be kissing the gound that

                  Comment

                  • DeMan
                    Top Contributor
                    • Nov 2006
                    • 1799

                    #24
                    And patient too...

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #25
                      Originally posted by RedSon
                      While it is true that my skill levels starting out were like yours I can assure that I never started like you. I respected the knowledge and lessons that my instructors were trying to impart on me and I never cut-and-pasted a homework question to a forum and expected someone else to do it for me. Before I post a question I always do an extensive google search on both the web and google groups. After that I search the forum in which I am posting to see if another person has posted something similar.

                      I also, never ask anyone to download anything from any website espically something that is an executible or in binary. Do you know how easy it is to write a virus? One that is not in any catalog nor can be detected easily by virus scanning heuristics. Next time do a little bit of leg work on your own and then show us that you have done the proper amount of work on your own by saying what you tried, how it failed, and why you think it doesn't work.

                      Furthermore you should be kissing the gound that
                      Damn timeout.

                      kissing the ground that Motoma walks on for sticking with you for this long. If you want to be a programmer you are going to show us that you are willing to put in just as much time and effort as we do helping you.

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #26
                        Originally posted by DeMan
                        And patient too...
                        Huh? What are you refering to here?

                        Comment

                        • Narc0
                          New Member
                          • Jan 2007
                          • 18

                          #27
                          I never said I am not grateful for the help, the only thing I disliked was your comment. Not necessary.

                          Anyways, any other help?

                          THANKS!

                          Comment

                          • Motoma
                            Recognized Expert Specialist
                            • Jan 2007
                            • 3236

                            #28
                            Originally posted by Narc0
                            Thanks RedSon for being so helpfull. Remember, you started like me.

                            Anyways, I compiled that last code and I got an error. Line8 "[Warning] comparison between pointer and integer"
                            My apologies, we have come to the limits of my knowledge. Sorry I couldn't help you more.
                            If someone else, more knowledgable than I could come in and correct my code, I am sure both Narc0 and I would be more than grateful.

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #29
                              Code:
                              #include <stdio.h>
                              #include <stdlib.h>
                              
                              int main(int argc, char *argv[])
                              {
                                int Switch[4], i;
                                for(i = 1; i <= argc; i++)
                              [b]Problem here discussed below */[/b]
                                  if(argv[i] == '1' || argv[i] == '2' || argv[i] == '3' || argv[i] == '4')
                                    Switch[atoi(argv[i]) - 1] = 1;
                                  else
                                    printf("%s?!?  That isn't one of the switches!", argv[i]);
                                if(Switch[0] + Switch[1]+Switch[2]+Switch[3] > 1) printf("%s","Let there be light.");
                                else printf("%s","Let there be night.");
                                return 0;
                              }
                              The problem is that argv is a pointer to and array of pointers char *argv[] or equivilently char **argv. So argv[i] has type char *, it is a pointer to an array of characters (and in this case these arrays of characters are NULL terminated strings).

                              I guess you are trying to test the first character in the string, for instance
                              Code:
                              char hw[] = "Hello World";
                              
                              if (hw[0] == 'H')
                              {
                                  printf("Hello is capitalised\n");
                              }
                              since you are comparing to '1' so you need an extra level of dereferencing

                              Code:
                                  if(argv[i][0] == '1' || argv[i][0] == '2' || argv[i][0] == '3' || argv[i][0] == '4')
                              note however this will not handle a command line of this nature

                              ProgramName 1234

                              because 1234 with come through as a single entry in the argv array.

                              Comment

                              • Motoma
                                Recognized Expert Specialist
                                • Jan 2007
                                • 3236

                                #30
                                Originally posted by Banfa
                                Code:
                                #include <stdio.h>
                                #include <stdlib.h>
                                
                                int main(int argc, char *argv[])
                                {
                                  int Switch[4], i;
                                  for(i = 1; i <= argc; i++)
                                [b]Problem here discussed below */[/b]
                                    if(argv[i] == '1' || argv[i] == '2' || argv[i] == '3' || argv[i] == '4')
                                      Switch[atoi(argv[i]) - 1] = 1;
                                    else
                                      printf("%s?!?  That isn't one of the switches!", argv[i]);
                                  if(Switch[0] + Switch[1]+Switch[2]+Switch[3] > 1) printf("%s","Let there be light.");
                                  else printf("%s","Let there be night.");
                                  return 0;
                                }
                                The problem is that argv is a pointer to and array of pointers char *argv[] or equivilently char **argv. So argv[i] has type char *, it is a pointer to an array of characters (and in this case these arrays of characters are NULL terminated strings).

                                I guess you are trying to test the first character in the string, for instance
                                Code:
                                char hw[] = "Hello World";
                                
                                if (hw[0] == 'H')
                                {
                                    printf("Hello is capitalised\n");
                                }
                                since you are comparing to '1' so you need an extra level of dereferencing

                                Code:
                                    if(argv[i][0] == '1' || argv[i][0] == '2' || argv[i][0] == '3' || argv[i][0] == '4')
                                note however this will not handle a command line of this nature

                                ProgramName 1234

                                because 1234 with come through as a single entry in the argv array.

                                Banfa to the rescue!
                                Some yummy gooey tasty code to follow:
                                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[atoi(argv[i]) - 1] = 1;
                                    else
                                      printf("%s?!?  That isn't one of the switches!\n", argv[i]);
                                  if(Switch[0]+Switch[1]+Switch[2]+Switch[3] > 1) printf("%s","Let there be light.\n");
                                  else printf("%s","Let there be night.\n");
                                  system("pause");
                                  return 0;
                                }

                                Comment

                                Working...