#include <iostream>
#include <limits>
#include <string>
usingnamespace std;
int throw_off_bridge()
{
cout << "Auuuuuuuugh!\n";
return 1;
}
int main()
{
string name, color;
int age;
cout << "Halt!\nWhat is your name? ";
getline( cin, name );
cout << "What is your age? "; // yes, but 'quest' takes a string...
cin >> age;
if (cin.get() != '\n') // get rid of that pesky newline
return throw_off_bridge(); // (unless the user didn't input a proper number)
cout << "What is your favorite color? ";
getline( cin, color );
if (color != "yellow")
return throw_off_bridge();
cout << "Right. Off you go.\n";
return 0;
}
Hope this helps.
PS. Please edit that [\code] tag in your first post.
Another Q.
Is there a way to make a search option in there? (This is partly for my parents' benefit, and dear ol' Dad wants a search option.) I read about find_first_of, but I'm not sure where to put it/ how to do it. Any ideas(again)?
#include<iostream>
#include<fstream>
#include<limits>
usingnamespace std;
int main()
{
string c, s, t, u, b;
bool a = true;
char d;
system("TITLE Address Book Maker");
for(int i = 0; i < 1; i++)
{
cout << "Welcome to the Address Book Maker!\nRules and Help Regarding Filenames\n 1. When naming a file you MUST put \".txt\" at the end.\n 2. To load a previous address book, type the EXACT name and put \".txt\" at the\nend(unless already there).\n";
cout << "Ok! Now, hit ENTER to start.";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
system("cls");
cout << "What will you call your Address Book file/What Address Book will you load?\n";
cin >> c;
cout << "Excellent! ";
do
{
cout << "Now, enter a person's first and last name. ";
cin >> s >> t;
cout << "Input their full address (ex. 123 Road Rd. Boston, MA (insert zip code here)). ";
getline (cin, u);
getline (cin, u);
cout << "Enter their phone number: ";
cin >> b;
ofstream streamone(c.c_str(), ios::app);
streamone << s << " " << t << "'s address is" << endl;
streamone << u << " and his phone number is" << endl;
streamone << b << "." << endl << endl;
cout << "The program will show this:\n" << s << " " << t << "'s address is" << endl << u << " and his phone number is" << endl << b << "." << endl << endl;
do{
cout << "Restart? (y or n) ";
cin >> d;
if(d == 'y' || d == 'Y')
continue;
elseif(d == 'n' || d == 'N')
a = false;
}while (d != 'y' && d != 'Y' && d != 'n' && d != 'N');
streamone.close();
}while(a);
}
return 0;
}
note: I like the way I did the getline, so nyeh. This isn't commercial(yet).
This is as advanced as I've gone, and I'm afraid I'll screw up so badly, that I can't think of a way out of that. D:
Where is a good place to put this on the Internet?
Also, I want to concatenate the search engine to the Address Book Maker using an if-else statement. Where should it go?
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
typedef vector <string> name_list;
void show_menu()
{
cout << "\nWhat would you like to do?\n\n"
<< "add names\n"
<< "delete a name\n"
<< "list names\n"
<< "quit\n\n"
<< "> ";
}
string read_name()
{
string name;
if (getline( cin, name ) and !name.empty())
{
string s = name;
// make sure the name is in "given-name(s) surname(s)" order.
int i = name.find( ',' );
if (i != string::npos())
{
s = name.substr( i +1 ) +' ' +name.substr( 0, i );
}
}
// get rid of extra spaces
stringstream ss( s );
string piece;
name.clear();
while (ss >> piece)
{
if (!name.empty()) name += ' ';
name += piece;
}
return name;
}
void add_names( name_list& result )
{
string name;
cout << "\nPlease enter your friends names, one per line.\n"
<< "Press ENTER twice to finish.\n";
while (true)
{
// get a name
name = read_name();
if (name.empty()) break;
// add it to the end of our list
result.push_back( name );
}
// Keep it sorted
sort( result.begin(), result.end() );
}
void delete_name( name_list& names )
{
cout << "\nEnter the name of the friend you wish delete\n"
<< "or just press ENTER to abort.\n\n"
<< "> ";
string name = read_name();
name_list::iterator i = find( names.begin(), names.end(), name );
if (i != names.end())
{
names.erase( i );
}
}
void list_names( name_list& names )
{
cout << "\nYour friends are:\n";
for (unsigned n = 0; n < names.size(); n++)
cout << setw( 2 ) << right << n << ": " << names[ n ] << endl;
}
int main()
{
string user_input;
name_list names;
bool done = false;
cout << "Welcome to the friend counter.\n"
show_menu();
while (!done and getline( cin, user_input ))
{
switch (toupper( user_input[ 0 ] ))
{
case'A': add_names( names ); break;
case'D': delete_name( names ); break;
case'L': list_names( names ); break;
case'Q': done = true; break;
default:
cout << "What? Try again> ";
}
}
cout << "Good bye.\n";
return 0;
}