![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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; |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
I mean:
words[0].length == words[1].length words[1].length == words[2].lenght I think that's what I mean. |
|
|
|
|
|
#4 |
|
hi: for(;;) goto hi;
|
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#6 |
|
Banned
![]() ![]() |
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. |
|
|
|
|
|
#7 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
This is because you use post increment inside the subscript operator: cin >> words[count++]; if (words[count].length() == words[count].length()) edit: peaceofpis code stinks too. lol. Last edited by Cache; Jun 6th, 2007 at 11:24 PM. |
|
|
|
|
|
|
#8 |
|
hi: for(;;) goto hi;
|
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. |
|
|
|
|
|
#9 |
|
Banned
![]() ![]() |
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.
|
|
|
|
|
|
#10 |
|
hi: for(;;) goto hi;
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|