creating an alphabetical order program

working out some bugs in an alphabetical listing program

Page 1 of 1

14 Replies - 2938 Views - Last Post: 25 September 2009 - 10:52 PM Rate Topic: -----

#1 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

creating an alphabetical order program

Posted 25 September 2009 - 06:45 PM

I'm sure you are all well aware of most of what I'm about to say here but I think you should understand my thinking on this program and why I'm confused.
My instructor gave us an assignment where we were to create a program that promts the user for three names. The user inputs the three names and the program lists them back in alphabetical order. How I decided to do this was to use the ASCII character number of each character entered to determine which name will come first in the order. As, I'm sure you all know, the enter key and the space key have low ASCII character values as do capitol letters. The values are lower for a capital A and higher for a lowercase a. First, here is my code:

this code uses an if/else control structure.
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
	string name1, name2, name3, name4, name5, name6;
	cout<<"Please enter three names sepparated by spaces. ";
	cin>>name1>>name2>>name3;
	if(name1<name2&&name1<name3)
		name4=name1;
	else if(name1>name2&&name1>name3)
			name6=name1;
		else
			name5=name1;
	if(name2<name1&&name2<name3)
			name4=name2;
		else if(name2>name1&&name2>name3)
			name6=name2;
		else
			name5=name2;
	if(name3<name2&&name3<name1)
		name4=name3;
		else if(name3>name2&&name3>name1)
			name6=name3;
		else name5=name3;

	

	cout<<"The names entered were "<<name4<<", "<<name5<<", and "<<name6<<endl<<endl;
	return 0;
} 


And this code uses a switch:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
	string name1, name2, name3, name4, name5, name6;
	cout<<"Please enter three names sepparated by spaces. ";
	cin>>name1>>name2>>name3;
	switch(name1)
	{
	case (name1<name2&&name1<name3):
		name4=name1;
		break;
	case (name1>name2&&name1>name3):
		name6=name1;
		break;
	default:
		name5=name1;
		break;
	}
	switch(name2)
	{
	case (name2<name1&&name2<name3):
		name4=name2;
		break;
	case (name2>name1&&name2>name3):
		name6=name2;
		break;
	default:
		name5=name2;
		break;
	}
	switch(name3)
	{
	case(name3<name1&&name3<name2):
		name4=name3;
		break;
	case(name3>name1&&name3>name2):
		name6=name3;
		break;
	default:
		name5=name3;
		break;
	}
	cout<<"The names you entered were "<<name4<<", "<<name5<<", and "<<name6<<endl<<endl;
	return 0;

}

I recieved a hundred to the assignment but that isn't quite good enough for me. The problem with this code is that while "Alexandria" comes before "Barack" or "barack", "alexandria" comes after "Barack" in the list. The ASCII character B has a smaller numeric value than the ASCII value of 'a'. Do i just need to make this program bigger and account for lowercase letters or is there a better way to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: creating an alphabetical order program

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: creating an alphabetical order program

Posted 25 September 2009 - 07:40 PM

Convert it to all upper or lower case before running your code:
http://www.dreaminco...e/snippet39.htm
Was This Post Helpful? 0
  • +
  • -

#3 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 09:51 PM

Well, thank you for the snippet direction. I am getting some errors and don't know why. I am sure that I'm just using it incorrectly. I don't understand the code you directed me to and unless I understand how it does what it does, I will probably continue to use it incorrectly. anyway, here is what I've got:
#include<iostream>
#include<string>
#include<iomanip>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
int main()
{
	string name1, name2, name3, name4, name5, name6;
	string ToUpper, strToConvert; 
	cout<<"Please enter three names sepparated by spaces. ";
	cin>>name1>>name2>>name3;
	{	
		for(unsigned int i=0;i<strToConvert.length();i++)
		{
		strToConvert[i]=ToUpper(strToConvert[i]);
		}
		return strToConvert;//return the converted string
	}
	if(name1<name2&&name1<name3)
		name4=name1;
	else if(name1>name2&&name1>name3)
			name6=name1;
		else
			name5=name1;
	if(name2<name1&&name2<name3)
			name4=name2;
		else if(name2>name1&&name2>name3)
			name6=name2;
		else
			name5=name2;
	if(name3<name2&&name3<name1)
		name4=name3;
		else if(name3>name2&&name3>name1)
			name6=name3;
		else name5=name3;

	

	cout<<"The names entered were "<<name4<<", "<<name5<<", and "<<name6<<endl<<endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:17 PM

what is the value of the strToConvert in the beginning of your program, before entering the for loop?

also, i must say i am not very impressed by this logic of yours...
why don't you try a simple sort of character array instead, then you would also not have to worry about the occurence of spaces in names.............
Was This Post Helpful? 0
  • +
  • -

#5 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:24 PM

To turn strings into upper case use the transform(), you need to include the <algorithm> library
Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:25 PM

Just use a quicksort method.

if string1 < string2
swap

if string2 < string3
swap

Done. Will work with any case.

Swap function:
void strSwap(string& str, string& str2)
{
  string t = str;

  str = str2;
  str2 = t;
}

This post has been edited by eker676: 25 September 2009 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:25 PM

hmmm.... i don't believe you read the entire thread. i haven't gotten to arrays yet. i'm a student. i'm 4 weeks into C++. and spaces? i'm not sure what you mean. and strToConvert would be name1 name2 and name3 wouldn't it? that's what i'm saying though, in my last post, is that i don't understand the code that the previous poster referred me to. therefore i don't know the answer to your question.
Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:26 PM

Use the quick sort method above ^ ;)

Then print out like this:
cout << str1 << " " << str2 << " " << str3 << endl;

This post has been edited by eker676: 25 September 2009 - 10:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:30 PM

View Postwoodpryan, on 25 Sep, 2009 - 09:25 PM, said:

hmmm.... i don't believe you read the entire thread. i haven't gotten to arrays yet. i'm a student. i'm 4 weeks into C++. and spaces? i'm not sure what you mean. and strToConvert would be name1 name2 and name3 wouldn't it? that's what i'm saying though, in my last post, is that i don't understand the code that the previous poster referred me to. therefore i don't know the answer to your question.

oh, in that case use:
strToConvert=name1+name2+name3;


after your statement
cin>>name1>>name2>>name3;



probably you will better understand what i ment by my last post after you get into arrays :)
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:35 PM

#include<iostream>
#include<string>
#include<iomanip>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
int main()
{
	string name1, name2, name3, name4, name5, name6;
	string ToUpper, strToConvert; 
	cout<<"Please enter three names sepparated by spaces. ";
	cin>>name1>>name2>>name3;
	{
		for(unsigned int i=0;i<strToConvert.length();i++)
		{
			strToConvert[i]=toupper(strToConvert[i]);
		}
		//return strToConvert;//return the converted string
	}
	if(name1<name2&&name1<name3)
		name4=name1;
	else if(name1>name2&&name1>name3)
		name6=name1;
	else
		name5=name1;
	if(name2<name1&&name2<name3)
		name4=name2;
	else if(name2>name1&&name2>name3)
		name6=name2;
	else
		name5=name2;
	if(name3<name2&&name3<name1)
		name4=name3;
	else if(name3>name2&&name3>name1)
		name6=name3;
	else name5=name3;
	
	
	
	cout<<"The names entered were "<<name4<<", "<<name5<<", and "<<name6<<endl<<endl;
	return 0;
}

You were trying to return from main, but to where? Also, function names are case sensitive.
Was This Post Helpful? 0
  • +
  • -

#11 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:40 PM

View Postaks29921, on 25 Sep, 2009 - 09:30 PM, said:

View Postwoodpryan, on 25 Sep, 2009 - 09:25 PM, said:

