Reputation: 53
Alright guys, I'm new to programming and need a little help. I have a program that takes a sentence that's entered and shows the number of words and vowels. I then want to repeat the program if the user wants so, but when I use the do-while loop, in gets stuck in an infinite loop. After I enter 'Y' to repeat, it loops back to show the vowels and number of words I entered for the previous sentence.
Here's my code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char sentence[50];
int countA=0, countE=0, countI=0, countO=0, countU=0, countSP=0;
char repeat;
do {
cout << "Enter sentence : ";
cin.get(sentence, 50, '\n'); cin.ignore(10, '\n');
cout << sentence;
cout << "\nThird character is : " << sentence[2];
cout << "\nLast character is : " << sentence[strlen(sentence)-1];
cout << "\nLength of sentence is : " << strlen(sentence);
for(int x=0; x < strlen(sentence); x++) {
char ch = tolower (sentence[x]);
switch (ch) {
case 'a': countA++;break;
case 'e': countE++;break;
case 'i': countI++;break;
case 'o': countO++;break;
case 'u': countU++;break;
case ' ': countSP++;break;
}
}
cout << "\nNumber of A's : " << countA;
cout << "\nNumber of E's : " << countE;
cout << "\nNumber of I's : " << countI;
cout << "\nNumber of O's : " << countO;
cout << "\nNumber of U's : " << countU;
cout << "\nNumber of words : " << countSP+1;
cout << "\n\nWould you like to enter a new sentence? (Y/N): ";
cin >> repeat;
}while (repeat == 'y' || repeat == 'Y');
_getche();
return 0;
}
Upvotes: 0
Views: 1010
Reputation: 799
The main thing to remember is that C ++ does what is called Short-Circuit evaluation. If one side of the && condition is false, then everything is false. For an example,
int y = 1;
int x = 2;
if (y == 0 && x ==2) {
....
}
It is just going to check the first part. Since y = 1, it is going to return a false Boolean and this if statement will never be executed.
Like wise, with or, ||, if one side of the condition is true, then the condition will return a True Boolean and then the condition will be executed.
For your situation, the correct way to do this would be:
(repeat == 'Y' || repeat == 'y');
This way, if the first side is true, then the condition will be met and it will execute.
Upvotes: 1
Reputation: 13
You need to set repeat
at the beginning of your loop to anything else than Y
or y
(e.g. : repeat = NULL;
)
Upvotes: 0
Reputation: 1605
Change (repeat == 'y' && repeat == 'Y');
to (repeat == 'y' || repeat == 'Y');
EDIT: You also have no braces to open or close your loop try this.
while (repeat == 'y' || repeat == 'Y')
{
_getche();
}
because the loop has no body and it does not know what to execute.
EDIT2 why not do this?
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
do while (repeat == 'y' || repeat == 'Y') {
Enter()
cout << "\n\nWould you like to enter a new sentence? (Y/N): ";
cin >> repeat;
}
}
return 0;
Enter(){
char sentence[50];
int countA=0, countE=0, countI=0, countO=0, countU=0, countSP=0;
char repeat = Y;
cout << "Enter sentence : ";
cin.get(sentence, 50, '\n'); cin.ignore(10, '\n');
cout << sentence;
cout << "\nThird character is : " << sentence[2];
cout << "\nLast character is : " << sentence[strlen(sentence)-1];
cout << "\nLength of sentence is : " << strlen(sentence);
for(int x=0; x < strlen(sentence); x++) {
char ch = tolower (sentence[x]);
switch (ch) {
case 'a': countA++;break;
case 'e': countE++;break;
case 'i': countI++;break;
case 'o': countO++;break;
case 'u': countU++;break;
case ' ': countSP++;break;
}
cout << "\nNumber of A's : " << countA;
cout << "\nNumber of E's : " << countE;
cout << "\nNumber of I's : " << countI;
cout << "\nNumber of O's : " << countO;
cout << "\nNumber of U's : " << countU;
cout << "\nNumber of words : " << countSP+1;
repeat = ' ';
}
Upvotes: 1
Reputation: 532
Try replacing repeat == 'y' && repeat == 'Y'
with repeat == 'y' || repeat == 'Y')
, because the condition in your code can never be true.
Upvotes: 1
Reputation: 91580
The expression (repeat == 'y' && repeat == 'Y')
will always equal false, as repeat
cannot be equal to both 'y'
and 'Y'
.
You might have meant:
(repeat == 'y' || repeat == 'Y');
Upvotes: 3