I am a newbie to C++ and I'm not sure of anything at this point in time however,I got a problem with the starting phase of this program that well prompt the user to input a single character and output a inter4national Civil Aviation Word !!!!! The prolem that I have is that you have to enter a character before it will start and it closes after just one entry I don't see what is wrong!!!! Can somebody Help
Thanks for your time

#include "stdafx.h"
# include <iostream>

using namespace std;

int main()
    
{
    int a;
    char letter;
    cin>>letter;

    cout << "Enter a single capitalize character, Example A:";
    cin >> letter;
    
    switch (letter)
{
  case 'A': cout << "ALPHA";      // do something
            break;
  case 'B': cout << "Bravo";      // do something
            break;
  case 'C': cout << "Charlie";
            break;
  case 'D': cout << "Delta";
            break;
  case 'E': cout << "Echo";
            break;
  case 'F': cout << "Foxtrot";
            break;
  case 'G': cout << "Gulf";
            break;
  case 'H': cout << "Hotel";
            break;
  case 'I': cout << "India";
            break;
  case 'J': cout << "Juliet";
            break;
  case 'K': cout << "Kilo";
            break;
  case 'L': cout << "Lima";
            break;
  case 'M': cout << "Mike";
            break;
  case 'N': cout << "November";
            break;
  case 'O': cout << "Oscar";
            break;
  case 'P': cout << "Papa";
            break;
  case 'Q': cout << "Quebec";
            break;
  case 'R': cout << "Romeo";
            break;
  case 'S': cout << "Sierra";
            break;
  case 'T': cout << "Tango";
            break;
  case 'U': cout << "Uniform";
            break;
  case 'V': cout << "Victor";
            break;
  case 'W': cout << "Whiskey";
            break;
  case 'X': cout << "X-ray";
            break;
  case 'Y': cout << "Yankee";
            break;
  case 'Z': cout << "Zulu";
            break;
  default : cout << "Error";
}

    
    cin >> a;

    return 0;
}

Dani AI

Generated

— two simple causes explain what you describe: the program reads input before it shows the prompt (there is an extra read at the top of main), and the program only runs once because there is no loop to accept repeated entries. Fixing those two items will make the behavior match your expectation.

A practical approach:

  • Remove the stray early read so the prompt is shown before anything is consumed.
  • Read a full line with std::getline (this keeps leftover newlines out of the stream) and take the first character from that string.
  • Put the prompt + read + lookup inside a loop so the user can enter letters repeatedly; use a sentinel (e.g., 'Q' or an empty line) to quit.
  • For the final “pause” when running from Explorer, avoid fragile tricks; run from a console or use a single std::cin.get() if you need a pause (portable and simple).

Example pattern (replace the placeholder array with the 26 NATO words):

#include <iostream>
#include <string>
#include <array>
#include <cctype>

int main() {
  std::array<std::string,26> nato = {/* ALPHA, BRAVO, ... ZULU */};
  std::string line;
  while (true) {
    std::cout << "Enter a capital letter (or Q to quit): ";
    if (!std::getline(std::cin, line) || line.empty()) break;
    char c = std::toupper(static_cast<unsigned char>(line[0]));
    if (c == 'Q') break;
    if (c < 'A' || c > 'Z') { std::cout << "Error\n"; continue; }
    std::cout << nato[c - 'A'] << '\n';
  }
  return 0;
}

Notes on replies here: was right to suggest a loop, but do not compare the stream object directly to a character — check the character or the stream state instead. ’s advice to use getline is sound. If compiling with Visual C++ and precompiled headers enabled, keep stdafx.h; otherwise disable precompiled headers or remove that include.

Recommended Answers

All 4 Replies

Hi wheelz,
You should check the input with While loop.It's something like this:

while((cin >> letter) != '0')
{
       Your switch statement;
       And so... .;
}

Good luck.

commented: Succinct and to the point - Salem +2

I don't think I understand the problem.

I am a newbie to C++ and I'm not sure of anything at this point in time however,I got a problem with the starting phase of this program that well prompt the user to input a single character and output a inter4national Civil Aviation Word !!!!!

So you prompt the user to enter a character.

The prolem that I have is that you have to enter a character before it will start...

Ummm, isn't this the definition of "prompt the user to input a single character"? Why would this be a problem?

... and it closes after just one entry

Bottom of the program:

case 'Z': cout << "Zulu";
            break;
  default : cout << "Error";
}

cin >> a;

    return 0;
}

Output the information, accept a character (which reads the [enter] from above -- remember, you entered a letter AND the ENTER key) and exit. The only problem I see is the program probably doesn't wait at the end. Change the top cin to getline to keep the input stream clean, read it into a string and move the first character of the string to letter and continue.

Also, #include "stdafx.h" i not necessary.

>>Also, #include "stdafx.h" i not necessary

It is with VC++ compilers and have precompiled headers turned on.

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.