3 Replies - 963 Views - Last Post: 21 July 2011 - 03:22 PM Rate Topic: -----

#1 lilli_g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 11

Problem displaying output after while loop

Posted 21 July 2011 - 02:25 PM

I am trying to write a program that prompts a user to input a sequence of characters and outputs the number of vowels in the the sequence.

My program will not display cout << "The number of vowels is "<< numVowels << "." << endl; after my loop. If I place this statement inside the while loop, it'll display it every time it loops, with the final one displaying the correct amount of vowels.

Also, it evaluates an extra "is not a vowel." with no vowel assigned.

Any suggestions?

#include <iostream>
#include <iomanip>

using namespace std;

bool isVowel (char letter);
  
int main()
{ 
    char letter;
    int result; 
    int numVowels = 0;
      
           
    cout << "Enter a series of letters to see if individual letters are vowels: ";
    cin.get(letter);    
    
    while (cin)
    { 
          result = isVowel (letter); 
    
    if ( result == 1)
    {      
    cout << "The letter " << letter << " is a vowel." <<endl;
     numVowels++;
     }
    else
    {
        cout << "The letter " << letter << " is not a vowel." << endl; 
        }
  
    
    cin.get(letter);
    
    }//end while
    
     cout<<"The number of vowels is "<< numVowels <<endl;
   
    
    return 0;
}

bool isVowel (char letter)
{
     
     switch (letter)
     
     {
     case 'a':
     case 'A':
     case 'e':
     case 'E':
     case 'i':
     case 'I':
     case 'o':
     case 'O':
     case 'u':
     case 'U':
            return true;
     break;
     default:
             return false;
     }// end switch
}// end isVowel
     


Is This A Good Question/Topic? 0
  • +

Replies To: Problem displaying output after while loop

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Problem displaying output after while loop

Posted 21 July 2011 - 02:32 PM

How are you exiting the loop? What operating system are you using?

The cout did display for me at the end of the loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 lilli_g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 11

Re: Problem displaying output after while loop

Posted 21 July 2011 - 03:02 PM

I'm using Windows 7. Is it at least showing the correct sum of vowels?
Was This Post Helpful? 0
  • +
  • -

#4 lilli_g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 11

Re: Problem displaying output after while loop

Posted 21 July 2011 - 03:22 PM

I got it to work:

I replaced while (cin) to while (letter >= 'A' && letter <= 'z').

Thank you for taking a look at it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1