CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Oct 2005
    Posts
    20

    Make loop continue inspite of invalid input

    Hi,

    Basically what I want is for the loop to continue asking the user for the Number, minutes, and seconds and to continue summing the minutes and seconds while discarding any invalid user input (which in this case would be negative numbers) and print out an error message . 0 is the sentinel value.

    What i currently have is that when an invalid (<0) value is inserted for Number, and/or minutes, and/or seconds---the main fx calls a void function and exits (return 1)...

    Thanks!

    The following is my code:


    --------------------------

    cout << "Enter thenumber:" << endl;
    cin >> Number;

    while (Number<0)
    {
    IsDataValid_Echo(Number, minutes, seconds);
    return 1; //Function call with 3 arguments
    }

    sum = 0;
    sums = 0;
    count = 1;
    while (Number!=0 && seconds>0 && minutes>0)
    {

    cout << "Enter the number of minutes:" << endl;
    cin >> minutes;
    sum = sum + minutes;

    cout << "Enter the number of seconds:" << endl;
    cin >> seconds;

    sums = sums + seconds;

    while (minutes<0 || seconds<0)
    {
    IsDataValid_Echo(Number, minutes, seconds);
    return 1;
    }

    cout << " number " << Number << ", " << minutes << " and " << seconds << endl;
    cout << "Total time is " << sum << " minutes " << " and " << sums << " seconds." << endl;
    cout << "For the next value," << endl;
    cout << "Enter the number:" << endl;
    cin >> Number;

    count ++;
    }
    Last edited by canton; October 18th, 2005 at 09:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured