#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Using strcpy in a C Program


    Hello I am new to the fourm. I am currently taking a basic C programming course at my local community college. I find C to be very interesting to me. In this course we are using simply gedit and the terminal to complete our programs and to run them. I am having a bit of an issue with a few lines of code.

    [CODE
    ] /*--------------------------------------------------------------*/
    /* */
    /* Set the IF and ELSE IF */
    /* */
    /*------------------------------------------------------------*/
    if (totalScore >= 1200)
    (strcpy totalMessage, "Great");
    else if (totalScore >= 900)
    (strcpy totalMessage, "Good");
    else if (totalScore >= 600)
    (strcpy totalMessage, "OK");
    else (strcpy totalMessage, "Poor");[/CODE]


    The issue I am having is I can not get my strcpy command to store the correct words into my character assignment. If anyone could lend me a hand it would be much appreciated. Also I will be seeking help from my professor so do not worry.

    Thank you from a beginning programmer
    snf1694
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Your function call syntax is all wrong. Rather the correct syntax is:
    fn_name(argument list);
    So for example, you should write strcpy(totalMessage, "OK");

    I was going to show you the errors that generated, but the error message, "parse error before `totalMessage'", wouldn't have meant much to a beginner. The meaning of error and warning messages may seem cryptic to you now, but as you gain experience and make a lot of mistakes in the process, then you will learn to associate the messages with particular types of mistakes you make. The reason why experience programmers understand those messages is because we've made a lot more mistakes than you have yet. Don't worry, you'll catch up to us. Give it time.

    But still, the error message points you to the line and tells you that the syntax is wrong. That should tell you to look at that line and figure out what's wrong. Part of figuring that out should include opening your textbook and reviewing the syntax of the type of statement you're trying to write.

    Good job attempting to use code tags in your very first post here. It would have worked too if you hadn't hit Enter before the close-bracket.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Thank you for your insight. It makes me feel better about all of the errors I am reciving and that this is just a step in the process.so after I corrected my errors, I tried compiling the program to see what would happen and the resuts are this...

    Code:
    sam@ubuntu:~$ g++ lab3.cc  lab3.cc: 
    In function ‘int main()’: lab3.cc:174:40: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive] /usr/include/string.h:128:14: error:   initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive] lab3.cc:178:39: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive] /usr/include/string.h:128:14: error:   initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive] lab3.cc:182:37: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive] /usr/include/string.h:128:14: error:   initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive] lab3.cc:186:40: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive] /usr/include/string.h:128:14: error:   initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive] lab3.cc:241:1: error: expected ‘;’ before ‘}’ token lab3.cc:241:1: error: expected ‘}’ at end of input sam@ubuntu:~$ ^C sam@ubuntu:~$
    I do remember my prof telling us to look line by line and try debugging one step at a time, however I am not sure where to start debugging. What would be my most logical choice of start?

    and thank you for your kind words, as a student seeing failure usually means lost material, but in the case of programming I am quickly finding out this is not the case.


    Thanks
    Snf1694
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    I'm about to depart now, so I don't have much time.

    It's complaining that it's expecting a char pointer (char*), which is the same thing as a char array, but instead you are giving it a single char. A C-style string is a char array filled with the characters of the string and ending with a null-terminator (a single char whose ASCII code is zero, written '\0'), which marks the end of the string. That means that the char array must be at least the length of the longest string it will be expected to hold plus one more for the null-terminator. A single char will simply not do the job.

    Also, there are certain datatypes for which the compiler will automatically perform a conversion. The actual wording of the error messages is telling you that the compiler cannot convert a char to a char pointer.

    Here is the test program I had inserted your erroneous code into. Note the declaration I added for totalMessage:
    Code:
    #include <strings.h>
    
    int main()
    {
        int  totalScore;
        char totalMessage[20];
        
        /*------------------------------------------------------------*/
        /*                                                            */ 
        /*            Set the IF and ELSE IF                          */ 
        /*                                                            */ 
        /*------------------------------------------------------------*/ 
        if (totalScore >= 1200) 
            (strcpy totalMessage, "Great"); 
        else if (totalScore >= 900) 
            (strcpy totalMessage, "Good"); 
        else if (totalScore >= 600) 
            (strcpy totalMessage, "OK"); 
        else 
            (strcpy totalMessage, "Poor");
    
        return 0;
    }
    I just arbitrarily chose to make it 20 characters long. That means that the longest string it can hold would be 19 characters long. Review about the null-terminator if you don't understand that. It is an extremely important concept with working with C-style strings.

    Gotta go.

    Comments on this post

    • requinix agrees : you need lots more rep
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Thank you very much sir, that is where my issue was. I did not count for the null byte. The compiler is happy and I am learning.

    Thank you very much your time

    snf1694

IMN logo majestic logo threadwatch logo seochat tools logo