char c=97;
Have you tried that? It will work.
97 and 'a' are the same thing... It's just how you look at it.
Ascii character conversion
...
* head explodes *
A char is just a number. It holds the ascii value for whatever character you have. The compiler doesn't care if you use 'a' or 97. They're exactly the same thing. You can use either one. It's really just a convenient shortcut so that you don't need to remember the codes for all the characters, just like you can write "Hi!" instead of {72, 105, 33, 0}. It's makes no difference to the compiler, they're exactly the same.
* head explodes *
A char is just a number. It holds the ascii value for whatever character you have. The compiler doesn't care if you use 'a' or 97. They're exactly the same thing. You can use either one. It's really just a convenient shortcut so that you don't need to remember the codes for all the characters, just like you can write "Hi!" instead of {72, 105, 33, 0}. It's makes no difference to the compiler, they're exactly the same.
Just wanted to mention that 'a' isn't necessarily equal to 97.
A friend of mine has to deal with IBM mainframes on a regular basis..
I'd better add that characters constants are always integers, wouldn't want to confuse things further ;)
A friend of mine has to deal with IBM mainframes on a regular basis..
I'd better add that characters constants are always integers, wouldn't want to confuse things further ;)
Hmm, now I'm confused. I have this code:
The first time I run it, it works fine. However, if I run it again without deleting "text.txt" first, "text.txt" becomes filled with asian symbols.
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h>
int main()
{
srand(time(NULL));
char test[100];
char buff;
int x = 0;
cout << "Enter text to encrypt: ";
cin >> test;
ofstream text ("text.txt");
while (x < 100)
{
text << test[x];
buff = (97 + rand(26));
text << buff;
buff = (97 + rand(26));
text << buff;
x++;
}
system("PAUSE");
text.close();
return 0;
}
The first time I run it, it works fine. However, if I run it again without deleting "text.txt" first, "text.txt" becomes filled with asian symbols.
char test[100];
This defines an array of chars. It doesn't set them to anything. It will be whatever values are in the computers memory at that point. It could be garbage it could be zeros. Whatever.
cin >> test;
This will copy a character string into test. However long the string is, if it less than 99 characters then the garbage values mentioned above will still be garbage.
You could initialise char test[100] by doing the following:
char test[100] = {'J'}
This will fill all the characters in the array with J (for Jared), so you can see it's uninitiated.
This defines an array of chars. It doesn't set them to anything. It will be whatever values are in the computers memory at that point. It could be garbage it could be zeros. Whatever.
cin >> test;
This will copy a character string into test. However long the string is, if it less than 99 characters then the garbage values mentioned above will still be garbage.
You could initialise char test[100] by doing the following:
char test[100] = {'J'}
This will fill all the characters in the array with J (for Jared), so you can see it's uninitiated.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement