473,320 Members | 1,916 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,320 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 1695
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.