Thread: If Else - Magic Game

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    6

    If Else - Magic Game

    magic.txt

    Please solve my problem!

    When i am entering single character instead of 'miz' then it is working great. But when i am using multi character like 'miz' it is showing error output. i did this program with c++ it is working but not working in c.

    When i am running this program it is showing {warning] multi character constant.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Next time, please post your code in [code][/code] tags, making sure to post well-formatted code (so we can read it) and to post it as plain text (so our line numbering and syntax highlighting will work). It should look like
    Code:
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
        char a;
     
       printf("Please Enter your First Name: ");
        scanf("%s",&a);
     
       if(a=='miz')
        {
            printf("\n\aYou are awesome!");
        }
     
       else
        {
            printf("\n\aYou are not awesome!");
        }
     
       getch();
    }
    You should declare main to explicitly return an int, and I prefer to explicitly state no parameters (i.e. 'void' in the param list):
    Code:
    int main(void)
    Single 'quotes' are for character literals. Double "quotes" are for string literals. You want double quotes. Also, you can't compare strings with == in C. You need to use the strcmp function.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    it's not working.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code and how does it not work, after you have made the changes implied by anduril462 in post #2?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Look this is very simple program. I don't need to use ' strcmp' Please tell me what's my mistake in Line 11 or solve. Write same program without error.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please tell me what's my mistake in Line 11
    Anduril already did so clearly, in post #2.

    I don't need to use ' strcmp'
    You're trying to compare two strings. Using a "string compare" function is clearly the best way to go. If you have a reason to disagree, please explain.

    Write same program without error.
    Homework policy. It is up to you to write the code. If you disregard the advice you're given, then there is little else we can do to help.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Loucogame View Post
    Look this is very simple program. I don't need to use ' strcmp' Please tell me what's my mistake in Line 11 or solve. Write same program without error.
    If you know so much about C, then you wouldn't need to come to us for help. But you did come to us for help. You are the novice, we are the experts (or at least, we probably know more than you). I suggest you actually listen to our advice instead of telling us we're wrong, especially when we're right. We don't like helping eople who have crappy attitudes and don't listen to the advice we give.

    I already told you your mistake with line 11. Re-read my post #2.

    You want to compare names. Names typically consist of multiple characters, therefore you must use a string, not a single character.

    Technically you don't need to use strcmp to compare strings, but if you don't, you must implement something similar to it to check if strings are equal. You can not compare strings with == in C. Using == to compare strings does not work the way you expect it (it compares the addresses of each string, not their contents).

    Note, you have another mistake too. You intend to store a string (several characters), so you must declare a variable that holds several characters. a is not such a variable. a only stores a single character. Try declaring it as an array of char. You will need to adjust your scanf too (line 9).

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Homework policy
    . It is up to you to write the code. If you disregard the advice you're given, then there is little else we can do to help.
    This is not my homework, i'm doing for learning myself.

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    If you know so much about C, then you wouldn't need to come to us for help. But you did come to us for help. You are the novice, we are the experts (or at least, we probably know more than you). I suggest you actually listen to our advice instead of telling us we're wrong, especially when we're right. We don't like helping eople who have crappy attitudes and don't listen to the advice we give.

    I already told you your mistake with line 11. Re-read my post #2.

    You want to compare names. Names typically consist of multiple characters, therefore you must use a string, not a single character.

    Technically you don't need to use strcmp to compare strings, but if you don't, you must implement something similar to it to check if strings are equal. You can not compare strings with == in C. Using == to compare strings does not work the way you expect it (it compares the addresses of each string, not their contents).

    Note, you have another mistake too. You intend to store a string (several characters), so you must declare a variable that holds several characters. a is not such a variable. a only stores a single character. Try declaring it as an array of char. You will need to adjust your scanf too (line 9).
    Hey why are you getting angry. I was just asking. I did this program years ago but i didn't use strcmp.That time program were running without any kind of error.
    When i am using single character instead of miz then it is running. Please tell me how can i do that. This is not my homework.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Loucogame View Post
    Hey why are you getting angry. I was just asking. I did this program years ago but i didn't use strcmp.That time program were running without any kind of error.
    When i am using single character instead of miz then it is running. Please tell me how can i do that. This is not my homework.
    I was a little angry because I told you what was wrong and you told me I was wrong and ignored my advice. It makes us feel like you're wasting our time (which we volunteer between our many other obligations).

    You used C++ before. In C++ you can compare strings with ==. In C you can not. They are different languages with very different capabilities when it comes to working with strings.

    A string (multiple characters in a row, like miz) is different from a single character in C. I already told you what you need if you want to compare strings. Reread my posts #2 and #7.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Loucogame
    This is not my homework, i'm doing for learning myself.
    In other words, you have assigned yourself homework to do. That is a great way to learn, but you need to do your own homework for the learning to take place.

    Quote Originally Posted by Loucogame
    Hey why are you getting angry. I was just asking.
    If you were "just asking", then you should have asked "why do I need to use 'strcmp'?" (or some other question) instead of stating "I don't need to use ' strcmp'".

    Quote Originally Posted by Loucogame
    I did this program years ago but i didn't use strcmp.That time program were running without any kind of error.
    Maybe the program that you wrote did something different, or maybe you wrote your own string comparison function. We don't know, and apparently you have forgotten, so move on to the present.

    Quote Originally Posted by Loucogame
    When i am using single character instead of miz then it is running.
    However, it would still be wrong because you declared the variable a to be a char, yet you read into it as if it could store a string (of non-zero length).

    The fixes needed have already been described in this thread:
    • Make a be an array of char large enough to store strings of your desired maximum length.
    • Change the scanf call to correctly read a string into a
    • Use a string literal instead of a multicharacter constant by changing the single quotes into double quotes, i.e., "miz" instead of 'miz'
    • Correctly compare strings by using strcmp, or otherwise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I doing this pointer magic the right way?
    By itsinthecompute in forum C Programming
    Replies: 14
    Last Post: 06-13-2013, 05:02 PM
  2. Magic Box
    By dantu1985 in forum C Programming
    Replies: 5
    Last Post: 08-18-2007, 12:04 PM
  3. Magic Squares
    By KidCourageous in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2004, 12:26 AM
  4. the magic of MAGIC numbers
    By borko_b in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-27-2002, 02:36 AM
  5. A little STL magic please?
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2002, 05:56 PM

Tags for this Thread