// Write a program to calculate the addition of integers and print the output.As we //are taking integer as input from user we don't know exact number of inputs he/she is //going to press. // Input Format: // line 1: Integers delimited by space // sample input: 3 4 5 6 7 // sapmle output: 25 // sample input: 3 5 a b 7 //sapmle output: Invalid Input // Now check what i have tried you will get real problem what i meant to say #include<iostream> #include<string> #include<cstdlib> using namespace std; int main() { string temp; bool enter = false; bool bad_ip = false; int sum = 0; int a = 0; while(!enter) { getline(cin,temp); if((cin >> a).fail()) { bad_ip = true; cout << "Invalid Input" << endl; cin.clear(); exit(1); } sum += a; if(temp.empty()) // i can't compare with newline or enterkey { cout << sum; enter = true; break; } } return 0; }
istringstream
#include <iostream> #include <sstream> using namespace std; int main() { string s; int a; for (;;) { getline(cin,s); if ( s.empty()) break; istringstream iss(s); iss >> a; cout << a << endl; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)