473,226 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Can somebody help...

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
Jul 22 '05 #1
3 1690
John writes:
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.
Jul 22 '05 #2
John wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3
John wrote:
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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: R. Tarazi | last post by:
Hello together, I'm having extreme difficulties using RegExps for a specific problem and would really appreciate any help and hope somebody will read through my "long" posting... 1. <?php...
3
by: Patrick | last post by:
Hi, I managed to open a msaccess database with python. import win32com.client.dynamic db1 = win32com.client.dynamic.Dispatch("Access.Application") dbname =...
6
by: lostinspace | last post by:
After four+ years of using FrontPage in a limited capacity and having created over 600 pages on my sites, I've finally (at least for the most part) abandoned FP, to begin the process of converting...
0
by: Pola | last post by:
Please Help I am using VC++ in win 2000 In my appl (Win32 project) I want to control the close operation of the apl (for example if somebody will try to close appl from the "Windows Task Manager")...
0
by: Jai | last post by:
Hi, Somebody please tell me how to bind(two way) a checkboxlist with objectdatasource if the checkboxlist is inside a formview..... Code of FormView is like this::--- <asp:FormView...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
14
by: cool.vimalsmail | last post by:
i dont know how to convert a txt file into a zip file (i.e.,)i have a file named packages and i want the packages file with a ".gz" extension by implementing a python program
3
by: Syoam4ka | last post by:
Hi, I want to change the backcolor of my button every second ,for example red, after a second blue, after a second re, then blue and so on...i need it in a web form not in a windows form(in winform...
5
by: Sunfire | last post by:
Can somebody put this code into c#? Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim imageFolder As String Dim imageText As String Dim bm As...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.