A newbie question

This is a discussion on A newbie question within the C Programming forums, part of the General Programming Boards category; Hello World, I was trying to make a program that takes a password from the user and tells if it ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    A newbie question

    Hello World,

    I was trying to make a program that takes a password from the user and tells if it true or false.
    Note: Write the program without using arrays. Use getch(). 3 trials are permitted. The password is asueng.

    Here's my code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count = 0, trial = 0;
        char letter;
        while ( trial++ < 3 )
        {
            printf("Enter the password (%d): ", trial);
            while ( 1 )
            {
                letter = getch();
                if ( letter == "\n" && count == 6 )
                {
                    printf("Accepted\n");
                    return 1;
                }
                else if ( letter == "\n" && count != 6 )
                {
                    printf("Not Accepted..\n");
                    break;
                }
                else
                {
                    printf("*");
                   if ( (letter == 'a' && count == 0) ||
                            ( letter == 's' && count == 1 ) ||
                            ( letter == 'u' && count == 2 ) ||
                            ( letter == 'e' && count == 3 ) ||
                            ( letter == 'n' && count == 4 ) ||
                            ( letter == 'g' && count == 5 )
                            )
                        count += 1;
                    else
                    {
                        if ( count > 0 )
                            count -= 1;
                        else
                            count = 0;
                    }
                }
            }
        }
        return 0;
    }
    The problem I'm facing is when I press Enter it prints a * which something I don't want. It should execute the code in if or the first else if. What's going wrong?

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are using double quotes where you should be using single quotes.

    " " = a string
    ' ' = a character

    You should probably compile with the -Wall flag (turn up the warning level on your compiler).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    Your compiler should be issuing a diagnostic (probably as a warning). This should be heeded.

    If your compiler is not issuing a diagnostic, switch to a good compiler (gcc, for example).

    It's fairly common for those new to C to assume that warnings are fine, but they often indicate real problems.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    @Quazah:

    No. It's not that.

    I have tried this just now, but nothing new... the same problem I'm facing is still occurring.

    Quote Originally Posted by cas View Post
    Your compiler should be issuing a diagnostic (probably as a warning). This should be heeded.

    If your compiler is not issuing a diagnostic, switch to a good compiler (gcc, for example).

    It's fairly common for those new to C to assume that warnings are fine, but they often indicate real problems.
    Yes.. I saw a warning saying something about pointers and integers. I can't remember the msg as it hasn't appeared again. The warning was pointing to 2 lines: the first if and the else if lines.
    Last edited by jokes_finder; 03-31-2011 at 04:13 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jokes_finder View Post
    @Quazah:

    No. It's not that.
    ...
    I can't remember the msg as it hasn't appeared again. The warning was pointing to 2 lines: the first if and the else if lines.
    Of course it's gone. You just said you switched out the ' ' for the " ", and now mysteriously, your warnings are gone. Funny how that worked out.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by quzah View Post
    Of course it's gone. You just said you switched out the ' ' for the " ", and now mysteriously, your warnings are gone. Funny how that worked out.


    Quzah.
    That's right... I tried again the double quotes ans the warning appeared

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jokes_finder View Post
    That's right... I tried again the double quotes ans the warning appeared
    Sooo... now you know there is a definate correlation between programming errors and compiler messages...
    Imagine that....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie expression tree question
    By Orange Lozenge in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2011, 10:36 AM
  2. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 05:24 AM
  3. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  4. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21