0
int main()
{

    cout << "The maximum number of elements in an array is 100. " << endl;
    const int MAX = 100;
    double myarray[MAX];
    int arraysize = Fill_Array(myarray, MAX);
    Show_Array(myarray, arraysize);
    Reverse_Array(myarray, arraysize);
    Show_Array(myarray, arraysize);
    Reverse_Positions(myarray, arraysize);
    Show_Array(myarray, arraysize);
    return 0;
}

int Fill_Array(double ar[], int size)
{
    int i = 0;
    cout << "\nEnter a double value (Enter q to quit): ";
    while (cin >> ar[i] && i < size)
    {
        i++;
        cout << "Enter next value: ";
    }
    return i;
}

void Show_Array(const double ar[], int size)
{
    cout << "\nHere is your array: " << endl << endl;
    for (int i = 0; i < size; ++i)
        cout <<"\#"<<i+1<<" is "<<ar[i] << endl;
}

void Reverse_Array(double ar[], int size)
{
    cout << "Now we reverse the whole array: " << endl;
    for (int i = 0; i != size / 2; ++i)
    {
        double temp;
        temp = ar[i];
        ar[i] = ar[size - 1 - i];
        ar[size - 1 - i] = temp;
    }
}

void Reverse_Positions(double ar[], int size)
{
    cout << "Enter two positions: " << endl;
    int a, b;
    if (cin >> a >> b)
    {
        double temp = ar[b - 1];
        ar[b - 1] = ar[a - 1];
        ar[a - 1] = temp;
    }
}

Something wrong with reverse_position function: It only shows "Enter two positions" and then the function terminates. There is no chance for me to enter the integers a and b. I doubt there is something wrong with the input queue, but I am not sure.

phantom
  • 3,292
  • 13
  • 21
  • "I doubt...but I am not sure" - don't guess - instead of just using `if (cin >>` everywhere (which is already better than most people do - well done), add `else throw std::runtime_error("error parsing two positions");` and a `try`/`catch` block in `main()`, with similar code anywhere else you're doing input. Then you'll find something interesting - hint: `q` to quit was not a good idea, or not implemented well depending on your perspective. – Tony Delroy Apr 30 '15 at 23:57
  • @TonyD, shouldn't really `throw` here, it's probably better to validate the input in a loop (`while(!cin >> ...){ cin.clear(); cin.ignore(1024, '\n');}`) – vsoftco May 01 '15 at 00:00
  • @vsoftco: depends entirely on the recovery requirements - hacking something like this up as a programming exercise, I'd likely use my `FATAL` macro do write to `std::cerr` and `exit(1)`, or `assert()`, but use `throw` here because it's a one-liner and reasonable thing for beginners to learn, but if you were writing an interactive program that needed to handle user errors by continuing to let them enter the same stream of numbers, then yes - `clear` and `ignore` are the way to go. – Tony Delroy May 01 '15 at 00:04
  • @TonyD I tried throw, but when I run the program it says R1060 -abort() has been called. – Chenglin Wang May 01 '15 at 00:15
  • @ChenglinWang: did you remember the try/catch block in main()? If you're not sure what I mean by that, it might be easier just to write a message to `std::cerr` - then you can see when input fails. To give the game away, you're trying to read `q` into a number variable, which sets error state on the stream such that all future input fails (you can clear the stream state as vsoftco suggests). Other options: numeric sentinel value (e.g. enter 0 to quit / enter -1 to quit / any value that wouldn't make sense as a array element value), or use `getline` to read lines then an `istringstream`. – Tony Delroy May 01 '15 at 00:23
  • @TonyD I am so sorry, but I don't know how to use std:: cerr either. Could you just point out where the run-time error occurs? Thanks. Also, q is not the only quitting condition, any character input (non-double input) will cause it to stop. – Chenglin Wang May 01 '15 at 00:36
  • Using `std::cerr` is just like using `std::cout`, which you're already doing, just it prints to the standard error stream which still tends to appear on the screen, but can be independently redirected by the shell: e.g, `program > cout` still prints errors to the terminal, `program 2>&1 > cout_and_cerr` writes both to a file called `cout_and_cerr`. Anyway, if you want robustness to recovery from non-numeric input, see [this answer](http://stackoverflow.com/questions/29979415/file-processing-using-try-catch-c/29979556#29979556) of vsoftco's. – Tony Delroy May 01 '15 at 00:47

0 Answers0