[THINKING IN C++]CHAPTER 03:EXERCISE 03

// :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 ;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值