beginning problems, please help.

This is a discussion on beginning problems, please help. within the C Programming forums, part of the General Programming Boards category; I'm trying to learn C and I'm having some problems. What is wong with this code Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    beginning problems with strcmp, please help.

    I'm trying to learn C and I'm having some problems.

    What is wong with this code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        
    char x, old, young;
    int a, b, c;
    
    printf ("Enter Name (type exit to end):");
    scanf("%s",x);
    
    if (strcmp(x,"exit"))
    {
          printf("equal");  /* just using this to test if statement
          }
      
        }
    I'm getting error: [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

    but don't know what it means.

    thanks for your help.
    Last edited by scarr105; 03-15-2005 at 02:27 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You only have one character. If you want a string, you'll need more than one. How on earth could you possibly store the word "exit" in a single character?
    Code:
    char x, old, young;
    I'll assume you've been told what an array is at this point, since you're using string functions...

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

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Well I don't know if I completely understand your answer. I'm reading a book "The complete reference C fourth edition" the book doesn't explain things as well as I would like.

    if I understand you answer I would guess I need to do somethign like this

    char x[5]

    is this right.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Also, strcmp returns 0 if the strings are equal so your if test won't work as you think it does. As it stands now, if the strings turn out to be the same, then the "equal" output is going to be skipped.

    You should work on spacing/formatting you code a little bit better. For small examples like this it isn't a big deal, but for larger samples it starts to become a real hinderance.

    Quote Originally Posted by scarr105
    if I understand you answer I would guess I need to do somethign like this

    char x[5]

    is this right.
    At a minimum yes. You need to be able to store at least as many characters (plus one for the NULL) that you expect the user to enter.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    that was just part of the code the whole program is going to have the use enter a name and age of as many as the wish and when they type exit it will show the youngest persons name and ag along with the oldest persons name and age.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you'd probably be better off with something like K&R's book. Ususally the refernece tomes are better for well, reference. They're not great at teaching, but rather clarifying things and explaining the language and such.


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

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    I know this, that is way I also joined this forum hoping people would help when I got stumped or need some help. If you don't feel this is a forum where a beginner should be than please direct me to a forum that will help beginners.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I did just help you. Where exactly did I say anything at all about this forum? I said most reference books are not good at teaching the language. Just like you wouldn't read a dictionary or encyclopedia to learn a language.

    As a matter of fact, read the fist reply you got. Guess what? That was help. I told you you can't store a word in a single character, and mentioned arrays as your solution. How is that not helping. Before I shred you, perhaps you should read this:

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

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

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    ok I think I got over that first huddle but now when I run my program I get a windows error and the program closes down.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        
    char x[35], old, young;
    int a, b, c;
    
    while(strcmp(x,"exit")){
          printf ("Enter Name (type exit to end):");
          scanf("%s",x);
    
          printf ("Enter age:");
          scanf("%g",a);
    
    }
    
        }
    if I understand this code it should just loop until until a 0 is return which would mean someone type exit.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    This is the right forum, just the way you are trying to learn is a bit odd. You should get a beginners book to really understand how the language works.
    When no one helps you out. Call google();

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your use of scanf is wrong in the second usage. scanf expects pointers for all of its arguments. If you have a pointer to pass to it, you just pass the pointer directly, as you did with the array name. Array names are treated, for the most part, as pointers. If you do not have a pointer, then you need to pass the address-of the variable to scanf. This is what you need to do for the second scanf line. To get the address-of a variable, you use the address-of operator, which is: &. Therefore:
    Code:
    scanf("%g", &a );
    Assuming you did of course mean to use %g.

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

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    ok I got you thank you, don't know why I can't remember that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM
  4. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  5. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM

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