C++ Primer Plus读书笔记

第五章 循环和关系表达式

1、

2、类别别名:

(1)   #define FLOAT_POINTER float * 

       FLOAT_POINTER pa, pb; 预处理器置换将该声明转换成  float * pa, pb; // pa 是指针,pb就是一个float

(2)  typedef char byte 不会出现上述问题是最佳的选择

3、  cin.get(name, arSize).get() // 输入长度为arSize的name接受换行

// 发送给cin的输入被缓冲。这意味着只有用户按下回车之后,他输入的内容才会发送给程序
所以 # 后面可以跟其他的字符。
// cin 将忽略空格和换行符,统计时候不算空格
   char ch;
    int cnt = 0;
    cin >> ch;
    while(ch != '#')
    {
        cout << ch;
        ++cnt;
        cin >> ch;
// cin.get(ch); 此时就可以读取空格了 } cout << endl << cnt << "characters read\n";

4、文件尾条件

  检测到EOF后,cin将两位(eofbit 和 failbit ) 都设置为1。可以通过成员函数 eof() 来查看 eofbit 是否被设置;如果检测到 EOF ,则 cin.eof() 将返回bool 值true,同样eofbit 和 failbit 被设置成1,则 fail() 成员函数返回 true 

注意,eof() 和 fail() 方法报告最近读取的结果;也就是说,他们在事后报告,而不是预先报告

ctrl + z + 回车 结束

while ( cin.fail() == false )
{

}

 int ch = cin.get() //返回的是一个整数 EOF时为-1

   cin.get(ch) //返回的是 true or false

5、读取数字的循环

int a;
while(cin >> a)  //当输入整形时正常运行,当输入错误例如输入字符或者OEF都导致cin返回false
{
    cout << a << endl;
}

当输入字符出现错误,然后在继续读取

for(int i = 1; i < Max; i++)
{
    cout << "round #" << i + 1 << ": ";
    while (! (cin >> golf[i] ) )
    {
        cin.clear();   // 重新设置输入状态
        while(cin.get() != '\n')  // 将读取的错误内容清除掉
            continue;
        cout << "Please enter a number: ";
     }
}
如果输入失败(即!(cin>>golf[i])表达式的值为true),则进入while循环,然后使用clear重置输入,紧接着cin.get()从错误输入的第一个字符开始依次读取错误字符,直到遇到换行符(注意,这里的换行符是前面cin在从键盘读取读取输入时,在行尾自动添加的),特别注意:这里的内层while循环只包含一条语句,即continue语句,如果测试条件为真,则continue使程序返回到while(cin.get() != 'n')语句,从而达到依次读取字符的目的。当cin.get()读取到换行符时,测试条件为false,内层while不在执行循环体,至此程序使用cin.get()达到了删除整行的作用,此时程序执行下一条语句:cout<<" Please enter a number: ";提示用户重新输入一个数字!

 6、文件的输入输出

写到文本文件当中 头文件fstream

  char automobile[50];
    int year;
    double a_price;
    double b_price;
    ofstream outFile;
    outFile.open("carinfo.txt");
    cout << "Enter the make and model of automible: ";
    cin.getline(automobile, 50);
    cout << "Enter the model year: ";
    cin >> year;
    cout << "Enter the original asking price: ";
    cin >> a_price;
    b_price = a_price * 0.913;
    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 $: " << b_price << endl;
    outFile.close();

 

 

从文件读取:

 char filename[SIZE];
    ifstream inFile;  // 声明ifstream变量
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);  // 调用open() 打开文件
    if(inFile.is_open() == 0)  // 判断是否被成功打开
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);  // cstdlib库函数中定义了一个同操作系统通信的参数值EXIT_FAILURE。函数exit()终止程序
    }
    double value;
    double sum = 0.0;
    int cnt = 0;
    inFile >> value;
    while(inFile.good())  // 判断读取是否正确,EOF 或者格式是否匹配
    {
        ++cnt;
        sum += value;
        inFile >> value;
    }
    //如果结束就分情况判断是哪种情况引起的结束
    if(inFile.eof())   // 遇见EOF
        cout << "End of file reached." << endl;
    else if(inFile.fail())  //类型不匹配
        cout << "Input terminated by data mismatch." << endl;
    else
        cout << "Input terminated for unknown reason." << endl;
    if(cnt == 0)
        cout << "No data processed." << endl;
    else
    {
        cout << "Items read: " << cnt << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << sum / cnt << endl;
    }
    inFile.close(); 

 7、动态分配 结构体数组

struct node
{
    string name;
    double money;
    int flag;
};
int main()
{
    int num;
    cin >> num;
    cin.get(); //接受回车
    node *p = new node[num];  // node类型的指针p
    for(int i = 0; i < num; i++)
    {
        getline(cin, (p + i) -> name);  // 通过指针 + 偏移 
        cin >> (p + i) -> money;
        cin.get();
     }

第七章 函数 -- c++的编程模块

1、为什么需要函数原型: 

 

你可能感兴趣的:(C++ Primer Plus读书笔记)