Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:08:04+00:00 2026-06-01T15:08:04+00:00

First off, thanks in advance for your help. This issue is driving me nuts.

  • 0

First off, thanks in advance for your help. This issue is driving me nuts.

I have a program that accepts a c-string, and then can count the number of vowels and consonants. This works without issue. However, I also need to include a function that allows the user to create a new string. The problem is, though, when the user selects “new string” from the menu, it just loops through the newString() method, without waiting for the user’s input. It then creates a new, blank screen.

Here is the entire program. The newString() method is at the end.

#include <iostream>    
using namespace std;

// function prototype
void printmenu(void);
int vowelCount(char *);
int consCount(char *);
int cons_and_vowelCount(char *);
void newString(char *, const int);

int main() {

    const int LENGTH = 101;
    char input_string[LENGTH];      //user defined string
    char choice;                        //user menu choice
    bool not_done = true;       //loop control flag

    // create the input_string object
    cout << "Enter a string of no more than " << LENGTH-1 << " characters:\n";
    cin.getline(input_string, LENGTH);

    do {
        printmenu();
        cin >> choice;
        switch(choice)
        {
            case 'a':
            case 'A':
               vowelCount(input_string);
               break;
            case 'b':
            case 'B':
               consCount(input_string);
               break;
            case 'c':
            case 'C':
                cons_and_vowelCount(input_string);
                break;
            case 'd':
            case 'D':
               newString(input_string, LENGTH);
               break;
            case 'e':
            case 'E':
               exit(0);
            default:
                cout << endl << "Error: '" << choice << "' is an invalid selection" << endl;
                break;
        } //close switch
    } //close do

while (not_done);
return 0;
} // close main

/* Function printmenu()
 * Input:
 *  none
 * Process:
 *  Prints the menu of query choices
 * Output:
 *  Prints the menu of query choices
 */
void printmenu(void)
{
    cout << endl << endl;
    cout << "A) Count the number of vowels in the string" << endl;
    cout << "B) Count the number of consonants in the string" << endl;
    cout << "C) Count both the vowels and consonants in the string" << endl;
    cout << "D) Enter another string" << endl;
    cout << "E) Exit the program" << endl;
    cout << endl << "Enter your selection: ";
    return;    
}

int vowelCount(char *str) {
    char vowels[11] = "aeiouAEIOU";
    int vowel_count = 0;

    for (int i = 0; i < strlen(str); i++) {
        for (int j = 0; j < strlen(vowels); j++) {
            if (str[i] == vowels[j]) {
                vowel_count++;
            }
        }
    }
    cout << "String contains " << vowel_count << " vowels" << endl;
    return vowel_count;
} // close vowelCount

int consCount(char *str) {
    char cons[43] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
    int cons_count = 0;

    for (int i = 0; i < strlen(str); i ++) {
        for (int j = 0; j < strlen(cons); j++) {
            if (str[i] == cons[j]) {
                cons_count++;
            }
        }
    }
    cout << "String contains " << cons_count << " consonants" << endl;
    return cons_count;
} // close consCount

int cons_and_vowelCount(char *str) {
    int cons = consCount(str);
    int vowels = vowelCount(str);
    int total = cons + vowels;

    cout << "The string contains a total of " << total << " vowels and "
            "consonants" << endl;
    return total;
}

void newString(char *str, int len) {
    cout << "Enter a string of no more than " << len-1 << " characters:\n";
    cin.getline(str, len);
    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:08:05+00:00Added an answer on June 1, 2026 at 3:08 pm

    The statement cin >> choice only consumes the character they type, not the carriage return that follows. Thus, the subsequent getline() call reads an empty line. One simple solution is to call getline() instead of cin >> choice and then use the first character as the choice.

    BTW, the while (not done) should immediately follow the do { … }, and the return 0 is redundant. Also, you should call newString at the start of the program instead of repeating its contents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off thank you in advance for taking time to help me with this,
First off thanks to all the users who have made my android developing adventure
First off, there's a bit of background to this issue available on my blog:
First off, I'm working on an app that's written such that some of your
First off I am new to this site and it is a big help,
first off this is a class assignment so i would appreciate help but just
First off, I'd like to apologize in advance for not knowing this. I've been
First off, let me say that this is not homework (I am an A-Level
First off, I am using Windows XP. I have multiple hard drives and it
First off, let me start off that I am not a .net developer. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.