在c++中定义宏的麻烦

Troubles with defining a macro n C++

本文关键字:麻烦 定义 c++      更新时间:2023-10-16

我想测试一下老师给我们的宏,不知道为什么我一直收到错误信息。有人知道为什么吗?我似乎做不到想办法。

#include <iostream>
#include <string>
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;

int main()
{
    int x;
    for(;;)
    {
        cout <<"How are you: " <endl;
        cout <<"1) goodn"; 
        cout <<"2) badn";
        cin >> x;
        if(x != 1 || x != 2)
            die("Invalid input");
    }

    return(0);
}
cout <<"How are you: " <endl;

在第二个<<中漏掉了一个<

检查这个循环:

    for(;;)
    {
        cout << "How are you: " < endl;
    }

std::endl之前少了一个<

    for(;;)
    {
        cout << "How are you: " << endl;
    }