第六章
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char ch; 6 int spaces=0,total=0; 7 cin.get(ch); 8 while(ch !='.')//注意使用单引号。 9 { 10 if(ch ==' ')spaces++; 11 total++; 12 cin.get(ch); 13 } 14 cout<<spaces<<" sapces, "<<total<<" characters total in sentence"<<endl; 15 return 0; 16 }
||和&&
1 #include<iostream> 2 using namespace std; 3 const int size=6; 4 int main() 5 { 6 float a[size]; 7 cout<<"enter value,when you make "<<size<<" enteries or enter a negetive"<<endl; 8 int i=0; 9 float temp; 10 cout<<"frist value"; 11 cin>>temp; 12 while(i<size&&temp>=0) 13 { 14 a[i]=temp; 15 i++; 16 if(i<size) 17 { 18 cout<<"next value:"; 19 cin>>temp; 20 } 21 } 22 if(i==0)cout<<"no data"<<endl; 23 else 24 { 25 cout<<"enter your naaq"; 26 float you; 27 cin>>you; 28 int count=0; 29 for(int j=0;j<i;j++)if(a[j]>you)count++; 30 cout<<count; 31 cout<<" of your neighbors have greater awarense of the new age then you do"; 32 } 33 return 0; 34 }
||是顺序点,也就是说,先修改左侧的值 ,在对右侧的值进行判定。
如 i++<||i==j;
&&也是顺序点。
?运算符:
x=(x>y)?x:y;
switch语句
1 switch(interger-expression) 2 { 3 case label1:statement(a); 4 case label2:statement(b); 5 … 6 case labeln:statement(n); 7 default:statement(s); 8 }
interger-expression为一个结果为整数值的表达式。
每个标签都必须是整数。
依次执行之后的所有语句,可以使用break停止。
1 #include<iostream> 2 using namespace std; 3 void showmenu(); 4 void report(); 5 void comfort(); 6 int main() 7 { 8 char choice; 9 cin>>choice; 10 while(choice!='q'&&choice!='Q') 11 { 12 switch(choice) 13 { 14 case 'a': 15 case 'A':cout<<"a and A"; 16 break; 17 case'b': 18 case'B':cout<<"b and B"; 19 break; 20 case'c': 21 case'C':cout<<"c and C"; 22 break; 23 case'd': 24 case'D':cout<<"d and D"; 25 break; 26 default:cout<<"that is not a choice"<<endl; 27 } 28 cin>>choice; 29 } 30 return 0; 31 }
将枚举量用作标签
当switch语句将int值与枚举标签进行比较时,将枚举量提升为int。
1 #include<iostream> 2 using namespace std; 3 enum{red,orange,yellow,green,blue,violet,indigo}; 4 int main() 5 { 6 cout<<"enter color code(1-6)"; 7 int code; 8 cin>>code; 9 while(code>=red&&code<=indigo) 10 { 11 switch(code) 12 { 13 case red:cout<<"red"<<endl;break; 14 case orange:cout<<"orange"<<endl;break; 15 case yellow:cout<<"yellow"<<endl;break; 16 case green:cout<<"green"<<endl;break; 17 case blue:cout<<"blue"<<endl;break; 18 case violet:cout<<"violet"<<endl;break; 19 case indigo:cout<<"indigo"<<endl;break; 20 } 21 cout<<"enter the code(0-6)"; 22 cin>>code; 23 } 24 return 0; 25 }
break和continue语句
continue跳过循环体剩余的部分,开始新一轮的循环,
break跳出循环。
1 #include<iostream> 2 const int size=80; 3 int main() 4 { 5 using namespace std; 6 char line[size]; 7 int spaces=0; 8 cout<<"enter a line of text"; 9 cin.get(line,size); 10 cout<<"complete line"<<endl<<line<<endl; 11 cout<<"line throught first today"<<endl; 12 for(int i=0;line[i]!='\n';i++) 13 { 14 cout<<line[i]; 15 if(line[i]=='.')break; 16 else if(line[i]!=' ')continue; 17 else spaces++; 18 } 19 cout<<endl<<spaces<<" spaces"<<endl; 20 return 0; 21 }
clear()方法重置错误输入标记,同时也重置文件尾。
假设要将一系列数字读取到数组中,并允许在数组填满之前结束输入。可以使用cin;
但是万一用户输入的是单词呢?
下面的程序如果数组被填满或者输入非数字输入,程序结束。
1 #include<iostream> 2 const int Max=5; 3 int main() 4 { 5 using namespace std; 6 double fish[Max]; 7 cout<<"enter the weight of your fishes"<<endl; 8 cout<<"you may enter uo to "<<Max<<" fishes<q to terminate>"<<endl; 9 cout<<"fish #1"; 10 int i=0; 11 while(i<Max&&cin>>fish[i])//cin位于测试条件中被转换成bool型,如果输入成功返回true,否则返回false 12 { 13 if(++i<Max)cout<<"fish #"<<i+1<<":"; 14 } 15 double total=0; 16 for(int j=0;j<i;j++)total+=fish[j]; 17 if(i==0)cout<<"no fish"<<endl; 18 else cout<<total/i<<"= average weight of "<<i<<" fish"<<endl; 19 return 0; 20 }
下面程序是输入错误继续输入的。
1 #include<iostream> 2 const int Max=5; 3 using namespace std; 4 int main() 5 { 6 int golf[Max]; 7 cout<<"enter thegolf scores"<<endl; 8 cout<<"you must enter "<<Max<<" round"<<endl; 9 int i; 10 for(i=0;i<Max;i++) 11 { 12 cout<<"round #"<<i+1<<":"; 13 while(!(cin>>golf[i])) 14 { 15 cin.clear(); 16 while(cin.get()!='\n')continue;//读取行尾之前的所有输入,从而删除这一行的错误输入。 17 cout<<"enter a number"; 18 } 19 } 20 double total=0; 21 for(i=0;i<Max;i++) 22 total+=golf[i]; 23 cout<<total/Max<<"=average score "<<Max<<" round"<<endl; 24 return 0; 25 }
写入到文本文件中
头文件fstream
自己声明ofstream变量(对象)
使用命名空间std;使用完文件后要关闭close()
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 int main() 5 { 6 char automobile[50]; 7 int year; 8 double a_price; 9 double b_price; 10 11 ofstream outfile; 12 outfile.open("carinfo.txt"); 13 14 cout<<"enter the make and model of automobile:"; 15 cin.getline(automobile,50); 16 cout<<"enter the model year"; 17 cin>>year; 18 cout<<"enter the original asking price:"; 19 cin>>a_price; 20 b_price=0.913*a_price; 21 22 cout<<fixed; 23 cout.precision(2);//精度 24 cout.setf(ios_base::showpoint); 25 cout<<"make and model:"<<automobile<<endl; 26 cout<<"year:"<<year<<endl; 27 cout<<"was asking $"<<a_price<<endl; 28 cout<<"was asking $"<<b_price<<endl; 29 30 outfile<<fixed; 31 outfile.precision(2); 32 outfile.setf(ios_base::showpoint); 33 outfile<<"make and model:"<<automobile<<endl; 34 outfile<<"year:"<<year<<endl; 35 outfile<<"was asking $"<<a_price<<endl; 36 outfile<<"was asking $"<<b_price<<endl; 37 38 outfile.close(); 39 return 0; 40 }
声明一个ofstream对象,然后使用open()将其与特定对象关联起来。
读取文本文件
可以使用is_open()检查文件是否被成功打开。
1 inFile.open(“bowling.txt”); 2 if(!inFile.is_open()) 3 { 4 exit(EXIT_FAILURE); 5 }
程序例
1 #include<iostream> 2 #include<fstream> 3 #include<cstdlib> 4 const int SIZE=60; 5 using namespace std; 6 int main() 7 { 8 char filename[SIZE]; 9 ifstream infile; 10 cout<<"enter name of data file "; 11 cin.getline(filename,SIZE); 12 infile.open(filename); 13 if(!infile.is_open()) 14 { 15 cout<<"coule not open the file"<<filename<<endl; 16 cout<<"program terminating"<<endl; 17 exit(EXIT_FAILURE); 18 } 19 double value; 20 double sum=0; 21 int count =0; 22 infile>>value; 23 while(infile.good()) 24 { 25 count++; 26 sum+=value; 27 infile>>value; 28 } 29 if(infile.eof())cout<<"end of file reached"<<endl; 30 else if(infile.fail())cout<<"input terminated by data mismatch"<<endl; 31 else cout<<"input terminated for unknow reason"<<endl; 32 if(count==0)cout<<"no data"<<endl; 33 else 34 { 35 cout<<"items read "<<count<<endl; 36 cout<<"sum"<<sum<<endl; 37 cout<<"average "<<sum/count<<endl; 38 } 39 infile.close(); 40 return 0; 41 }
方法good()
在没有任何错误时返回true。