Thread: Little problem with array

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

    Little problem with array

    Why do I get an error here?

    Code:
    int move_tortoise[10] = {"1", "1", "1", "1", "1", "2", "2", "3", "3" "3"};
    (error: excess elements in char array initializer)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "" indicates a string. You are trying to put in integers, so ditch the quotes.


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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because "1" is a character string (which is an array of two characters '1' and '\0' that are each integral values) not an integer. Lose the double quotes.

    The missing comma near the end won't help, as the two strings "3" "3" will be concatenated to "33" which is an array of three characters.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    Ok thank you =)
    So which type do I use for an array with strings in it?

    The missing comma was a mistake, but nevertheless, it's good to know that

    Also, would you please tell me how I save the sum of two variables in one of these.

    Code:
    int a;
    int b;
    And now I want to save the sum of "a" and "b" in "a"
    I think
    Code:
    a = a + b;
    doesn't work...
    Last edited by Eizi; 09-12-2011 at 06:48 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    Hmm, I think the error is somewhere else, so I just post the whole loop.

    Code:
    switch(array[i])
            {
                case 1
                a = a + 3;
    
                case 2
                a = a - 6;
    
                case 3
                a = a + 1;
            }
    Error code: "syntax error before "a""

    This code should (as far as I know) add 3 to the variable "a" if array[i] == 1, substract 6 if array[i] == 2 and add 1 if array[i] == 3.
    But I have a syntax error (as I already wrote) and I have no idea how to fix it...
    Last edited by Eizi; 09-12-2011 at 08:15 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot the colons for the cases. Also, if you don't want fall through, remember to use break.
    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

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    I am also just learning these things. You probably want to replace the double quotes with single quote and remove the 10 just have empty square brackets. But don't listen to me, need to check that I am telling you the right thing.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    @laserlight: I'm sorry, I think I do not completely understand what you mean..

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eizi
    I'm sorry, I think I do not completely understand what you mean..
    Ah, so you partially understand what I mean. What do you understand and which part do you not understand?
    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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eizi View Post
    Ok thank you =)
    So which type do I use for an array with strings in it?
    For ANSI or ASCII text you use char, for Unicode you use wchar_t;

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eizi View Post
    Hmm, I think the error is somewhere else, so I just post the whole loop.

    Code:
    switch(array[i])
            {
                case 1
                a = a + 3;
    
                case 2
                a = a - 6;
    
                case 3
                a = a + 1;
            }
    Error code: "syntax error before "a""

    This code should (as far as I know) add 3 to the variable "a" if array[i] == 1, substract 6 if array[i] == 2 and add 1 if array[i] == 3.
    But I have a syntax error (as I already wrote) and I have no idea how to fix it...
    Code:
    switch(array[i])
            {
                case 1 :
                a = a + 3;
                 break;
                case 2 :
                a = a - 6;
                break;  
                case 3
                a = a + 1;
                break;
            }

    These things should be covered in your C textbooks and/or C Library documentation that matches your compiler. (the latter being a "must have" if you're even slightly serious about programming.)
    Last edited by CommonTater; 09-12-2011 at 09:29 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Code:
    switch(array[i])
            {
                case 1 :
                a = a + 3;
                 break;
                case 2 :
                a = a - 6;
                break;
                case 3
                a = a + 1;
                break;
            }
    That is not correct and has poor formatting. I might rewrite it as:
    Code:
    switch (array[i])
    {
    case 1:
        a += 3;
        break;
    case 2:
        a -= 6;
        break;
    case 3:
        ++a;
        break;
    }
    Anyway, Eizi, you can ignore my post #9 now since this was what I was hoping you would get on your own from my post #6. Of course, whether it is really what you set out to do is another matter as you have provided very little context.
    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

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    That is not correct and has poor formatting. I might rewrite it as:
    Code:
    switch (array[i])
    {
    case 1:
        a += 3;
        break;
    case 2:
        a -= 6;
        break;
    case 3:
        ++a;
        break;
    }
    Anyway, Eizi, you can ignore my post #9 now since this was what I was hoping you would get on your own from my post #6. Of course, whether it is really what you set out to do is another matter as you have provided very little context.
    Now THAT is beyond hypercritical... Really Lase... I'm thinking you need to get more sleep.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Now THAT is beyond hypercritical... Really Lase... I'm thinking you need to get more sleep.
    If you don't want feedback on your code, don't post code. At the moment, looking at your sample code, you clearly need to get more sleep
    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

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    sorry for the late reply

    ok thank you for the help

    Quote Originally Posted by laserlight
    Ah, so you partially understand what I mean. What do you understand and which part do you not understand?
    I didn't understand how I should exactly edit my code to fix the errors...

    Anyway, thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. problem with array i think
    By taurus in forum C Programming
    Replies: 5
    Last Post: 09-21-2009, 02:44 AM
  4. array problem.
    By pessi in forum C Programming
    Replies: 6
    Last Post: 08-21-2007, 07:00 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM