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.
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;
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().
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);
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.
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