C++ program help The program I have below allows the user to enter the names of
ID: 3861054 • Letter: C
Question
C++ program help
The program I have below allows the user to enter the names of several local businesses. Sorts the business names and display the results. Continues this process until they are out of business names. Can someone improve it so that it can have the following:
#include <iostream>
#include <string>
#include <list>
using namespace std;
std::string readName ();
void display (list <std::string>);
bool promptContinue ();
int main()
{
std::string Business = "";
list <std::string> Array;
bool Continue = true;
do {
//enter input items
Array.push_back(readName ());
//sort business
Array.sort();
//display business
display (Array);
}while(promptContinue ());
return 0;
} //end of main function
std::string readName ()
{
std::string _Business = "";
cout << "Please enter the name of a business: ";
getline (cin, _Business);
return _Business;
}
void display (list <std::string> _Array)
{
cout << "Your businesses are: " << endl;
int i = 0;
while (i < _Array.size ())
{
cout << _Array.front () << endl;
_Array.push_back (_Array.front ()); // This loops the front of the list to the back this makes it possible to not use an iterator
_Array.pop_front ();
i++;
}
}
bool promptContinue ()
{
std::string prompt = "";
cout << "Another Business? ";
cin >> prompt;
cin.ignore (); // Clear the whitespace leftover from cin
return (prompt == "yes");
}
Explanation / Answer
#include <iostream>
#include <string>
#include <list>
using namespace std;
std::string readName ();
void display (list <std::string>);
bool promptContinue ();
int main()
{
std::string Business = "";
list <std::string> Array;
bool Continue = true;
do {
//enter input items
Array.push_back(readName ());
//sort business
Array.sort();
//display business
display (Array);
}while(promptContinue ());
return 0;
} //end of main function
std::string readName ()
{
std::string _Business = "";
cout << "Please enter the name of a business: ";
getline (cin, _Business);
return _Business;
}
string Capitalize(string str)
{
int flag=1;
for(auto& x: str)
{
if(flag==1)
{
x = toupper(x);
flag=0;
}
if(x=='-')
{
flag=1;
}
}
return str;
}
void display (list <std::string> _Array)
{
cout << "Your businesses are: " << endl;
int i = 0;
while (i < _Array.size ())
{
cout <<Capitalize( _Array.front () )<< endl;
_Array.push_back (_Array.front ()); // This loops the front of the list to the back this makes it possible to not use an iterator
_Array.pop_front ();
i++;
}
}
bool promptContinue ()
{
std::string prompt = "";
cout << "Another Business? ";
cin >> prompt;
cin.ignore (); // Clear the whitespace leftover from cin
return (prompt == "yes");
}
========================
Please enter the name of a business:
akshay
Your businesses are:
Akshay
Another Business?
yes
Please enter the name of a business:
akshay-abhang
Your businesses are:
Akshay
Akshay-Abhang
Another Business?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.