本文主要是介绍C++ Print Plus 学习笔记 第六章 分支、逻辑表达式、cctype、 三元运算 switch 类型错误的文本处理 和I/O,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
if ifelse ifelseif 这三个 都不说了
有一个要留意
关于逻辑表达式要留意的:
|| 运算顺序是先算左边再算右边 如果左边为true 那他就不管右边了 。直接返回true
cctype:
三元运算:
#include <iostream>
int main()
{using namespace std;int a, b;cout << "Enter two integers: ";cin >> a >> b;cout << "The larger of " << a << " and " << b;
// 如果是true 就返回a 否则 返回bint c = a > b ? a : b;cout << " is " << c << endl;return 0;
}
关于switch:
每个case 中如果没带break 的话 就会从被跳转到的case中 一直执行下去 ,执行完switch中的所有语句 所以 所有的case中一定要带有break语句
关于 cin>> 输入错误类型的处理:
比如 int型变量 被输入了字符类型时的处理:
#include <iostream>
const int Max = 5;
int main()
{using namespace std;int golf[Max];cout << "Please enter your golf scores.\n";cout << "You must enter " << Max << " rounds.\n";int i;for (i = 0; i < Max; i++){cout << "round #" << i + 1 << ": ";
// ---------这里while (!(cin >> golf[i])) {cin .clear();while (cin.get() != '\n')continue;cout << "Please enter a number: ";}
// -----------}double total = 0.0;for (i = 0; i < Max; i++)total += golf[i];cout << total / Max << " = average score "<< Max << " rounds\n";return 0;
}
文件I/O
文件输入:
代码示例:
#include <iostream>
#include <fstream>int main()
{using namespace std;char automobile[50];int year;double a_price;double d_price;ofstream OutFile;
// 如果文件不存在就创建 如果存在 将会清空文件内容(真变态)。OutFile.open("carinfo.txt");cout << "Enter the make and model of automobile: ";cin.getline(automobile, 50);cout << " Enter the model year: ";cin >> year;cout << " Enter the original asking price: ";cin >> a_price;d_price = 0.913 * a_price;cout << fixed;cout.precision(2);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(2);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;
}
carinfo.txt内容展示:
读取文件:
示例代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE = 60;
int main()
{using namespace std;char filename[SIZE];ifstream inFile;cout << "Enter name of data file: ";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Could not open the file " << filename << endl;cout << "Program terminating.\n";
// #include <cstdlib>
// 程序退出exit(EXIT_FAILURE);}double value;double sum = 0.0;int count = 0;inFile >> value;while (inFile.good()){++count;sum += value;inFile >> value;}if (inFile.eof())cout << "End of ile reached.\n";else if (inFile.fail())cout << "Input terminated by data mismatch.\n";elsecout << "Input terminated for unknown reason.\n";if (count == 0)cout << "No data processed.\n";else{cout << "Items read: " << count << endl;cout << "Sum: " << sum << endl;cout << "Average: " << sum / count << endl;}inFile.close();return 0;
}
我就碰到这种情况 我使用VSCode 最后一行没有回车 结果 最后一行的数据死活没读取到
关于good()
inFile >> value;while (inFile.good()){++count;sum += value;inFile >> value;}
可以精简为
while (inFile >> value){++count;sum += value;}
原因:
第六章总结:
第六章结束 内容比较简单 但是也多。
这篇关于C++ Print Plus 学习笔记 第六章 分支、逻辑表达式、cctype、 三元运算 switch 类型错误的文本处理 和I/O的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!