did i define the string true need help

May 3, 2011 at 9:38pm
char carModels[6]={"ford focus""honda civic""citroen couple""peugeot 208""mazda RX6""golf GTI"};

how i can define that string ?
May 3, 2011 at 9:45pm
If it's a string, use a string, not a char:

 
string carModels[6] = {"ford focus","honda civic","etc"};
May 3, 2011 at 10:27pm
closed account (zb0S216C)
Disch is right. Use string instead. string objects provide more functionality over standard strings.

Your code however, defines an array of 6 characters, not 6 strings. If you insist on using standard strings, you might consider using this:
 
char *CarModels[ 6 ] = { "Ford Focus\0", "Honda Civic\0", ... };
Last edited on May 3, 2011 at 10:27pm
May 4, 2011 at 8:36pm
i did as you wrote but its still doesn't work

error char unexpected
May 4, 2011 at 8:40pm
@ Framework:

- string literals are const char*s, not char*s.
- Also, no need to put the explicit null at the end of those strings. The "quotes" do that automatically.

i did as you wrote but its still doesn't work


What I wrote will compile just fine. Show us the code that's giving you the error so we can spot what's wrong.
May 4, 2011 at 9:41pm
disch i have put all program on forum i can find the error :S
May 4, 2011 at 9:47pm
I saw you made a new thread about it.

Since we're already talking about it here...

1
2
char*carModels[6]={"ford focus""honda civic""citroen couple""peugeot 208""mazda RX6""golf GTI"};
char*carEngines[6]={"17cc""19cc""16cc""14cc""18cc""16cc"};


This is wrong.

1) char* is no good. You either need const char* or you need string.
2) You need commas between your items.


This would be correct:

1
2
3
const char* carModels[6]={"ford focus", "honda civic", "citroen couple", "peugeot 208", "mazda RX6", "golf GTI"};

//make similar corrections to carEngines 

May 4, 2011 at 10:18pm
closed account (zb0S216C)
i did as you wrote but its still doesn't work

Strange. It works with VC++ Express 2010. Although I do admit to forgetting the const before char *. Mistakes happen.
May 5, 2011 at 8:25pm
my friend thank you for tring help but its still doesnt work this is a project of mine i have to do it
otherwise i will get A BIG F
May 5, 2011 at 8:32pm
What I posted actually does work. You just must be typing it wrong or something.


Don't just tell us it doesn't work. Show us what you're doing. Tell us what error you're getting. We can't help you when we don't know what's wrong.

read this: http://cplusplus.com/forum/articles/40071/#msg218019
Last edited on May 5, 2011 at 8:32pm
May 6, 2011 at 6:12pm
ok the problem is those strings char*carModels[6]={"ford focus","honda civic","citroen couple","peugeot 208","mazda RX6","golf GTI"};
char*carEngines[6]={"17cc","19cc","16cc","14cc","18cc","16cc"}; \



when i press built button it show errors and say char unexpected Error 2 error C2143: syntax error : missing ';' before '{' c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 8 car galary
Error 4 error C2065: 'carModels' : undeclared identifier c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 18 car galary



things like 6 errors i did it many times as you said but still doesnt work :(((
May 6, 2011 at 6:14pm
Replace char* with const char*.
May 6, 2011 at 6:26pm
After replacing char with const char as LB said, it compiles just fine here.

Can you paste the 3 or so lines before and after the lines that are giving you the error?
May 6, 2011 at 8:24pm
Error 1 error C2062: type 'char' unexpected c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 8 car galary

Error 2 error C2143: syntax error : missing ';' before '{' c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 8 car galary

Error 3 error C2143: syntax error : missing ';' before '}' c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 8 car galary

Error 4 error C2065: 'carModels' : undeclared identifier c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 18 car galary

Error 5 error C2065: 'carModels' : undeclared identifier c:\documents and settings\gul\desktop\car galary\car galary\cars.cpp 23 car galary

so on errors as error 4 and 5 untıl errors 8 same error


AS YOU SAİD I WROTE CONST BEFORE CHAR BUT STILL DOESNT WORK AND GİVE SAME ERRORS
May 6, 2011 at 8:39pm
Could you post all of your code, please? You're probably doing something like:
1
2
3
4
5
6
7
8
9
10
void foo()
{
        const char* carModels[6]={"ford focus","honda civic","citroen couple","peugeot 208","mazda RX6","golf GTI"};
        const char* carEngines[6]={"17cc","19cc","16cc","14cc","18cc","16cc"};
}

void bar()
{
        do_something_with(carModels); /* Error: carModels undeclared */
}

which doesn't work.
May 7, 2011 at 1:28am
From the code in the other thread:
1
2
3
4
5
int main()
{
int select,select1,installment;
int total,total1, //<--- there is a ',' it should be a ';'
char*carModels[6]={"ford focus""honda civic""citroen couple""peugeot 208""mazda RX6""golf GTI"};
May 7, 2011 at 8:25am
http://www.cplusplus.com/forum/beginner/42313/


here my all program all errors in chars strings ı used const char as you wrote but still errors same
May 8, 2011 at 9:03pm
thank you ı solved the errors :) ı may want again for other programs keep in touch
Topic archived. No new replies allowed.