I have been stuck on this program for almost a week and cant seem to find out what's wrong with it. I would appreciate it if you helped me with my code and not write a different one (since usually its far more advanced to me to even understand).
Thank you in advance!
As for the program it has to get the sentence entered, output all the vowels and then say how many vowels the sentence had.
While loops run for as long as the given condition is true. So this means that your loop will keep looping only while iterations == 10.
Since you initialize it with 0 iterations = 0;, that means it will not be 10... and your loop body will never run.
ALSO -- that semicolon after the while statement doesn't belong there. It effectively ends the while loop completely so that it has no body. The following {block} of code is not considered part of the while statement.
So yeah... you probably wanted this:
1 2
while (iterations < 10) // <- loop while iterations is less than 10. NO SEMICOLON
{
line 8 you initialize iterations to 0 then on line 14 you loop only if iterations is equal to 10???
It looks to me like you are trying to input a sentence of 10 characters 1 character at a time then after each character is inputted you are increasing the count and the iterations. In that case it would have to be != 10 ( loop 10 times ).
Here is how I would do it.
1) Input a full sentence.
2) iterator through the sentence
3) if vowel found while iterating increase the count by 1.
Also are you trying to put all the vowels into a string or something? What are line 10 and 31 doing?
#include <iostream>
#include <string>
int main()
{
using std::cout;
using std::cin;
using std::getline; //I assume you want spaces in your sentences
using std::string; // can you use std::string? Or only char arrays/chars?
using std::endl;
string setence , vowels = "";
int numVowels = 0;
cout << "Please enter a setence: ";
getline( cin , setence );
for( int i = 0; setence[i]; ++i ) //loop while it is not a null character
{
switch( toupper(setence[i]) )
{
case'A': case'E': case'I': case'O': case'U': //some times y?
++numVowels;
vowels += setence[i];
}
}
cout << "Number of vowels: " << numVowels << endl;
cout << "Vowels used are: " << vowels << endl;
}
std::string, when accessed with the [] operator, is not necessarily null terminated. Null is just another character and has no special significance. It's only significant with char arrays.
When working with std::string, you really should check the length() or size() rather than look for the null.