Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 6th, 2007, 8:43 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Need just a little help

I'm trying to expand my looking for the longest word program. This new version asks the user to input a maximum of 5 words and then outputs the longest word. The program I initially wrote worked great, but what I didn't like about it is that if the user entered 2 words that were the same length, the compiler would just output the first of the two words that were the same length.

So I'm trying to add just a little bit of additional code that will test to see if a word that has been entered is the same length as a word that was previously entered.

Only problem is that the code in my if statement is executed all the time, even before the user has had a chance to enter the second word. Maybe I have the if statement in the wrong place.

Here is my program:

int main()
{
	const int arraySize = 5;
	string words[arraySize];
	string max;
	char reply = 0;
	int count = 0;

	do
	{
		cout << "Enter a word: ";
		cin >> words[count++];
		
		cout << "Do you want to enter another? y/n:";
		cin >> reply;
		cout << endl;

        if (words[count].length() == words[count].length())
			cout << "Please enter a different word";

	}while (count < arraySize && tolower(reply) == 'y');

	if (count == arraySize)
		cout << "You cannot enter any more words." << endl;
	
	 max = words[0];

	for (int i = 0; i < arraySize; i++)
		if (words[i].length() > max.length())
			max = words[i];

	cout << endl << "The longest word is " << max << endl;

	return 0;
357mag is offline   Reply With Quote
Old Jun 6th, 2007, 9:54 PM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I think I may know what the problem is. In my code I'm actually comparing the same value of count. So I'm actually comparing the length of the first word with the length of the first word. And since that is always true, the code in the if statement will always run.

Somehow I need to write the code so I'm comparing consecutive values of count like this:

if (count[0].length == count[1].length)
   

if (count[1].length  == count[2].length)

Only snag is I don't quite know how to do that. But I'm working on it.
357mag is offline   Reply With Quote
Old Jun 6th, 2007, 9:57 PM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I mean:
words[0].length == words[1].length

words[1].length == words[2].lenght

I think that's what I mean.
357mag is offline   Reply With Quote
Old Jun 6th, 2007, 10:46 PM   #4
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 116
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
words[i].length == words[i+1].length

Not entirely sure of the variables used, but that's the answer I'd give you
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jun 6th, 2007, 10:52 PM   #5
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I tried your suggestion but it doesn't work. Somehow I have to write the if statement or place it in some position that it will test for consecutive values of the counter variable. All my ideas have failed so far.
357mag is offline   Reply With Quote
Old Jun 6th, 2007, 11:03 PM   #6
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
The simplest solution with the knowledge you seem to currently have in C++ would be to keep an array of max's. If the length is equal, append to the array. If the length is greater, start the array over again.

If this doesn't make sense, I can demonstrate. But I don't want to ruin the fun.
Sane is offline   Reply With Quote
Old Jun 6th, 2007, 11:14 PM   #7
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by 357mag View Post
So I'm actually comparing the length of the first word with the length of the first word.
Nope. It compares the length of the string one element past the element used for input in the current iteration.

This is because you use post increment inside the subscript operator:
cin >> words[count++];
then in the same iteration:
if (words[count].length() == words[count].length())
Oops! Besides being a pointless test, it also overshoots the array length by one (eventually).

edit: peaceofpis code stinks too. lol.

Last edited by Cache; Jun 6th, 2007 at 11:24 PM.
Cache is offline   Reply With Quote
Old Jun 6th, 2007, 11:12 PM   #8
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 116
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
After fully reading the code, I think I know. Switching the order of something seems to work, I hope this is what you're looking for

#include <iostream>
#include <string>
using namespace std;

int main()
{
	const int arraySize = 5;
	string words[arraySize];
	string max;
	char reply = 0;
	int count = 0;

	do
	{
		cout << "Enter a word: ";
		count++;
		cin >> words[count];
		
        if (words[count].length() == words[count-1].length() )
			cout << "Please enter a different word" << endl;

		cout << "Do you want to enter another? y/n:";
		cin >> reply;
		cout << endl;

	}while (count < arraySize && tolower(reply) == 'y');

	if (count == arraySize)
		cout << "You cannot enter any more words." << endl;
	
	 max = words[0];

	for (int i = 0; i < arraySize; i++)
		if (words[i].length() > max.length())
			max = words[i];

	cout << endl << "The longest word is " << max << endl;
	return 0;
}
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jun 6th, 2007, 11:22 PM   #9
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Oh by the way... my comment explained how to show all the words that have the maximum length, which is how a program should handle this sort of situation. I don't think you should not allow the user to enter same length words. It doesn't make sense why one would need a program that behaves in such a manner.
Sane is offline   Reply With Quote
Old Jun 6th, 2007, 11:39 PM   #10
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 116
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
edit 2: peaceofpis code stinks too. lol.

Even though it does what he asked for. Fixed code here:

	do
	{
		cout << "Enter a word: ";
		count++;
		cin >> words[count];

		if (count > 1)
			for (int i = 1; i <= count; i++)		
				if ( i != count && words[i].length() == words[count].length() )
				{
					cout << "Please enter a different word" << endl;
					break;
				}

edit: take back my added idea, coding gets sloppier as I go
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC