我正在编写一个代码,它要求我忽略注释行(即,以# 符号开头的行)直到行尾。我正在使用 linux 用 c++ 编写代码。例如:如果添加两个数字。
xxx@ubuntu:~ $ ./add
Enter the two numbers to be added
1 #this is the first number
2 #this is the second number
result: 3
所以注释行可以在任何地方。它只需要忽略整行并将下一个值作为输入。
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<< "Enter the two numbers to be added:\n";
while(cin >>a >>b)
{
if (a == '#'|| b == '#')
continue;
cout << "Result: "<<a+b;
}
return 0;
}