mardi 5 juillet 2016

How does cin detect eol vs an invalid character

Problem :

Write a program to calculate the addition of integers and print the output.

Input Format:

Line 1: Integers delimited by space

Output:

The output consists of a single integer which corresponds to addition, followed by a new line. Line 1 addition of numbers

Sample Input and Output:

Input 1:

3 4 5 6 7

Output 1:

25

Input 2 :

3 5 a b 7

Output:

Invalid Input


So , i've tried many approaches , but nothing seems to work

This was my last attempt before i gave up

#include <iostream>

using namespace std;
int main(){

    int i;
    int sum=0;

     cin>>i;
      while(isdigit(i)){

        sum+=i;
       cin>>i;
      }
      if(!isdigit(i)) cout<<"Invalid Input";
      else cout<<sum;


  return 0;
}

Aucun commentaire:

Enregistrer un commentaire