Can somebody help...

J

John

I am new to c++ programming and i just want to make a program that
takes a users name, then converts it to a hex string, then puts it
together in a fixed order.
An example output would be:

Name: John
Output: 84721305

this is my generation code so far
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main () {

char name[254],seri[8],ser[]="";
int name_len,a,x,temp;
int TmpVal=0x12345678;


printf("Name: ");
gets(name);
name_len=strlen(name);
for(a=0;a<=name_len;a++) {
temp=name[a];
TmpVal=TmpVal+temp;

}


printf("%X \n",TmpVal);
itoa(TmpVal,ser,16);

printf("Output: %s",ser);
printf("Output2: ",ser2);
printf("\nPRESS ANY KEY TO CLOSE");

getche();
return 0;
}

I can't figure out how to sort these numbers in a specific order. I
would like it to be characters 5,3,7,1,0,2,6,4 in that order. If
anyone can help, or give a general explanation on how i would do that,
i would appreciate that very much. Thank You
 
O

osmium

John said:
I am new to c++ programming and i just want to make a program that
takes a users name, then converts it to a hex string, then puts it
together in a fixed order.
An example output would be:

Name: John
Output: 84721305

this is my generation code so far
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main () {

char name[254],seri[8],ser[]="";
int name_len,a,x,temp;
int TmpVal=0x12345678;


printf("Name: ");
gets(name);
name_len=strlen(name);
for(a=0;a<=name_len;a++) {
temp=name[a];
TmpVal=TmpVal+temp;

}


printf("%X \n",TmpVal);
itoa(TmpVal,ser,16);

printf("Output: %s",ser);
printf("Output2: ",ser2);
printf("\nPRESS ANY KEY TO CLOSE");

getche();
return 0;
}

I can't figure out how to sort these numbers in a specific order. I
would like it to be characters 5,3,7,1,0,2,6,4 in that order. If
anyone can help, or give a general explanation on how i would do that,
i would appreciate that very much.

I don't understand your example. 'J' would be 4A in hex, right? But there
is no 'A' in the output. Where did it go? You specify a unique collating
sequence that seems to be tied to octal (eight entries) rather than 16
entries. Is it only supposed to work with certain selected (input) letters?
Whatever you are up to, it seems to be a one-way deal, the two halves of the
pair needed to represent a character have been separated, irreversibly, from
each other.

The first thing that comes to mind WRT your specific question is a look up
table. There are two "built in" sort mechanisms in C++. qsort and another
sort in the STL. You might experiment with just doing a simple,
alphabetical sort to start with. Then build your unique collating sequence
into the compare function you must provide.
 
K

Kevin Goodsell

John said:
I am new to c++ programming

Are you sure that's what you are doing? It looks like C code to me, with
a number of unsafe constructs and non-standard extensions. Where did you
learn this? You might want to consider finding a new source to learn from.
and i just want to make a program that
takes a users name, then converts it to a hex string, then puts it
together in a fixed order.
An example output would be:

Name: John
Output: 84721305

I don't understand how you are mapping your input to your output.
this is my generation code so far
#include <conio.h>

This is not a standard header.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

