Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I prevent users from inputting alphabetic characters? For example, if th

ID: 651224 • Letter: H

Question

How can I prevent users from inputting alphabetic characters? For example, if they inputting "a", it will print out "Invaild input, please enter again"

Below is my codes

******************************************************************************

#include <iostream>
#include <string>
using namespace std;

void writeProverb(int number);// Fill in the prototype of the function writeProverb.


int main()
{

int wordCode;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input any other number for the word country" << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);

return 0;
}

// ******************************************************************************
//                            writeProverb
//  
//   task:      This function prints a proverb. The function takes a number
//              from the call. If that number is a 1 it prints "Now is the time
//              for all good men to come men to come to the aid of their party."
//              Otherwise, it prints "Now is the time for all good men
//              to come to the aid of their country"
//   data in:   code for ending word of proverb (integer)
//   data out: none
//
//   *****************************************************************************


void writeProverb(int number)


{

if (number == 1)

  cout << "Now is the time for all good men to come to the aid of their party" << endl;

else
  cout << "Now is the time for all good men to come to the aid of their country" << endl;

system("pause"); // Fill in the body of the function to accomplish what is described above
}

Explanation / Answer

void writeProverb(int number);// Fill in the prototype of the function writeProverb.


int main()
{

int wordCode;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input any other number for the word country" << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;

if(!cin.good())
   {
      cout<<"Invalid input! Please enter again"<<endl;
      cin.clear();
   }

cout << endl;
writeProverb(wordCode);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote