I need a C++ program like this please! and please follow all instructions! Thank
ID: 3841741 • Letter: I
Question
I need a C++ program like this please! and please follow all instructions! Thanks so much!
A menu-driven program with 3 choices: Names Rearranger, Number Validator, or Exit
Menu Option 1: Name Rearranger:
1. Input ONE STRING from the user containing first name, middle name, and last name. ie, "John Allen Smith". USE THE GETLINE function. (Not cin)
2. Loop through the string and validate for a-z or A-Z or . If the name has any other characters, then ask the user to input again, repeat until valid input has been input.
3. Loop through the string and throw out extraneous blanks, ie "John Allen Smith" should become "John Allen Smith".
4. Create a third string with the names arranged as last first middle. ie, "Smith, John Allen".
5. Create a professional looking display with both the original input from the user, the name without blanks, and the name rearranged.
Menu Option 2: Number Validator:
This function will be used to validate numbers for improper characters, ie, 345.bb, which, if you input into a double would cause your program to crash.
1. Input ONE STRING from the user that represents a money value, ie, $345.55 USE THE GETLINE function. (Not cin)
2. Strip off any leading $$'s.
3. Loop through the string and validate for 0 - 9 or "." If the string has any other characters, then ask the user to input again, repeat until valid input has been input.
4. When you finally have valid input, convert the string to a double.
5. Display both the original input and the number as a double.
Use the string class functions to accomplish all of this. The string class is discussed in the 2nd half of the chapter.
***Use modular design in your program. Main calls functions. Main does not toil or sweat.
***This is very important Main function should have as little as possible.
Explanation / Answer
#include <iostream>
#include <string.h>
#include<cstddef>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
void menu() {
cout << "Main menu" << endl;
cout << "1. Names Re-arranger" << endl;
cout << "2. Number Validator" << endl;
cout << "3. Exit" << endl;
cout << "Enter Your Choice" << endl;
}
void NameRearranger() {
string name;
//getting a valid input
while (1) {
cout << "Enter your Name(allowed characters are a-z and A-Z and spaces): " << endl;
fflush(stdin);
getline(cin, name);
int flag = 0;
for (int i = 0; i < name.length(); i++) {
if ((!isalpha(tolower(name[i]))) || (name[i] != ' ')) {
flag = 1;
break;
}
}
if (flag == 0)
break;
}
string name2;
int j = 0;
//removing extra spaces
for (int i = 0; i < name.length(); i++) {
if (isalpha(name[i])) {
name2[j] = name[i];
j++;
} else if (name[i] == ' ' && name[(i - 1)] != ' ') {
name2[j] = name[i];
j++;
}
}
//rearranging name
for (int i = name2.length(); i >= 0; i--) {
if (name2[i] == ' ') {
cout << "You Entered: " << endl;
cout << name << endl;
cout << "Name without blanks: " << endl;
cout << name2 << endl;
cout << "rearrange: " << endl;
cout << name2.substr(i);
cout << ", " << name2.substr(0, i);
}
}
}
void NumberValidator() {
string amt, amt1;
while (1) {
cout << "Enter Amount(allowed characters are 0-9 and .): " << endl;
fflush(stdin);
getline(cin, amt);
//removing $
if (amt[0] == '$') {
amt1 = amt.substr(1);
}
//validating number
int flag = 0;
for (int i = 0; i < amt1.length(); i++) {
if ((!isalnum(amt1[i])) || (amt1[i] != '.')) {
flag = 1;
break;
}
}
if (flag == 0)
break;
}
double amt2;
amt2 = atof (amt1.c_str());
cout << "You Entered: " << endl;
cout << amt << endl;
cout << "validated: " << endl;
cout << amt2 << endl;
}
int main() {
int ch;
while (ch != 3) {
menu();
fflush(stdin);
cin>>ch;
switch (ch) {
case 1:
NameRearranger();
break;
case 2:
NumberValidator();
break;
case 3:
cout << "Thank You" << endl;
return 0;
break;
default:
cout << "Invalid Input" << endl;
break;
}
}
return 0;
}
Please comment if you have any issue in understanding or running this program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.