Char

ultrashawn18

Beta member
Messages
2
Hello I was wondering if there is a way to store a series of letters, such as a name. I at first tried to use "char" however this only stores one charecter. here is just a simple program I was trying to write which I had hoped would store the Candidate's name and initals.

//just a lil program w00t 05 tho

#include<iostream.h>

int main()
{
double can1=25;
double can2=57;
double can3=52;

/* I am definig everything im suppose to in the book */

char cand1='Mike Presely', can1b='MP';
char cand2='Sue Farted', can2b='SF';
char cand3='Barnes Nobles', can3b='BN';

int year;
year = 2000;

cout<<"The current results for the "<< year <<" election is as follows\n\n";


cout<<cand1<<", "<<can1b <<" ..... " <<can1<<"\n"; //this will hopefully display the results for the first candidate of the election
cout<<cand2<<", "<<can2b <<" ..... " <<can2<<"\n";
cout<<cand3<<", "<<can3b <<" ..... " <<can3<<"\n";

cin.get();
return(0);

}

I know the answer may be really simple but I did search the forums and look in Dev-C++ help before asking. Anyway anyhelp is greatly aprecaited.
 
It might be better if your going to have names to actually use an array

Something like this
char names[] = { 'Mike Presely', 'Sue Farted', 'Barnes Nobles' }

To output them you would reference the names by using names[0] for the first name and names[1] for the second and so on...
 
or char *name1='mikey';

then recall it as a string.

printf("name1 = %s", name1);

you could also use an array.

char name[50];
that creates a string that can store 50 chars 0 - 49.
that can be referenced as a string also.
 
Called a string...

i dont think you know what a string is yet.. im just guessing. basicly the problem is your typing char cand1 = whatever when you should be typing cand1[15] = whatever. but if the names more than 15 characters just replace it with a higher number. there, easy explanation instead of :

or char *name1='mikey';

then recall it as a string.

printf("name1 = %s", name1);

you could also use an array.

char name[50];
that creates a string that can store 50 chars 0 - 49.
that can be referenced as a string also.
lol
 
or

#include <string>

string hey = "I'm a little string short and stout";

cout<<hey;

where string is something like a char array but wrapped in a class. its dynamic so it can have any length put it.
 
Re: Called a string...

Darkcoder said:
i dont think you know what a string is yet.. im just guessing. basicly the problem is your typing char cand1 = whatever when you should be typing cand1[15] = whatever. but if the names more than 15 characters just replace it with a higher number. there, easy explanation instead of :

or char *name1='mikey';

then recall it as a string.

printf("name1 = %s", name1);

you could also use an array.

char name[50];
that creates a string that can store 50 chars 0 - 49.
that can be referenced as a string also.
lol

the char *name is a reference to a pointer, and is perfectly acceptable to contain a string. and can be used in the following method
str1 = getval("function");
to accept returns from functions of any length.

the method
char name[] = 'name1';will create a char array with the correct amount of chars, wich takes the point away from the necessity to specify the amount of places in the char array as with the method. however the array is only created the size of the charspushed into it on creation.

char[40] = 'too many spaces is a memory waste';
and can cause leaks when trying to push too many char into a memory space that isn't properly allocated.

In any case, all the outlined methods in this post are in accordance with ANSI C usage and will compile on any platform. ad are perfecty valid ways to use strings.

http://computer.howstuffworks.com/c.htm is a god place to start...
 
Back
Top Bottom