第六章 复习题

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     char ch;
 6     int ct1,ct2;
 7     ct1=ct2=0;
 8     while((ch=cin.get())!='$')
 9     {
10         cout<<ch;
11         ct1++;
12         if(ch='$')ct2++;
13         cout<<ch;
14     }
15     cout<<"ct1="<<ct1<<",ct2="<<ct2<<endl;
16     return 0;
17 }

输入

hi!

send $10 or $20 now!

输出结果是

hi!

h$i$!$

$send $10 or $20 now!

$$s$e$n$d$ $ct1=9,ct2=9

因为第12行是ch='$'而不是ch=='$'。是赋值而不是判断。

原文地址:https://www.cnblogs.com/taoxiuxia/p/4149843.html