473,698 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,te mp;
int TmpVal=0x123456 78;
printf("Name: ");
gets(name);
name_len=strlen (name);
for(a=0;a<=name _len;a++) {
temp=name[a];
TmpVal=TmpVal+t emp;

}
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 1713
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,te mp;
int TmpVal=0x123456 78;
printf("Name: ");
gets(name);
name_len=strlen (name);
for(a=0;a<=name _len;a++) {
temp=name[a];
TmpVal=TmpVal+t emp;

}
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,te mp;
int TmpVal=0x123456 78;
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+t emp;
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.l earn.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
1581
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 // Find all blocks containing the postal code, a minimum of 50 characters and a maximum of 200 characters before and after.
3
1400
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 = "D:\\Project\\Database\\database.mdb" db1.OpenCurrentDatabase(dbname) Now, I have the problem, that I don't know how I can read the lines
6
1932
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 those pages. There are some things (a result of this transition,) which I'm not too happy about :-( I'm using 1st Page 2000. Much of my content is created in Word and then cut and pasted from Word-to-NotePad-to-the page. Formerly with FP the...
0
278
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") I want to know in my appl what message to expect from "Task Manager", when user will try to close my appl from the "Windows Task Manager". what message should I get in my appl thank you Pola
0
2415
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 ID="FormView1" runat="server" DataSourceID="ObjectDataSource1"> <EditItemTemplate>
11
11528
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 function whether i click the link then i tried this (using forms)
14
1304
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
1680
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 there is a timer.tick, there it works, but in the asp.net there is a timer.elapse, and it doesn't workd , don't know why, so i was told that javascripters could help me) can somebody help me please?(javascript/c#)
5
1722
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 Bitmap Dim ms As MemoryStream imageFolder = Request.QueryString(imFolder)
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.