I am working on this assignment for class and have been searching and can't seem to find a solution. I am having a problem after my program runs through one time and you type 'y' to start over, the first phrase automatically becomes "". I am unable to input a new phrase except for the second phrase. I am not seeing where the problem is or if I am completely missing something.
#include <iostream>
usingnamespace std;
int main()
{
char phraseOne[80];
char phraseTwo[80];
int wordCount1;
int wordCount2;
int len1;
int len2;
int compare;
char yOrN = 'Y';
while(toupper(yOrN) == 'Y')
{
//Initialize each time through
wordCount1 = 0;
wordCount2 = 0;
len1 = 0;
len2 = 0;
//Get phrase one
cout << "Enter a phrase: ";
cin.getline(phraseOne, 80);
cout << endl << endl;
//Get phrae two
cout << "Enter another phrase: ";
cin.getline(phraseTwo, 80);
cout << endl << endl;
//Count the number of words
for(int i = 0; i < 80; i++)
{
if (phraseOne[i] == ' ')
wordCount1++;
if (phraseTwo[i] == ' ')
wordCount2++;
}
//Find out the number of characters
len1 = strlen(phraseOne);
len2 = strlen(phraseTwo);
//compare the two phrases
compare = strcmp(phraseOne, phraseTwo);
//Output the number of words and character of each phrase
cout << "Phrase \"" << phraseOne << "\" has " << wordCount1 + 1 << " words and " << len1 << " characters." << endl << endl;
cout << "Phrase \"" << phraseTwo << "\" has " << wordCount2 + 1 << " words and " << len2 << " characters." << endl << endl;
//Output the results of the comparison
if (compare > 0)
cout << "In C++, phrase \"" << phraseOne << "\" is greater than phrase \"" << phraseTwo << "\"";
elseif (compare < 0)
cout << "In C++, phrase \"" << phraseTwo << "\" is greater than phrase \"" << phraseOne << "\"";
else
cout << "In C++, phrase \"" << phraseOne << "\" is equal to phrase \"" << phraseTwo << "\"";
//Ask the user to play again
cout << "\n\nPlay again? y/n: ";
cin >> yOrN;
cout << endl;
//Check for valid input
while (toupper(yOrN) != 'Y' && toupper(yOrN) != 'N')
{
cout << "\nInvalid input, Play again? y/n: ";
cin >> yOrN;
cout << endl;
}
}
//End the program
cout << "\n\nThank you for using the phrase comparison program";
sytem("pause")
return 0;
}
Use strings instead of vectors as you can then use the .size() in your for loops because trying to find a vector/array field that doesn't exhist gives errors, for(int i = 0; i < phraseOne.size(); i++)
Also try using Cin as an aurgument in getline cos this just works for me quite well... getline(cin, phraseOne);
@SunnyMcduff I cant help cause you wont be learning but I can guide you.
//Get phrase one
cout << "Enter a phrase: ";
cin.getline(phraseOne, 80);
cout << endl << endl;
//Get phrae two
cout << "Enter another phrase: ";
cin.getline(phraseTwo, 80);
cout << endl << endl;
Why do not you put this in a (if statement or while loop(that contains a if)). make your life easier so if the user say "Y" it will go back to the first phrase and if "N" it will quit program. delete the while loop and include that within the if. I hope this could help. Good Luck.
Why do not you put this in a (if statement) make your life easier so if the user say "Y" it will go back to the first phrase and if "N" it will quit program. I hope this could help. Good Luck.
My loop sends it back to the first phrase, the problem is that for some reason it keeps skipping over the prompt for the user to type in a value of the first phrase again. As though there is a value in the input stream waiting to be assigned automatically. I am able to type in an input for the second phrase every time. Here is what the program looks like the second time around.
Enter Phrase: Hello
Enter another Phrase: Hello Again
phrase "hello" has 1 word and 5 characters
etc.
.......
play again? y or n: y
Enter Phrase:
Enter another Phrase: Hello Again
Phrase "" has 1 word and 0 characters
Phrase "Hello Again" has 2 words and 11 characters
etc.
Unfortunately I am stuck using the format I am using because my professor wants it that way. Thanks for the responses, I am still trying to find a solution that works based on my constraints.
To get rid of that character that was being assigned to my first variable. It works, so I am happy, but its still mildly annoying that I don't know why that character was being used an input. Oh well, thanks again everyone for your time.
For some reason I was unable to use getline(cin, string word).
and while the ignore function works, I am running into trouble once the program loops through a few times, again random characters not being ignored. I guess I am using getline() wrong.