C++中switch和枚举结合使用:
#include "iostream"
using namespace std;
// 创建一个枚举类型常量, 实际对应的是0~2
enum {red, orange, yellow};
int main() {
cout << "Enter color code (0~2)";
int code;
cin >> code;
while (code >= red && code <= yellow)
{
switch(code)
{
case red :
cout << "red" << endl;
break;
case orange:
cout << "orange" << endl;
break;
case yellow:
cout << "yellow" << endl;
break;
default :
cout << "unkonw" << endl;
}
cout << "Enter color code (0~2)";
cin >> code;
}
cout << "end" << endl;
return 0;
}
输出结果:
注意c++中switch语句只能对整数值进行处理(java 高版本可以对string进行处理), 而枚举实际也是int型变量, switch将int和枚举进行比较的时候, 会将枚举提升为int, 所以枚举也可进行处理.
再看一个while循环的简单使用
注意while条件判断中的语句写法
#include "iostream"
using namespace std;
const int MAX = 5;
int main() {
int golf[MAX];
cout << "Please enter your golf scores." << endl;
cout << "you must enter " << MAX << " rounds." << endl;
int i;
for (i = 0; i < MAX; i++)
{
cout << "round #" << i + 1 << ": " << endl;
// 如果用户输入的不是int型数值
// cin >> golf[i] 这个表达式由于是放在判断语句中
// 会返回false
while (!(cin >> golf[i]))
{
// 首先清空cin的输入
cin.clear();
// 舍弃掉用户错误的输入
// 通过判断是不是到了输入的末尾判断
while(cin.get() != '\n')
// 通过continue来舍弃输入
continue;
cout << "please enter a number : ";
}
}
double total = 0;
for (int i = 0; i < MAX; i++)
{
total += golf[i];
}
cout << total / MAX << " = average score "
<< MAX << " rounds" << endl;
return 0;
}
运行结果
关于程序需要注意的地方, 注释写的比较明白
cin/cout读写文件
写文件:
简单IO
声明一个ofstream对象, 并将其通文件关联起来后, 就可以像使用cout那样使用它, 所有可用于cout的操作和方法(例如 << , endl, setf()) 都可用于ofstreawm对象.
使用文件输出的主要步骤如下:
1.包含头文件fstream
2.创建一个ofstream对象
3.将该ofstream对象同一个文件关联起来
4.像使用cout那样使用该ofstream对象
看一个例子:
// 向文件中输出内容
#include "iostream"
#include "fstream"
using namespace std;
int main()
{
char automobile[50];
int year;
double a_price;
double d_price;
// 创建一个ofstrem对象
ofstream outFile;
// 和一个文件关联起来
outFile.open("carinfo.txt");
cout << "Enter the make and model of automobile: ";
// 接收一行输入, 最长50个字符, 包含\0
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
d_price = 0.9 * a_price;
// 表示输出浮点数的时候, 使用一般方式而不是科学计数法
cout << fixed;
// 显示几位有效数字, 如果和上面的配合使用, 就表示
// 显示小数点后几位有效数字
cout.precision(2);
// 强制显示浮点数, 小数点后的0
cout.setf(ios_base::showpoint);
cout << "Make and model : " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl;
outFile << fixed;
outFile.precision(4);
outFile.setf(ios_base::showpoint);
outFile << "Make and model : " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
// 关闭输出流
outFile.close();
return 0;
}
看程序输出结果
注意事项:
1.outFile.close() 关闭输出流, 表示用完该文件后将其关闭, 不调用的话在程序正常终止的时候也会关闭
2.outFile.open("carinfo.txt"); 这条语句会在当前目录下 如果文件不存在创建一个该名称的txt文件, 如果文件存在那么会将文件清空
其余的注意事项都在注释里写明白了
接下来看cin从文件中读入数据
// 从文件中读入
#include "iostream"
#include "fstream"
#include "cstdlib" // support for exit()
const int SIZE = 60;
using namespace std;
int main()
{
char filename[SIZE];
// 声明一个file input stream
ifstream inFile;
cout << "Enter name of data file : ";
cin.getline(filename, SIZE);
// 将inFile和一个文件关联起来
inFile.open(filename);
// 判断是否能打开文件
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating. \n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count;
// 获取第一个value
inFile >> value;
// 判断是否是正确的数值, 并且不能是结尾符EOF
while (inFile.good())
{
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reached." << endl;
else if(inFile.fail())
cout << "Input terminated by data mismatch." << endl;
else
cout << "Input terminated for unknow reason.\n" << endl;
if (count == 0)
cout << "No data processed." << endl;
else {
cout << "Items read : " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average : " << sum / count << endl;
}
// 记得关闭输入流
inFile.close();
return 0;
}
看一下输出结果:
其实和cout像文件中输出内容类似, 只不过使用的类和方法不同而已