adding data into an char array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

adding data into an char array

 
0
  #1
Dec 29th, 2004
hello, ok

here it goes...

I've got a char array but it doesnt want to let me add data to it!! I cant figure out why

[php]

//skip the program heres the line...

int chararry[12][10] = {Jan, Feb....}

[/php]

but I keep getting an error saying its not been initialized... I've tried google but nothing :eek:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: adding data into an char array

 
0
  #2
Dec 29th, 2004
Originally Posted by Acidburn
hello, ok

here it goes...

I've got a char array but it doesnt want to let me add data to it!! I cant figure out why

[php]

//skip the program heres the line...

int chararry[12][10] = {Jan, Feb....}

[/php]

but I keep getting an error saying its not been initialized... I've tried google but nothing :eek:
What about the ; behind {}

And 'Jan', 'Feb'
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: adding data into an char array

 
0
  #3
Dec 29th, 2004
Well, but {Jan, Feb} is not 2 strings, but 2 something elses. Enums, variables, something. If you wanted an array of strings, you'd do something more like this:

char* chararry[12] = { "Jan", "Feb", <etc> };

or better yet make it a const char* unless you plan on modifying it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: adding data into an char array

 
0
  #4
Dec 29th, 2004
umm! sorry

[php]int chararry[12][10] = {"Jan", "Feb"} [/php]

i get the following errors

F:\University work\Computer Science\C++\New Folder\montharray.cpp(8) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\University work\Computer Science\C++\New Folder\montharray.cpp(8) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast


i sorted it.... But I do hate pointers!!!! Don't understand how to use them
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: adding data into an char array

 
0
  #5
Dec 29th, 2004
Hi Acidburn, change the " to ' Ive tried it out and got the exact error message you got:

  1. C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(14) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
  2. This conversion requires a reinterpret_cast, a C-style cast or function-style cast
  3. C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(14) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
  4. This conversion requires a reinterpret_cast, a C-style cast or function-style cast
  5. Error executing cl.exe.
  6.  
  7. Testing.obj - 2 error(s), 0 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: adding data into an char array

 
0
  #6
Dec 29th, 2004
>int chararry[12][10] = {"Jan", "Feb"}
Your declaring an array of integers but trying to initialize it with strings. Do you see anything wrong with that? If not then maybe a strongly typed language like C++ isn't your cup of tea.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: adding data into an char array

 
0
  #7
Dec 29th, 2004
Originally Posted by Narue
>int chararry[12][10] = {"Jan", "Feb"}
Your declaring an array of integers but trying to initialize it with strings. Do you see anything wrong with that? If not then maybe a strongly typed language like C++ isn't your cup of tea.
yeah i just noticed lol ...damit programming when your tired is not a good way
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: adding data into an char array

 
0
  #8
Dec 31st, 2004
btw a string array would mean you only need a one dimension array:

  1. #include <string> // the string class is in the string library (std name space)
  2. using namespace std;
  3.  
  4. string months[12] = { "January", "February"...... };

as the lengths are initialised by its constructor. The array is then MUCH easier to work with when you only have a single dimension.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC