C++ quick question, array

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

Join Date: Nov 2009
Posts: 2
Reputation: EastJohn is an unknown quantity at this point 
Solved Threads: 0
EastJohn EastJohn is offline Offline
Newbie Poster

C++ quick question, array

 
0
  #1
33 Days Ago
My array keeps outputting this random character (kind of like a # in a box). Anyways, its just garbage really, but how do i stop it from doing this.

If my array was like

char title[10]

and the user inputted something with only 5 characters, then the remaining characters would be the garbage character.

How do I stop this?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 356
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 55
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
32 Days Ago
set some default value to them such as 0
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: abhaymv is an unknown quantity at this point 
Solved Threads: 0
abhaymv abhaymv is offline Offline
Newbie Poster
 
-1
  #3
32 Days Ago
Are you outputting array with a loop?If so, try putting the condition
  1. for(i=0;i!=\0;i++)
  2. cout<<title[i];
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #4
32 Days Ago
Initialize the array by flooding it with all 0s. char title[10] = {0};
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 131
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #5
32 Days Ago
I believe I know why, although I am not positive. Anyway, my guess would be that the [10] is only initializing the amount of characters you can have in the word. Therefore, in order to have it as an array you would need:
  1. char title[10][25]

This works for me anyway..

@ancientdragon, wouldn't he need quotes around the character to initialize it? Not positive if you do or not..
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC