Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-03-2005, 10:47 PM   #1
Beta Member
 
Join Date: Dec 2004
Posts: 2
Default Char

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.
__________________

ultrashawn18 is offline   Reply With Quote
Old 01-05-2005, 04:08 AM   #2
Baseband Member
 
Join Date: Dec 2004
Posts: 53
Default Re: Char

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...
__________________

Eyes0nly is offline   Reply With Quote
Old 01-12-2005, 06:43 PM   #3
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: Char

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.
root is offline   Reply With Quote
Old 02-07-2005, 10:07 AM   #4
Beta Member
 
Join Date: Feb 2005
Posts: 4
Talking 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
Darkcoder is offline   Reply With Quote
Old 02-07-2005, 12:54 PM   #5
Daemon Poster
 
Tulsileaf's Avatar
 
Join Date: Jul 2004
Posts: 1,249
Default Re: Char

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.
Tulsileaf is offline   Reply With Quote
Old 02-07-2005, 03:11 PM   #6
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: Called a string...

Quote:
Originally Posted by Darkcoder
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...
root is offline   Reply With Quote
Old 02-07-2005, 09:48 PM   #7
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Char

i always write
using namespace std; right after i include
also you should #include <cstdlib> and use
system ("pause"); inplace of cin.get();

http://www.computerforums.org/showthread.php?t=6279

read post 8 down.
__________________

__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:27 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0