hmmm.... i don't believe you read the entire thread. i haven't gotten to arrays yet. i'm a student. i'm 4 weeks into C++. and spaces? i'm not sure what you mean. and strToConvert would be name1 name2 and name3 wouldn't it? that's what i'm saying though, in my last post, is that i don't understand the code that the previous poster referred me to. therefore i don't know the answer to your question.

oh, in that case use:
strToConvert=name1+name2+name3;


after your statement
cin>>name1>>name2>>name3;



probably you will better understand what i ment by my last post after you get into arrays :)

thank you for your reply. i did what you said and these are the errors i'm getting:
alphabetical.cpp: In function ‘int main()’:
alphabetical.cpp:17: error: no match for call to ‘(std::string) (char&)’
alphabetical.cpp:19: error: cannot convert ‘std::string’ to ‘int’ in return


here is the modified code:
#include<iostream>
#include<string>
#include<iomanip>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
int main()
{
	string name1, name2, name3, name4, name5, name6;
	string ToUpper, strToConvert; 
	cout<<"Please enter three names sepparated by spaces. ";
	cin>>name1>>name2>>name3;
	strToConvert=name1+name2+name3;
	{	
		for(unsigned int i=0;i<strToConvert.length();i++)
		{
		strToConvert[i]=ToUpper(strToConvert[i]);
		}
		return strToConvert;//return the converted string
	}
	if(name1<name2&&name1<name3)
		name4=name1;
	else if(name1>name2&&name1>name3)
			name6=name1;
		else
			name5=name1;
	if(name2<name1&&name2<name3)
			name4=name2;
		else if(name2>name1&&name2>name3)
			name6=name2;
		else
			name5=name2;
	if(name3<name2&&name3<name1)
		name4=name3;
		else if(name3>name2&&name3>name1)
			name6=name3;
		else name5=name3;

	

	cout<<"The names entered were "<<name4<<", "<<name5<<", and "<<name6<<endl<<endl;
	return 0;
}




View Posteker676, on 25 Sep, 2009 - 09:25 PM, said:

Just use a quicksort method.

if string1 < string2
swap

if string2 < string3
swap

Done. Will work with any case.

Swap function:
void strSwap(string& str, string& str2)
{
  string t = str;

  str = str2;
  str2 = t;
}

i should have thought of that. and that makes complete sense. but it doesn't solve the main issue, which is that the ASCII value of an uppercase letter is greater than that of a lowercase letter making B < a and A < b at the same time. in other words Alexandria < Barack but Barack < alexandria.
Was This Post Helpful? 0
  • +
  • -

#12 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:41 PM

Go the simple route.

Use a swap method and two if statements.

Also, in your current code you are trying to place a function within your code. You don't need to return a value because a function was never called.

Edit:
Convert the string to lowercase:

#include <string>
#include <cctype>

... 

string strToLower(const string& str)
{
	string t = "";

	int len = str.length();

	for(int i = 0; i < len; i++)
	{
		t += tolower(str[i]);
	}
	return t;
}



Call it like this:

name1 = strToLower(name1);


Do that for all the names and then use the quicksort method.

This post has been edited by eker676: 25 September 2009 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#13 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:45 PM

for my purposes, shouldn't i chang int to string? for(unsigned int i=0;i<strToConvert.length();i++)
Was This Post Helpful? 0
  • +
  • -

#14 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:47 PM

You can't convert all the strings at once. This will result in a big jumble of names all in one string.

You have to convert each name separately from one another so they are usable later.
Was This Post Helpful? 0
  • +
  • -

#15 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: creating an alphabetical order program

Posted 25 September 2009 - 10:52 PM

View Posteker676, on 25 Sep, 2009 - 09:47 PM, said:

You can't convert all the strings at once. This will result in a big jumble of names all in one string.

You have to convert each name separately from one another so they are usable later.

ok. thanks. i will work on this and get back to this thread tomorrow. thanks everyone for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1