c++ - Ignoring a line beginning with a '#' in command prompt -
i writing code requires me ignore comment lines (i.e, lines beginning # sign) till end of line. i'm using linux code in c++. e.g: in case of adding 2 numbers.
xxx@ubuntu:~ $ ./add enter 2 numbers added 1 #this first number 2 #this second number result: 3
so comment line can anywhere. has ignore entire line , take next value input.
#include <iostream> using namespace std; int main() { int a,b; cout<< "enter 2 numbers added:\n"; while(cin >>a >>b) { if (a == '#'|| b == '#') continue; cout << "result: "<<a+b; } return 0; }
from have shown, think might want.
int main() { string comment; int nr1,nr2; // read first number. should first 1 always. no comment before number! cin >> nr1; // see if can read second number successfully. means integer. if(cin >> nr2) { } // otherwise clear cin , read rest of comment line else { cin.clear(); getline(cin,comment); // read second number second line cin >> nr2; } // read rest of second line. getline(cin,comment); cout << "result: " << nr1 + nr2 << endl; return 0; }
Comments
Post a Comment