changing words to ASCII

B

b.stewart

I have only been using C++ for a few afternoons and i still get
confused at nearly every line i type so maybe someone here can help me.
Im trying to write a small program that can change a word/name into
ASCII decimal for each character you type on the ...'black screen?'
(dont know what to call it but i mean the screen that appears when you
press F5). I found a file (i think somewhere on this site?) that would
change a string of characters into binary using ASCII so i thought i
could modify it to only change the characters into ASCII.
After what is typed below i have no idea what to put next, is the whole
idea of using this char to binary file wrong or is there a way to
modify it?
any help would be greatly appreciated!

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

char *entry, letter;
int ascii, len, total;
void prog();

int main()
{
entry = new char[50];

cout<<"Enter string to convert into ASCII (up to 50 chars): ";
cin.getline(entry, 50);
len = strlen(entry);
for(int i = 0; i<len; i++)

total = 0;
letter = entry;
ascii = letter;
}

???
 
M

mlimber

I have only been using C++ for a few afternoons and i still get
confused at nearly every line i type so maybe someone here can help me.
Im trying to write a small program that can change a word/name into
ASCII decimal for each character you type on the ...'black screen?'
(dont know what to call it but i mean the screen that appears when you
press F5). I found a file (i think somewhere on this site?) that would
change a string of characters into binary using ASCII so i thought i
could modify it to only change the characters into ASCII.
After what is typed below i have no idea what to put next, is the whole
idea of using this char to binary file wrong or is there a way to
modify it?
any help would be greatly appreciated!

#include <iostream>
using namespace std;

You should put this line after all your standard headers.
#include <cstring>
#include <cstdlib>

char *entry, letter;
int ascii, len, total;
void prog();

<Gasp!> Global variables! Don't declare variables until you need them,
and when you do declare them, make sure they have as small of scope as
possible. It makes larger programs much easier to read and debug.
int main()
{
entry = new char[50];

Why not just use an array:

char entry[ 50 ];

Or better, use std::string (you'll have to include <string> above):

string entry;
cout<<"Enter string to convert into ASCII (up to 50 chars): ";
cin.getline(entry, 50);

If you use std::string, you do this:

getline( cin, entry );
len = strlen(entry);
for(int i = 0; i<len; i++)

Your indentation makes me think you wanted to put an opening brace here
so that all of the indented lines are repeated each time through the
for loop:

{
total = 0;
letter = entry;
ascii = letter;


This won't work for several reasons. First you need a closing brace on
the for loop. Second, total is initialized to zero each time the loop
runs, so it doesn't accumulate anything. Of course, since it is never
used, that doesn't matter, but I'm guessing you wanted to use it for
something.

The statement "letter = entry" could be omitted in favor of "ascii =
entry;". There's no need to create extra variables. (Of course,
without the braces on the for loop, the compiler won't know what your
index i is.)

Finally, you don't actually do anything but copy values around in
memory. Presumably you want to print the ASCII values out to the
screen.

Your program would be better written:

#include <iostream>
#include <string>
using namespace std;

int main()
{
cout << "Enter a string: " << flush;
string s;
getling( cin, s );
for( int n=0; n < s.size(); ++n )
{
cout << int( s[n] ) << endl;
}
return 0;
}

Note that the braces on the for loop aren't necessary in my program
since the loop body is only one line, but it's not bad practice to
always use them. I also avoided creating any extra variables by using a
function-style cast to int.

Anyway, I'd suggest picking up a good book on C++. _Accelerated C++_ by
Koenig and Moo is a good one to start with.

Cheers! --M
 
M

Mike Wahler

I have only been using C++ for a few afternoons and i still get
confused at nearly every line i type so maybe someone here can help me.

The best help with that I can offer is to strongly recommend
you obtain some textbooks. See www.accu.org for peer reviews.
Your code below indicates you're in dire need of some books.
Im trying to write a small program that can change a word/name into
ASCII decimal for each character you type on the

I can show you a program which will display the decimal value
of a character object, but note that the C++ language makes no
requirement that the character set used be ASCII or any other
particular encoding. It only requires that certain character
glyphs be included in the character set.
...'black screen?'
(dont know what to call it

Call it 'output device' or 'standard output'.
but i mean the screen that appears when you
press F5).

C++ knows nothing of 'F5' or keys or keyboards, It's a feature
of your compiler or development environment.
I found a file (i think somewhere on this site?) that would
change a string of characters into binary using ASCII
so i thought i
could modify it to only change the characters into ASCII.
After what is typed below i have no idea what to put next,

After reading what you typed below, I have an idea: throw
it away and start over, after doing much reading.
is the whole
idea of using this char to binary file wrong


It's often a good idea to implement something by using
an already existing implementation and modifying it, if
the existing program is close enough -- but you need to
understand it first. I'm not convinced you do.
or is there a way to
modify it?

I recommend you use this way: delete it and start from scratch
-- for two reasons: doing it yourself will help you learn, and
this exercise is so trivial, there's no need to try to modify
a program which does something else.
any help would be greatly appreciated!

See below.
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

char *entry, letter;
int ascii, len, total;
void prog();

int main()
{
entry = new char[50];

cout<<"Enter string to convert into ASCII (up to 50 chars): ";
cin.getline(entry, 50);
len = strlen(entry);
for(int i = 0; i<len; i++)

total = 0;
letter = entry;
ascii = letter;
}


Your code above won't even compile.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::string s;

std::cout << "Enter string: ";
std::getline(std::cin, s);

std::copy(s.begin(), s.end(),
std::eek:stream_iterator<int>(std::cout, " "));

std::cout.put('\n');

return 0;
}

-Mike
 
C

ctrl4ltdeleteme

What is the benifit to not declaring
using std::cout;
using std::endl;
at the beginning?
 
M

mlimber

ctrl4ltdeleteme said:
What is the benifit to not declaring
using std::cout;
using std::endl;
at the beginning?

None in the short program given, though it may prevent name collisions
in a larger program. Or Mr. Wahler may just have been taking his
fingers out for a jog.

Cheers! --M
 
M

Mike Wahler

ctrl4ltdeleteme said:
What is the benifit to not declaring
using std::cout;
using std::endl;
at the beginning?

No particular benefit or detriment. It's simply
my preferred style is to explictly qualify standard
library names.

I do feel that the general 'rule of thumb' about
namespaces bears repeating: "NEVER put a
'using namespace' statement in a header".


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top