These three are deprecated in C++, though they are common in C.
int main () {

char name[254],seri[8],ser[]="";

In C++ you should use std::string rather than char arrays to represent
strings.
int name_len,a,x,temp;
int TmpVal=0x12345678;

That value is too large to portably store in an int. int is only
required to store values in the range [-32767, 32767]. Better use a long
instead.
printf("Name: ");

stdout may be line-buffered (and often is). That means that output will
not appear until a complete line is available, unless you do something
special to force it. If you want your prompt to be seen when you expect
it to be, you'd better either add a new-line:

printf("Name: \n");

or flush stdout:

fflush(stdout);

of course, in C++ we usually use std::cout:

std::cout << "Name: " << std::flush;
gets(name);

NEVER USE gets!! This is a horrible, evil function that is, for all
intents and purposes, impossible to use safely! The only use for gets()
is to insert security holes into your program. Read up on buffer
overflow attacks and the Morris worm.

(For example:
http://en.wikipedia.org/wiki/Buffer_overflow
http://en2.wikipedia.org/wiki/Morris_worm
)


In C, it's usually recommended to use fgets() instead. In C++ we usually
use std::getline().
name_len=strlen(name);

If this is how you are using it, maybe 'name_len' should be a size_t
instead of an int.
for(a=0;a<=name_len;a++) {

You should probably make 'a' a size_t also.

Are you sure you want this loop to execute when a == name_len? In that
case, name[a] will be '\0', which has the value 0 and will apparently
have no effect. So it's probably harmless in this case, but in many
cases a similar mistake could cause serious problems. The usual idiom is

for (index=0; index<length; ++index)
temp=name[a];
TmpVal=TmpVal+temp;

This is usually written as

TmpVal += temp;

However, if these are signed types (such as 'int'), this might overflow
and give undefined behavior. unsigned types (like size_t) have
well-defined semantics for when the result can't be represented in the
destination type, and will probably do what you want.
}


printf("%X \n",TmpVal);

This results in undefined behavior because you lied to printf about what
type you passed it. %X is *only* for unsigned int, but you passed a
(signed) int. Keep in mind that printf - and scanf, and other similar
functions - are stupid. They can't determine the type you passed - they
rely completely on you telling them correctly what the type is. That's
one reason that C++ programmers prefer the type-safe stream classes. If
you must use printf & friends, always double-check your format strings.

If you follow my other advice and make TmpVal a size_t (or even if you
don't), you can call printf this way:

printf("%lX \n", (unsigned long)TmpVal);
itoa(TmpVal,ser,16);

This is not a standard function. Since it's non-standard I have no way
of knowing exactly what it's supposed to do, but I can make an educated
guess. And if that guess is correct, this is a serious error. 'ser' is
an array of ONE character. There is NO space for anything else. If you
try to write anything more to it you will invoke undefined behavior
(possibly trashing other values in memory resulting in strange and
unpredictable results, possibly crashing the program, possibly allowing
a malicious user to compromise system security).
printf("Output: %s",ser);

Since 'ser' can never be a string with more than 0 characters, you can
replace this with

printf("Output: ");

and get the same result. If this ever gives a different result, it means
you've caused undefined behavior.
printf("Output2: ",ser2);

What is ser2? It does not seem to be declared anywhere. Also, while this
is harmless, you have passed more parameters to printf than it is
expecting. It's expecting 0 additional parameters after the first
(because you haven't given any conversion specifiers) and you've passed 1.
printf("\nPRESS ANY KEY TO CLOSE");

A portable program must terminate its output with a newline. It is
implementation-defined whether this is required, but it is required on
some systems and is always safe.
getche();

This is not a standard function.
return 0;
}

I can't figure out how to sort these numbers in a specific order. I
would like it to be characters 5,3,7,1,0,2,6,4 in that order. If
anyone can help, or give a general explanation on how i would do that,
i would appreciate that very much. Thank You

I have no idea what you mean here. In C++ sorting is usually done with
std::sort(), which allows you to provide a comparison criteria, so it
should work for nearly all of your sorting needs. You could also use
qsort() (which is also what one would generally use in C).

In the future, please post C code and C questions in comp.lang.c, and
use this group only for C++ issues. Regardless of which group you post
in, please

1) Try to explain your problem clearly.
2) Post a complete, minimal, compilable (or as close as you can get it)
example.
3) Leave out non-standard things.

-Kevin
 
T

Thomas Matthews

John said:
I am new to c++ programming and i just want to make a program that
takes a users name, then converts it to a hex string, then puts it
together in a fixed order.
An example output would be:

Name: John
Output: 84721305

If I understand your requirements, your example output is wrong.
Name: John

Let us break it down into the hex codes for ASCII:
'J' == 0x4A
'o' == 0x6F
'h' == 0x68
'n' == 0x6E

Now to sort out the values:
0x4A, 0x68, 0x6E, 0x6F
{without spaces & prefixes: 4A686E6F)

In C++, characters can be treated as numbers. So all you would
have to do is sort the list of letters, then print each one
using hex notation.

By the way, the ASCII coding sequence is 0 to 0x7F. So if we
just use the lower 7 bits of each octet in your output, your
output translates to:
84721305 == <EOT> r <CR> <0x05> {I don't remember what 05 is}.
In other words, none of those mappings match your output.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top