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  否则 返回b
  int 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";
  else
    cout << "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;
  }

原因:

 

第六章总结:

第六章结束  内容比较简单 但是也多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@凌晨三点半

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值