Question

I need a C++ program like this please! and please follow all instructions! Thanks so much!...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

Add a comment
Know the answer?
Add Answer to:
I need a C++ program like this please! and please follow all instructions! Thanks so much!...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • So Im here and im stuck. I need to figure out how to get the character...

    So Im here and im stuck. I need to figure out how to get the character of the first name to be printed on a separate line and the middle and last. is there a way where i can tell a my function to stop once it hit " "? Please help. I #include iostream 2 #include string using namespace i; string name 7 getline cin, name 4 int main ) cout<"Please enter your full name: " problem3.cpp 3Write a...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Help. Write a C++ program that determines which of a company's four divisions (North, South, East,...

    Help. Write a C++ program that determines which of a company's four divisions (North, South, East, West) has the greatest sales for the quarter. It should include three functions at a minimum: float getSales() is passed the name of a division. It asks the user for a division's quarterly sales figure, calls a validate() function that will validate the input, and then returns the value back to main(). This function should be called once for each division. void validate() is...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • C++ Hey, I really need help with this lab. I don't understand the function part at...

    C++ Hey, I really need help with this lab. I don't understand the function part at all. It'll be great way for me to understand. Than you Functions and loops: Write a program that will ask the user which type of triangle they would like to draw on the characters. The choices are screen with left-top-big left-bottom-big right-top-big right-bottom-big Get the user's choice, ask how big it should be, and then draw the shape. After each successful drawing, ask the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT