Update: FREE homework help NOW available ...
You can contact me via:
http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
http://developers-heaven.net/forum/index.php/topic,2587.0.html
You can contact me via:
http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
http://developers-heaven.net/forum/index.php/topic,2587.0.html
An often asked student request is regarding the use of cin in C++
So ...
here is a little demo of using cin in C++
Code Select
// demo_cin.cpp // // 2016-01-26 @ 19:35 //
// a demo of using C++ cin //
#include <iostream>
#include <sstream> // re. stringstream objects //
#include <string>
using namespace std;
char takeInChr( const string& msg )
{
cout << msg;
string reply;
getline( cin, reply );
if( reply.size() )
return reply[0];
// else ...
return 0;
}
bool more( const string& text = "" )
{
char reply = takeInChr( "More " + text + "(y/n) ? " );
if( reply == 'n' || reply == 'N' )
return false;
// else ...
return true;
}
int main()
{
string dummy;
do
{
cout << "Enter a char: ";
char c;
cin >> c; // extract first char //
/*
above, skips over any leading whitespace and reads first (non-whitespace) char entered.
note: leaves any next char like the '\n' char still in the cin (in) stream
*/
cout << "You entered: '" << c << "'\n";
getline( cin, dummy ); // clean up any stuff and '\n' whitespace left over //
if( dummy.size() != 0 ) cout << "You also entered '" << dummy << "'\n";
}
while( more( "characters " ) ) ;
do
{
cout << "Enter an integer: ";
int val;
cin >> val;
/*
above, skips over any leading whitespace and expects first non-whitespace entered to be a 0..9 or a + or -
reads all next 0..9's and stops at first non-0..9
Note: cin flags are set if good data or bad data encountered.
Note also: cin leaves all next char like the '\n' char still in the cin (in) stream
Thus if you entered -123abc 456
above and then pressed the Enter key,
val holds -123 and calling cin.good() would then return true,
but abc 456 and the '\n' whitespace char would still all be in the cin stream
*/
if( cin.good() )
{
cout << "You entered: '" << val << "'\n";
getline( cin, dummy ); // 'eat' whitespace '\n' left in cin stream //
if( dummy.size() != 0 ) cout << "You also entered: '" << dummy << "'\n";
}
else
{
cin.clear(); // firstly, clear (all) cin error flags //
getline( cin, dummy );
cout << "Error! Was expecting a *valid* integer, but '" << dummy << "' was encountered.\n";
}
}
while( more( "integers " ) );
while( true )
{
// Safe data entry ... uses ... as per the following ...
cout << "Enter your ID number, a space and then your name(s): ";
string line;
// get the whole line into 'line' and 'eat' the '\n' char(s) at the end //
getline( cin, line );
istringstream iss( line ); // form 'iss' object from string obj. 'line' //
// then parse out the data you expect from the iss object ...
int id;
string name;
if( iss >> id && id > 0 ) // accept ONLY positive ID's here //
{
string word;
while( iss >> word )
name += word + ' ';
if( name.size() != 0 )
{
name.erase( name.size()-1 ) ; // erase last ' ' char added //
cout << "You entered '" << id << ' ' << name << "'\n";
if( !more( "lines to parse " ) ) break;
}
else
cout << "You forgot to enter any names ... try again.\n";
}
else
cout << "Error! Was expecting a *valid* positive integer ... followed by name(s).\n"
<< "Try again ...\n";
}
}