> Try to add system("pause")
What!?
http://www.cplusplus.com/forum/articles/11153/
[edit]
> Right idea. Wrong code.
/me Double takes.
What!? What?!!!!
[/edit]
ZHuge already gave you the correct answer. Don't
cin >> mystring;
. Instead #include <string> and use
getline().
(The problem is that you are entering a name with spaces in it, like "Johnny Appleseed". Try again with a one-word name.) See also
http://www.cplusplus.com/forum/articles/6046/
I actually just responded to another thread (
http://www.cplusplus.com/forum/beginner/11402/ ) with a handy little function to help with input. Try it here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <cctype>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
using namespace std;
//----------------------------------------------------------------------------
struct suspect_t
{
string name;
unsigned age;
unsigned height; // in inches
};
//----------------------------------------------------------------------------
template <typename CharType, typename CharTraits>
std::basic_istream <CharType, CharTraits> &
endl( std::basic_istream <CharType, CharTraits> & ins )
{
return ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}
//----------------------------------------------------------------------------
char menu()
{
char selection = '-';
cout << "What would you like to do: \n\n";
cout << " a: Enter New Suspect\n";
cout << " b: View Current Suspects\n";
cout << " c: Complete Crime Details\n";
cout << " d: Add Additional Notes\n";
cout << " e: Generate Conclusion\n";
cout << " q: Quit\n\n";
cout << "Enter your choice: " << flush;
while (true)
{
cin >> selection >> endl;
selection = tolower( selection );
if (string( "abcdefq" ).find( selection ) != string::npos) break;
cout << "Try again: " << flush;
}
return selection;
}
//----------------------------------------------------------------------------
suspect_t add_new_suspect()
{
suspect_t result;
cout << "\n\nAdd New Suspect\n";
while (result.name.empty())
{
cout << "\nName: " << flush;
getline( cin, result.name );
// (you may want to add some processing here, like to get
// rid of any leading and trailing spaces in the name, etc)
}
while (true)
{
cout << "\nAge: " << flush;
cin >> result.age >> endl;
if (cin) break;
cin.clear(); // So if the user entered anything invalid, fix cin
cin >> endl; // and get rid of the offending input
}
while (true)
{
cout << "\nHeight (Inches): " << flush;
cin >> result.height >> endl;
if (cin) break;
cin.clear();
cin >> endl;
}
return result;
}
//----------------------------------------------------------------------------
int main ()
{
bool done = false;
vector <suspect_t> suspects;
cout << "Welcome to Crime Buster v 1.00\n\n";
while (!done)
switch (menu())
{
case 'a':
suspects.push_back( add_new_suspect() );
break;
case 'q':
done = true;
}
return 0;
}
|
You'll notice that I added functions and a way to remember suspects (via a vector and a class).
I added a
switch statement to process menu selections.
I also added a way to
quit -- don't forget that.
There is also some rudementary error handling -- preventing the user from inputting nonsense.
Add the stuff for selection 'b' (View Current Suspects) to see how it worked.
Hope this helps.