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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <sstream>
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
template <class type>
bool good_input(type & item, const string & thestring);
bool good_input(string & item, const string & thestring);
int anumber;
char acharacter;
string astring;
class word
{
private:
string data;
public:
word()
{
data = "";
}
word(const word & w)
{
data = w.data;
}
~word()
{}
const char * c_str()
{
return data.c_str();
}
friend istream &operator>>(istream &in, word & w)
{
string temp = w.data;
in >> w.data;
int size = w.data.size();
for (int i = 0; i < size; ++i)
if (!isalpha(w.data[i]))
{
w.data = temp;
}
}
friend bool & operator >>(stringstream &in, word &w)
{
word temp = w;
in >> w.data;
int size = w.data.size(), i = 0;
for (i = 0; i < size; ++i)
if (!isalpha(w.data[i]))
{
w = temp;
//return 0;
}
//return i;
}
friend ostream & operator<<(ostream & out, const word & w)
{
out << w.data;
}
} aword;
int main()
{
string holder = "";
short start = 0;
while(1)
{
switch(start)
{
case 0:
printf("Enter a number.\n>");
getline(cin, holder);
if (!good_input(anumber, holder))
{
--start;
printf("\n");
}
else printf("\nYou typed: %d\n", anumber);
break;
case 1:
printf("\nEnter a character.\n>");
getline(cin, holder);
if (!good_input(acharacter, holder))
--start;
else printf("\nYou typed: %c\n", acharacter);
break;
case 2:
printf("\nEnter a word.\n>");
getline(cin, holder);
if (!good_input(aword, holder))
--start;
else printf("\nYou typed: %s\n", aword.c_str());
break;
case 3:
printf("\nEnter a string.\n>");
getline(cin, holder);
if (!good_input(astring, holder))
--start;
else { start = -1; /*printf("\nYou typed: %s\n\n", astring);*/ cout << "You typed: " << astring << "\n\n"; }
//}
break;
default: break;
}
++start;
}
return 0;
}
template <class type>
bool good_input(type & item, const string & thestring)
{
type tempitem = item;
stringstream tempstream(thestring);
string junk = "", garbage = "";
if (tempstream >> item)
{
if ( (tempstream >> junk) )
{
while( tempstream >> garbage)
{
junk += " ";
junk += garbage;
//cout << "garbage = " << garbage << endl;
//cout << "junk = " << junk << endl;
}
item = tempitem;
return false;
}
else return true;
}
else
{
item = tempitem;
return false;
}
}
bool good_input(string & item, const string & thestring)
{
string tempitem = item;
stringstream tempstream(thestring);
string junk = "", garbage = "";
if (tempstream >> item)
{
if ( (tempstream >> junk) )
{
while( tempstream >> garbage)
{
junk += " ";
junk += garbage;
}
item += ' ';
item += junk;
return true;
}
else return true;
}
else
{
item = tempitem;
return false;
}
}
|