Advertisement

A simple programing question...

Started by August 28, 2001 06:40 PM
2 comments, last by GameDweeb 23 years, 5 months ago
Okay, I got this program running almost like I want it to, but I don''t know what''s up with it... I run it put the names in and then it spits out a whole bunch of special characters instead of normal ones... what do I need it to do to get it to put out readable characters rather than special ones? here''s the code: int i = 0; char currentcar[12]; char inputcarname[12]; for (i = 0; i < 4; i++) { cout << "Input car name " << i << " "; cin >> inputcarname; currentcar == inputcarname; } for (i = 0; i < 4; i++) { cout << currentcar << endl; } can anyone tell me what I need to do? </i>
change the line

currentcar </i> == inputcarname; <br><br>to <br><br>currentcar<i> </i> = inputcarname;<br><br>Then it should work.<br><br>In your line you don't give the input you recived to currentcar. Thats why you get garbage when you cout currentcar. It gives what in its memory; random data or garbage <br><br>Humanity's first sin was faith; the first virtue was doubt<br><br><br>Edited by - SkyRat on August 28, 2001 8:00:23 PM
Humanity's first sin was faith; the first virtue was doubt
Advertisement
OK, assuming that what you''re really trying to do is read in four car names, this is what you need to do:

int i=0;

// declare an array four elements long, each element capable of
// holding a string of up to 11 characters (last char is the
// NULL terminator character):
char currentcar[4][12];

// declare an array which is capable of holding a single 11
// character string, which is currently being input by the user:
char inputcarname[12];

// read and store the car names:
for(i=0;i<4;i++) {
// make sure the arrays are emptied first:
memset( &currentcar[0],0,sizeof(currentcar) );<br>memset( &inputcarname[0],0,sizeof(inputcarname) );<br><br>// read the car name:<br>cout << "Input car name " << i << ": ";<br>cin >> inputcarname;<br><br>// NULL-terminate the string:<br>inputcarname[11]=''\0'';<br><br>// copy the name the user input into the correct spot in<br>// the currentcar array.<br>memcpy( &currentcar[0],&inputcarname[0],sizeof(inputcarname));<br>}<br><br>// print out the car names:<br>for(i=0;i<4;i++) {<br>cout << currentcar << endl;<br>}<br><br>Note that if the user inputs more than 12 characters, you will crash. </i>
Hey thanx for the replys you guys!

This topic is closed to new replies.

Advertisement