i am getting a problem it dose not give me the correct out put

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravind12345
    New Member
    • Jan 2014
    • 22

    i am getting a problem it dose not give me the correct out put

    (i am new to programing in c)
    ( http://www.youtube.com/watch?v=DHtC4IkmPLw this link will explain what the real problem is. )
    when i compile this peace of program i get a problem and that is when i run this program the problem is that it does not give me the correct output

    And this is the program

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char c[15], d[15], f[15];
    int age[2];
    printf("Hello starshine the earth says hello.My name is willy wonka i shake you warmly by the hand.");
    printf("Please enter your age: ");
    scanf("%s",age);
    printf("ok now enter a prefered username of 9 letters only:  ");
    scanf("%s",c);
    printf("ok now enter a prefered password: ");
    scanf("%s",d);
    printf("and the last step is the prefered name you would like to be known:  ");
    scanf("%s",f);
    printf("Now we are going to munch all the information we have this may take a few seconds. ");
    switch ( age[2] )
    {
    case '15': puts("Account successfuly created.   ");
    break;
    case '16': puts("Account successfuly created.   ");
    break;
    case '17': puts("Account successfuly created.   ");
    break;
    case '18': puts("Account successfuly created.   ");
    break;
    case '19': puts("Account successfuly created.   ");
    break;
    case '20': puts("Account successfuly created.   ");
    break;
    default: puts("you are too young/ too old to create an account.");
    }
    getch();
    clrscr();
    }
    Last edited by Rabbit; Jan 14 '14, 05:19 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 17 age[2] is a single character which would be something like '1' or '5'. All you cases statements try to use 2 character characters, that is a single quote ' is used to delimit a character and only a single character may appear between them. I would expect you compiler to be emitting warnings about all you case values because '15' is not a valid character it would have to be '1' or '5'.

    Additionally you seem to support a very limited range of age values, what if someone inputs an age of 9 how about 35?

    What your program should be doing is getting the age as an integer and then using comparisons i.e.

    Pseudo Code
    Code:
    age = get number from user
    if (age > 14 && age < 21)
    {
      print account created
    }
    else
    {
      print user to young/old
    }

    Comment

    • aravind12345
      New Member
      • Jan 2014
      • 22

      #3
      Hey thanks man I will try it in the morning have a good night

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Thanks, apart from the fact it is mid-afternoon here of course :D

        Comment

        • aravind12345
          New Member
          • Jan 2014
          • 22

          #5
          We'll its just morning here a free eight hours

          Comment

          • aravind12345
            New Member
            • Jan 2014
            • 22

            #6
            It didn't work i am still get the same problem

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              If you want help you need to provide more detail of what you have tried (i.e. your current code) and what the precise problem is.

              Comment

              Working...