help a n00b learn...

im struggling with trying to understand the differences btwn some of the operatives (i think thats the right word...) like string, long int, short int, char, and all the others. does anyone have some relatively simple but well commented code for programs that could help clear up some of the haze in my mind?
well first of all, some of the ones you mentioned are of completely different types.

string and char are for characters. so you would be able to store the text "hello" in a string or character array.

obviously, int is for whole numbers, float is for decimal numbers (decimal as in ones with a decimal point, not base 10). so 10 would be int, 10.3 would be float. if you try to store 10.3 as an int, it'll just say 10 because int doesn't support float point numbers.

as for long int, short int, etc etc, they cover different ranges. as the name suggests, LONG int obviously covers a larger range of numbers. the draw back to that is that it takes up more memory. so generally, you just declare int, when you need to work with large numbers, you declare long int, etc.
im kinda going thru the tutorial from the site, and im still learning. i appreciate the help.

so...the word "hello" would be 1 string or an array of 5 chars?
and a char can be letter or number?
also, another thing i am completely lost on is this whole bitwise thing...i even looked it up on wikipedia, but its all still greek to me.

i've been looking at some of the other posts in this catagory, and one thing i don't understand is the getline command. i understand it is supposed to pull data valid to a particular data type u are using out of a string, but can anyone explain how it does it?
i'm not qutie sure about bitwise. i never quite learned it myself, even tho i probably should. your understanding of char arrays and strings is correct.

yes, numbers can be stored as char, but as just that. CHARACTERS. for example, if you store 1 and 2 as char, you can't perform addition, subtraction, etc. in that sense. a 5 character array of "hello" is no different from a 5 character array of "12345".

the getline command does exactly what it does. gets the entire line from either the user input or file. usually, if you use cin >> variable, the "cin" command will stop once it sees a SPACE or a NEW LINE (i.e. if the user hits enter).

the getline, however, will not stop there. it will include space as a character, and also get the new line and store it as a character as well. the new line character is "\n" in C++.

so, by example, if i enter the text "Hello World!" and hit enter in both:
1
2
3
4
char text[50];
cin >> text;
cout << text;
//outputs "Hello" only 

but
1
2
3
4
char text[50];
cin.getline(text, 50);
cout << text;
//outputs "Hello World!" 
Operators = Things like +, -, =, /, &, |

Types = short, int, long etc.

For short, int and long the difference is the size each requires in memory and the largest numerical numer they can represent (all bytes are F if unsigned). Signed types can be negative while unsigned cannot be; however unsigned can store larger positive number than signed.

2x short = 1x int
2x int = 1x long

float is a decimal number. double is 2x the size of a float and can hold more. However, floats and decimals are not entirely accurate and you should not compare them with the == operator.
so if u have the user input a sentance as a string constant, could you create a string array and use getline to pull each word in order and place them in the elements of the array?

i.e. cin >> string text; //user inputs "hello, my name is james."
string sentance [];
sentance=getline (text);
cout << text[0]\n text[1]\n text[2]\n text[3]\n text[4]\n

and it would print on the screen:
hello,
my
name
is
james

would that work?
Nope.

getline() will wait until a new line character (enter char) before returning.
1
2
3
4
5
6
7
8
9
string sentence[10];
for (int i = 0; i < 10; ++i) {
 cout << "Enter Word:";
 cin >> sentence[i];
}

for (int i = 0; i < 10; ++i)
 cout << sentence[i] << " ";
cout << end;


Thats more like what you are thinking.
However you can do.

1
2
3
4
5
6
std::ostringstream o;
o << "hello";
o << " ";
o << "world";

cout << o.str() << endl;


Only streams support << and >>

Topic archived. No new replies allowed.