C++: I am trying to solve this problem: http://screencast.com/t/5qEYoyMHyo8 This is my code: http://pastebin.com/W46viXhC Can you please tell me why it gives an infinite loop ?
@e.mccormick
@dan815
what exaclty is the error
infinite loop
#include<iostream> using namespace std; void sum() { int n; cout<<"Enter the number"; cin>>n; int sum=0; while(n>0) { sum+=n; --n; } cout<<"\n\nThe sum is"<<sum; } int main() { char ch; do { cout<<"\n \n DO you want to call the function"; cin>>ch; if(ch=='y') { sum(); } } while(ch!='n'); }
@bhaskar.pathk that char ch eval needs some work to catch nNyY. The probkem states all four are needed. @Christos Did you try printing out n inside the loop to see what is happening to it? Or is it the other loop? I did not compile it to see which os going forever.
Join our real-time social learning platform and learn together with your friends!