Answer to Question #30260 in C++ for tindoh

Question #30260
Write a program that prompts the user to enter in an unlimited number of colors. Test to make sure that the user did not already enter a color before storing it (i.e. “We’re sorry, but you’ve already added the color ‘yellow’”). When the user types the string “exit” you should stop collecting colors. The print out the colors entered in alphabetical order.
1
Expert's answer
2013-05-14T08:30:16-0400
#include <set>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

set<string> a;

int main()
{
string s;
cin>>s;
while(s!="exit")
{
if(a.find(s)==a.end()) a.insert(s); else
cout<<"We're sorry, but you've already added the color '"<<s<<"'"<<endl;
cin>>s;
}
for(set<string>::iterator it=a.begin(); it!=a.end(); it++)
cout<<*it<<' ';
cout<<endl;
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS