Noob Questions!

Started by
25 comments, last by CarlosNYM 16 years, 9 months ago
Hey! In my previous topic I explained my current situation of noobness and since today I restarted my training, I am going to have a lot of questions. In this topic I will post a lot of questions when I am confused about something(which will prolly be a lot)so if you don't mind helping me please check back here once in a while. Ok, so I started today with arrays and I'm confused. I wrote this code to get the desired output on the screen as 1. ________________________ #include<iostream> using namespace std; int main() { char array[3]; array[1]=1; cout << array[1]; system("pause"); } ________________________ And what i get some weird looking smiley face character.... How do I assign a value to a particular element in array and then display it
Advertisement
char is character, int is integer.

you are assigning the integer 1 to the first character slot, which is not the same value as the character 1. To make it the character:

//note the single quotesarray[1] = '1';


In computers all things have a value as a binary number. It just so happens that the binary value for the integer 1 is the same as the binary value for the smiley face character in your computer's font library.

-me
Also, your code implies to me that you think array[1] is the first element in the array. It's not. It's the second. When you index an array, you index it using zero-based indexing (that is, the first element is at index 0). In general, an array of size N is indexed by values from 0 to (N - 1).
I changed what you said but now im getting 49....

also i do know that array[1] is the second element, i just picked it randomly
Quote:
I changed what you said but now im getting 49....

What exactly did you change? You should get 49 if you changed the type of array from "char array[3]" to "int array[3]" (becayse 49 is the ASCII code for the character '1', e.g., 49 == '1') -- you don't need to do that. Just put the 1 in single quotes, leave the array an array of chars. Otherwise std::cout will use the operator<< overload for printing ints, which does not turn the value into the ASCII character and instead just prints the number.

Quote:
also i do know that array[1] is the second element, i just picked it randomly

Cool, just making sure.
Sorry for double posting but if you take away the single quotes it works
-------------------------
#include<iostream>
using namespace std;

int main()
{
int array[3];
array[1]=1;

cout << array[1];
system("pause");
}
-------------------------

Im not sure why the single quotes give you 49 but...
That's because you changed the type of the array to an int array. If the type was still a char array, you'd need to use '1' to get the desired output.

The way you have it now works, of course, but you will have unexpected results if you put, say, 'a' into the array. It would print 97.

Quote:
Im not sure why the single quotes give you 49 but...

http://asciitable.com/

ints are integers -- numbers. chars are actually integers as well, they're just smaller ones (int can represent a larger range of values than char). By convention, char is also used to represent characters. The ASCII mapping between numerical values and character glyphs is used to perform this representation. The ASCII code for the character '1' is 49; similarly the ASCII code for 'a' is 97. The variable "char x = 'a';" actually holds the binary representation of 97, and it's only the underlying implementation that turns that number into the onscreen letter you are expected -- but only when that number is in a variable of type char. That's why, if you change the variable type to int and print it, you get a number -- cout's version of the << operator for ints just pukes the number onto the screen. But if the variable is a char, you get the appropriate letter -- cout's version of the << operator determines that letter the number represents and puts that on the screen instead(*).

(*) This is actually a gross oversimplification, so don't use it to make any performance judgements, or anything, this is just for illustration.
Are you sure you used the single quotes? I compiled your example with single quotes and it worked. Try:

#include<iostream>using namespace std;int main(){char array[3];array[0]='1';cout << array[0];system("pause");}
Game Development Tutorials, a few good game development tutorials located on my site.
Oh ok!

So I would use char array[5] if i wanted to record, say a 6 letter name?

and int array[5] when i would record 6 numbers.
Quote:Original post by CarlosNYM
Oh ok!

So I would use char array[5] if i wanted to record, say a 6 letter name?

and int array[5] when i would record 6 numbers.


yes, but both those arrays only store 5 values, not 6.

//declares an array that can hold 5 integersint array[5];//accesses the last (5th) element of the arrayarray[4];


-me

This topic is closed to new replies.

Advertisement