Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

ASCII Code Help

ascii value ascii

  • Please log in to reply
7 replies to this topic

#1 iNEXSERTiON

iNEXSERTiON

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 12 January 2010 - 06:16 AM

Write a C++ program that will allow for input of two full names in this format: Lastname, Firstname. It must print the names and state which name comes before the other alphabetically. For a test run, you may input two names of your choice.


Here's what I have so far:
#include <iostream.h>
#include <conio.h>

void main();
{	int firstname, lastname, first, second, numInput;

	cout << "Please enter how many names you'd like to input: ";
   cin >> numInput;

   cout << endl << endl;

   cout << "Enter the name(s) in the following format [LASTNAME, FIRSTNAME]";
   cout << endl << endl;

   for(int x; x <= numInput; x++)
   {	cout << "Please enter the name: ";

      cout << "Lastname... ";
      cin >> lastname;
      cout << endl;

      cout << "Firstname...";
      cin >> firstname;
      cout << endl;
   }

What I need help with is, how do I add the code segment where it will find the ASCII value of the first letter of the lastname, and then tell it which of the names comes first...
  • 0

#2 Walle

Walle

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 70 posts

Posted 12 January 2010 - 07:21 AM

Well, I won't give you any code, but I can give you some *pointers :P

First, check out the ASCII table, possibly from asciitable.com. As you see, every letter, number and symbol are represented also by values. You see values both in decimal, hex and octal. The reason is simply because "ASCII values", just like any other value, is just a bunch of bits. 8 bits, or 1 byte, to be precise. Each ASCII letter is represented by one byte of data (it's a char). Take the capital letter "L" for example, which is 0x4C (76 in decimal). The binary representation is "0100 1100". If you have an unsigned int holding the value 76, the binary representation will also be "0100 1100". Although, the size of ints varies, 4 bytes is the usual size in windows for example, therefore the unsigned int would be padded with a lot of zeros (25 to be precise), while a char is only 1 byte so the binary value is therefore only padded with one zero.

The reason I'm telling you this is because that crucial to know about ASCII. If you already knew it, sorry to be bugging you :) The point being, you don't need to compare ASCII letters to eachother, you could compare numbers instead. Taken that two letters are in the same case, the one with the lower numerical value is the first one. If they are in different case, just subtract 32 from the higher value, and you can still compare them.

Also, your name variables cannot be int's, since an int only can hold a numerical value.
They should be strings (or arrays of chars).
  • 0
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#3 thechef

thechef

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 12 January 2010 - 09:27 AM

Walle is right; you should probably store your names as strings. With strings, you can do less-than greater-than comparisons. If one string is less than another string, it comes before the other in the alphabet.

Cheers!
  • 0

#4 iNEXSERTiON

iNEXSERTiON

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 12 January 2010 - 12:14 PM

Yeah, we barely talked about all that stuff. We learned a BIT about strings. But thanks for the advice, I'll try to finish it tomorrow.
  • 0

#5 julmuri

julmuri

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 137 posts

Posted 12 January 2010 - 12:55 PM

Little tip how I might go about it:

class Name { /* ... */ };


struct AscendingName
	: public std::binary_function< Name, Name, bool >
{
	bool operator()( const Name& a, const Name& b )
	{
		const std::string& a_ = a.last();
		const std::string& b_ = b.last();

		if ( a_.empty() || b_.empty() )
		{
			throw std::runtime_error( "empty name" );
		} // if

		return a_[0] < b_[0];
	}
}; // struct AscendingName


// ...

typedef std::vector< Name >			NameList;
typedef NameList::iterator			NameIter;
typedef NameList::const_iterator		NameConstIter;

NameList names;

// ...

std::sort( names.begin(), names.end(), AscendingName() );

Athough, if you do not mind depencies, boost lambda is way to go.

Anwaysy because I am very bored person I wrote hole sample. (:
Pastebin, do not click if you do not like to be spoonfed
  • 0

std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;


#6 iNEXSERTiON

iNEXSERTiON

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 14 January 2010 - 06:39 AM

I'm using Borland C++ 5... But is there any way I can do it without strings? Like, somehow tell it to take the first letter of the last name and find it's value?
  • 0

#7 Walle

Walle

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 70 posts

Posted 14 January 2010 - 07:46 AM

Why would You like to complicate matters by not using strings? You need to store the names for display later anyway. O.f.c. You could store the names into arrays of char, but that's pretty much just a very bad version of a string..
  • 0
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#8 iNEXSERTiON

iNEXSERTiON

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 14 January 2010 - 04:51 PM

**, well the assignment is due tomorrow. I got something figured out that works.

Edited by iNEXSERTiON, 14 January 2010 - 05:29 PM.

  • 0





Also tagged with one or more of these keywords: ascii value, ascii

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download