c++ - does not continue reading input from the user? -


when run program input 50000, output stops @ 633 instead of 50000, why , how fix !??

 int main() {      long int n;       cin>>n; //n = input = 50000       double* r = new double[n];       for(long int i=0;i<n;i++)     {         cin>>r[i]; // each value in range 6 digits          cout<<i<<"  "<<r[i]<< endl; //i should stops @ 49999     }      return 0; } 

you don't check result of

cin>>r[i]; 

so happen cin goes fail state after getting invalid input. hit situation, no more data can retrieved cin until cin.clear() called.

you should have code like

if(!(cin >> r[i])) {     cout << "invalid input, please try again" << endl;     --i;     cin.clear(); } 

to handle this.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -