Thread: character arrays (basics, stuck on applying the tutorial)

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    13

    character arrays (basics, stuck on applying the tutorial)

    I'm working my way through the C++ tutorials and I'm stuck on character arrays. Basically, I don't understand how a user can input a sentence into the program and then how to store that sentence in a variable, ready to be called in a cout<< line.

    Here is a piece of code I'm trying to work on (to no avail). The problem here isn't identical but an answer to the previous question will probably help answer this one.
    Code:
    #include <iostream> //For cout
    
    using namespace std;
    
    int main()
    {
    int monstype;
    char monsdescription[35];
    cout <<"please choose the monster you wish to fight:\n1. Big hairy ugly thing\n2. Small and easy on the eyes\n3. Medium size, much the same as you!\n";
    cin>> monstype;
    if (monstype == 1){
    monsdescription = "Big hairy ugly thing";
    }
    else if (monstype == 2){
    monsdescription = "Small and easy on the eyes";
    }
    else{
    monsdescription = "Medium size, much the same as you!";
    }
    cout <<"You have chosen "<< monstype <<", "<< monsdescription <<"";
    cin.get();
    }
    Why doesn't
    Code:
    if (monstype == 1){
    monsdescription = "Big hairy ugly thing";
    }
    work? The error line says:
    incompatible types in assignment of 'const char [27]' to 'char [35]'

    or
    Code:
    else{
    monsdescription = "Medium size, much the same as you!";
    }
    with an error line :
    invalid array assignment

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since you are using null terminated character arrays (C-string) you can not use the assignment operator (=) you need to include string.h and use strcpy().


    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char monsdescription[35];
    So include string, and do
    std::string monsdescription;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Thanks for your comments but I'm still stuck...

    I've tried using the strcpy but it's not working (it's the only operator that isn't in the example in the tutorial!). Here's my best attempt so far:

    Code:
    if (monstype == 1){
    char strcpy (char monsdescription,"Big hairy ugly thing");
    }
    Meaning the overall code is now:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    int monstype;
    char monsdescription[35];
    cout <<"please choose the monster you wish to fight:\n1. Big hairy ugly thing\n2. Small and easy on the eyes\n3. Medium size, much the same as you!\n";
    cin>> monstype;
    if (monstype == 1){
    char strcpy (char monsdescription,"Big hairy ugly thing");
    }
    else if (monstype == 2){
    char strcpy (char monsdescription,"Small and easy on the eyes");
    }
    else{
    char strcpy (char monsdescription,"Medium size, much the same as you!");
    }
    cout <<"You have chosen "<< monstype <<", "<< monsdescription <<"";
    cin.get();
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so you need to decide whether you are going to use C styles strings which are character arrays or C++ strings. My suggestion would be to stick with C++ strings for what you are doing. In that case you need to do:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <string>
    
    ...
    int main() {
    
        string monsdescription;
    ...
        if (monstype == 1){
    	monsdescription = "Big hairy ugly thing";
        }
    And then your provided cout statement is fine.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    That was much much simpler and easier to understand, thanks a lot!

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No problem, also don't forget at the end of your main function you need to return(0) back to the OS.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    don't forget at the end of your main function you need to return(0) back to the OS.
    This will happen automatically if control reaches the end of the global main function without encountering a return statement.
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    This will happen automatically if control reaches the end of the global main function without encountering a return statement.
    Happy Canada Day Laser! So, my guess is then per the standard for C++ you do not need to have the statement included?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, though of course you may choose to have it for consistency or style.
    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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I see, thanks for the insight Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on tutorial number 5 (basics): switch cases
    By rwebb2305 in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2011, 06:01 AM
  2. New to C++ Stuck on Arrays. Help please!
    By PersianStyle in forum C++ Programming
    Replies: 5
    Last Post: 07-15-2009, 01:45 AM
  3. Stuck, arrays
    By NoobieGecko in forum C Programming
    Replies: 26
    Last Post: 02-17-2008, 12:14 AM
  4. Stuck again: Arrays
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 12:37 AM
  5. Generating a random character/letter, stuck!
    By DanFraser in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2003, 11:30 AM