|
Thread: C++
-
July 21st, 2002, 09:55 PM
#21
c++
Not quite... but close. the userName[21] creates a username with up to 21 characters (which are located in positions 0-20 of the character array) Declaring a variable as just a char only allows a character, but the [] make the variable into an array of characters instead. Also, if you want, you can include string.h and use the string variable type instead. This header file will also give you more string manipulation functions/procedures.
so you mean if my program looks like this it wil work:
#include <iostream>
using namespace std;
int main void
{
char userName[100]//this allows the user to type a 100 character name
cout << "insert your name" << end;
cin >> userName
cout << "hello" <<userName<< "you are very cool"
}
Support your right to arm bears.

^^This was the first video game which i played on an old win3.1 box
-
July 21st, 2002, 09:59 PM
#22
Banned
yes but the part thats after the // is a one line comment and dosnt need to be there really so you dont need to update it to be 100 characters......
-
July 21st, 2002, 10:16 PM
#23
Junior Member
-
July 21st, 2002, 10:30 PM
#24
Banned
ok this string that youve declared........ how long can it be...... is it unlimeted length like im hoping? and it can take any input type? if these are right i think im in love with them then
-
July 21st, 2002, 10:59 PM
#25
Junior Member
Probably no limits (on a 32-bit compiler) up to around a couple of gigs, might hit problems with more than a few tens of megabytes due to memory fragmentation - after about a megabyte you're better off with a rope class (basically they split a string into lots of little ones that the memory system can handle).
What do you mean by "take any input type"?
Byte Me 
-
July 21st, 2002, 11:13 PM
#26
and it can take any input type?
I assume you're refferring to whether it can be a different variable type (such as an integer, etc.). The answer is no. Strings will be string's of characters. Granted, you can enter numbers, but they will be characters of the numbers, not the integer values. You will have to change it to an integer later if you need to do integer manipulations.
If you wish to have them of any type, you can create your own class which will allow you to declare an array dynamically or a linked-list which will allow you to create an array of any length of any type.
AJ
-
July 21st, 2002, 11:59 PM
#27
Re: c++
Originally posted here by White_Eskimo
#include <iostream>
using namespace std;
int main void
{
char userName[100]//this allows the user to type a 100 character name
cout << "insert your name" << end;
cin >> userName
cout << "hello" << userName << "you are very cool"
}
that will work except for the lack of semi-colons, and you would want to use a different input method. it should look like this:
#include <iostream>
using namespace std;
#define MAXNAMELENGTH 100
int main void
{
char userName[MAXNAMELENGTH];//this allows the user to type a 99 character name --1 char is needed for the terminating NULL char.
cout << "insert your name" << endl;//endl, not end
cin.getline(userName, MAXNAMELENGTH); //you wont want return chars in a name, and this will stop a buffer overflow because it will only read upto 100 chars.
userName[MAXNAMELENGTH-1] = NULL; //make sure there is a nullchar at the end.
cout << "hello" <<userName<< "you are very cool";
}
Originally posted here by LoggOff
yes but the part thats after the // is a one line comment and dosnt need to be there really so you dont need to update it to be 100 characters......
well comments are almost always a good thing. saying they are un-needed just starts people off not using them and gives headaches to anybody who winds up having to read and modify their code.
- 8-
There are 10 types of people in this world: those who understand binary, and those who dont.
-
July 22nd, 2002, 01:04 AM
#28
Banned
/me falls in love with string...... if only i could figure out how to use them......?
-
July 22nd, 2002, 01:27 AM
#29
Check out the AO FAQ for a bunch of links to help you use C++ and strings.
AJ
-
July 22nd, 2002, 10:15 AM
#30
Banned
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|