#include<iostream.h>
#include<stdlib.h>

const int MAXCHARS = 500;
char st_line[MAXCHARS];
char ch_option;
int i_va;
int i_ve;
int i_vi;
int i_vo;
int i_vu;
int i_word;
int i_count = 1;
int i_number = 0;
int i_vowel = 0;
char ch_char;


void getdata();
void vowels();
void words();
void putdata();

int main()
{
do{
getdata();
vowels();
words();
putdata();
}while(ch_option != 'y');
}

void getdata()
{
cout << "\nPlease enter line of text:\n";
cin.getline(st_line,MAXCHARS);
vowels();
}

void vowels()
{

while((ch_char = st_line[i_number++]) != '\0')
switch(ch_char)
{
case 'a': case 'A':
ch_char = i_va;
case 'e': case 'E':
ch_char = i_ve;
case 'i': case 'I':
ch_char = i_vi;
case 'o': case 'O':
ch_char = i_vo;
case 'u': case 'U':
ch_char = i_vu;
i_vowel++;
}
return;
}

void words()
{

if(st_line[i_word] == ' ' || st_line[i_word] == '\0')
i_count--;
while(st_line[i_word] != '\0')
{
if(st_line[i_word] == ' ' && (st_line[i_word + 1] != ' ' && st_line[i_word + 1
] != '\0'))
i_count++;
i_word++;
}
return;
}


void putdata()
{
cout << "\nInputted String";
cout << "\n\n*******************************";
cout << "\n\n " << st_line;
cout << "\n\nNumber of a's: " << i_va;
cout << "\nNumber of e's: " << i_ve;
cout << "\nNumber of i's: " << i_vi;
cout << "\nNumber of o's: " << i_vo;
cout << "\nNumber of u's: " << i_vu;
cout << "\nTotal Vowels: "<< i_vowel;
cout << "\n\nNumber of Words: " << i_word;
cout << "\n\n\nDo you want to exit (Y/N)? ";
cin.get(ch_option);
}

Dani AI

Generated

Short answer: the loop+word-count fails because counters/indices and the option character are not properly initialized or reset between runs, the vowel switch in the first post used the wrong assignments and fell through, and the input/terminal-read for the Y/N prompt is tripped by leftover newlines. Fix those three things and the program will behave.

Concrete guidance and a robust pattern to follow:

  • Read each input line with std::getline into a std::string. That avoids buffer-length bugs and makes word tokenization easy.
  • Use local counters (reset them at the start of each iteration) instead of globals that keep previous values.
  • Count words by tokenizing with std::istringstream and iss >> token — this treats any run of whitespace as a single separator and handles multiple spaces automatically.
  • In the vowel switch, increment the correct counter and include break statements. Normalize characters with std::tolower(static_cast<unsigned char>(ch)) to avoid UB on negative char.
  • For the continue prompt, read a whole line (again std::getline) and check the first non-empty character with std::toupper so you don't get a leftover newline being read as the answer.

Example (modern, minimal pattern):

#include <iostream>
#include <sstream>
#include <string>
#include <cctype>

int main() {
  std::string line, reply;
  do {
    std::cout << "Enter a line:\n";
    std::getline(std::cin, line);

    int a=0,e=0,ii=0,o=0,u=0;
    for (char ch : line) {
      switch (std::tolower(static_cast<unsigned char>(ch))) {
        case 'a': ++a; break;
        case 'e': ++e; break;
        case 'i': ++ii; break;
        case 'o': ++o; break;
        case 'u': ++u; break;
      }
    }

    int words = 0;
    std::istringstream iss(line);
    std::string token;
    while (iss >> token) ++words;

    std::cout << "a:" << a << " e:" << e << " i:" << ii
              << " o:" << o << " u:" << u
              << "  vowels:" << (a+e+ii+o+u)
              << "  words:" << words << '\n';

    std::cout << "Try again? (Y/N): ";
    std::getline(std::cin, reply);
  } while (!reply.empty() && std::toupper(static_cast<unsigned char>(reply[0])) == 'Y');

  return 0;
}

Quick troubleshooting notes: always initialize counters, reset per-loop; avoid void main; if you must mix operator>> and getline, call std::cin.ignore() to discard the leftover newline; and test with inputs containing multiple spaces and leading/trailing spaces. — the second post fixed the vowel increments but still needs resets and clean input handling. — the switch fall-through was indeed an important part of the original bug.

Recommended Answers

All 2 Replies

This looks like a dup of your switch () question.

Wrong program this is the one that runs but it doesnt loop when i want it to and I wanna know how can I make it count the number of words that the user inputs


#include<iostream.h>
#include<stdlib.h>

const int MAXCHARS = 500;
char st_line[MAXCHARS];
char ch_option;
int i_va;
int i_ve;
int i_vi;
int i_vo;
int i_vu;
int i_number = 0;
int i_vowel = 0;
char ch_char;


void getdata();
void vowels();
void putdata();

void main()
{
while(ch_option != 'n')
   {
getdata();
vowels();
putdata();
   }
}

void getdata()
{
        cout << "\nPlease enter line of text:\n";
        cin.getline(st_line,MAXCHARS);
}

void vowels()
{
while((ch_char = st_line[i_number++]) != '\0')
switch(ch_char)
  {
        case 'a': case 'A':
          i_va++;
        break;
        case 'e': case 'E':
          i_ve++;
        break;
        case 'i': case 'I':
          i_vi++;
        break;
        case 'o': case 'O':
          i_vo++;
        break;
        case 'u': case 'U':
          i_vu++;
        break;
  }
        i_vowel = i_va + i_ve + i_vi + i_vo + i_vu;
}


void putdata()
{
        cout << "\nInputted String";
        cout << "\n\n*******************************";
        cout << "\n\n " << st_line;
        cout << "\n\nNumber of a's: " << i_va;
        cout << "\nNumber of e's: " << i_ve;
        cout << "\nNumber of i's: " << i_vi;
        cout << "\nNumber of o's: " << i_vo;
        cout << "\nNumber of u's: " << i_vu;
        cout << "\nTotal Vowels: "<< i_vowel;
        cout << "\n\nNumber of Words: ";
        cout << "\n\n\nDo you want to exit (Y/N)? ";
        cin.get(ch_option);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.