//
:C03:Solution-03.cpp
/* 3. Write a program that uses a while loop to read words from standard
input (cin) into a string. This is an "infinite" while loop, which you
break out of (and exit the program) using a break statement. For each
word that is read, evaluate it by first using a sequence of if statements
to "map" an integral value to the word, and then use a switch
statement that uses that integral value as its selector (this sequence
of events is not meant to be good programming style; it’s just supposed
to give you exercise with control flow). Inside each case, print something
meaningful. You must decide what the "interesting" words are and what
the meaning is. You must also decide what word will signal the end of
the program. Test the program by redirecting a file into the program’
s standard input (if you want to save typing,this file can be your program’
s source file).
*/
#include < iostream >
#include < string >
#include < fstream >
using namespace std;
int main( int argc, char * argv[])
{
if (argc == 1 ){
cout << " please direct to a .txt file! " << endl;
return - 1 ;
}
string word;
int wordmap;
bool flag = true ;
ifstream file(argv[ 1 ]);
while (flag == true ) {
file >> word;
if (word == " go!go!go! " )
wordmap = 1 ;
else if (word == " alibaba " )
wordmap = 2 ;
else if (word == " quit " )
wordmap = 3 ;
else if (word == " nike " )
wordmap = 4 ;
else
wordmap = 5 ;
switch (wordmap){
case 1 :
cout << " Ah!Le!Ah!Le!Ah!Le! " << endl << endl; break ;
case 2 :
cout << " zhima kai men! " << endl << endl; break ;
case 3 :
cout << " You've quited the program. " << endl << endl;
flag = false ; break ;
case 4 :
cout << " nothing is impossible! " << endl << endl; break ;
case 5 :
cout << " Please type something meaningful. " << endl << endl; break ;
}
}
}
/* 3. Write a program that uses a while loop to read words from standard
input (cin) into a string. This is an "infinite" while loop, which you
break out of (and exit the program) using a break statement. For each
word that is read, evaluate it by first using a sequence of if statements
to "map" an integral value to the word, and then use a switch
statement that uses that integral value as its selector (this sequence
of events is not meant to be good programming style; it’s just supposed
to give you exercise with control flow). Inside each case, print something
meaningful. You must decide what the "interesting" words are and what
the meaning is. You must also decide what word will signal the end of
the program. Test the program by redirecting a file into the program’
s standard input (if you want to save typing,this file can be your program’
s source file).
*/
#include < iostream >
#include < string >
#include < fstream >
using namespace std;
int main( int argc, char * argv[])
{
if (argc == 1 ){
cout << " please direct to a .txt file! " << endl;
return - 1 ;
}
string word;
int wordmap;
bool flag = true ;
ifstream file(argv[ 1 ]);
while (flag == true ) {
file >> word;
if (word == " go!go!go! " )
wordmap = 1 ;
else if (word == " alibaba " )
wordmap = 2 ;
else if (word == " quit " )
wordmap = 3 ;
else if (word == " nike " )
wordmap = 4 ;
else
wordmap = 5 ;
switch (wordmap){
case 1 :
cout << " Ah!Le!Ah!Le!Ah!Le! " << endl << endl; break ;
case 2 :
cout << " zhima kai men! " << endl << endl; break ;
case 3 :
cout << " You've quited the program. " << endl << endl;
flag = false ; break ;
case 4 :
cout << " nothing is impossible! " << endl << endl; break ;
case 5 :
cout << " Please type something meaningful. " << endl << endl; break ;
}
}
}