Why do I get an error here?
(error: excess elements in char array initializer)Code:int move_tortoise[10] = {"1", "1", "1", "1", "1", "2", "2", "3", "3" "3"};
Why do I get an error here?
(error: excess elements in char array initializer)Code:int move_tortoise[10] = {"1", "1", "1", "1", "1", "2", "2", "3", "3" "3"};
"" 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.
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.
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.
And now I want to save the sum of "a" and "b" in "a"Code:int a; int b;
I think
doesn't work...Code:a = a + b;
Last edited by Eizi; 09-12-2011 at 06:48 AM.
Hmm, I think the error is somewhere else, so I just post the whole loop.
Error code: "syntax error before "a""Code:switch(array[i]) { case 1 a = a + 3; case 2 a = a - 6; case 3 a = a + 1; }
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.
You forgot the colons for the cases. Also, if you don't want fall through, remember to use break.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
@laserlight: 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?Originally Posted by Eizi
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
That is not correct and has poor formatting. I might rewrite it as:Originally Posted by CommonTater
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.Code:switch (array[i]) { case 1: a += 3; break; case 2: a -= 6; break; case 3: ++a; break; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 sleepOriginally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
sorry for the late reply
ok thank you for the help
I didn't understand how I should exactly edit my code to fix the errors...Originally Posted by laserlight
Anyway, thank you for your help