Hey guys

I know I have posted this type of question before but I want to get to the nitty-gritty of this. I have the following program that sorts ten numbers enter by the user. The program work but I am not able to see the results because the program closes as soon as it done displaying. If i use

system("pause");

the programs stops, but I know this is not ideal at all to be used. I have try to use

cin.ignore();
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;

but this guys don't work all the time. I wanted to know, why is this, why do these two commands some times work and sometimes don't' Thanks for the help

GCard

I have numbered 1 and 2 where i try to use cin.ignore()

#include <iostream>
using namespace std;
int main()
{
    int numbers[10],temp,i,size = 10;
    int original,sorted;
    //Creates an array of 10 user supplied numbers.
    for(i=0;i<size;i++)
    {
                       cout<<"Please enter a number.\n";
                       cin>>numbers[i];
                       } // end of for loop
//outer loop goes through the entire list
        for(original=1; original < size; original++)
        {
                        for(sorted = size-1; sorted >= original; sorted--)
                        {
                        /*If element sorted-1 is greater than element after 
                        it then swap it.*/
                           if(numbers[sorted-1] > numbers[sorted])
                           { 
                             temp = numbers[sorted-1];
                             numbers[sorted-1] = numbers[sorted];
                             numbers[sorted] = temp;
                           }// end of if
                        }// end of inner for loop
        }// end of outer for loop
        //print the sorted array.
        cout << "The sorted array is \n";
        for(i=0;i<size;i++)
        {
                           cout<<numbers[i]<<" "<<endl;
                           //cin.ignore(); <-------- (1)
        }
        //cin.ignore(); <-------- (2)
        system("pause");
        return 0;
}// end of main

>I wanted to know, why is this, why do these two
>commands some times work and sometimes don't'
My initial guess is that they're in the wrong order. You clear the stream buffer first, then pause, not the other way around. If you really want the nitty gritty, try this thread.

danke schön

Talk bout nitty gritty. It will take me some reading to get ti but it seems pretty straight forward

The only thing I can remember from a long time ago was something else. I would prolly do:

#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
     int numbers[10], temp, i, size = 10, original, sorted;

     for (i = 0; i < size; i++)
     {
          cout << "Please enter a number: ";
          cin >> number[i];
          cout << endl;
     }

     for (original = 1; original < size; original++)
     {
          for (sorted = (size - 1); sorted >= original; sorted++)
          {
               if (numbers[sorted-1] > numbers[sorted])
               {
                    temp = numbers[sorted-1];
                    numbers[sorted-1] = numbers[sorted];
                    numbers[sorted] = temp;
               }
          }
     }     

     cout << "The sorted array is:" << endl;
     
     for (i = 0; i < size; i++)
          cout << numbers[i] << " ";

     _getch();
}

If you wanted to you could do (if that's what you were aiming for)

for (i = 0; i < size; i++)
     {
          cout << numbers[i] << " ";
          _getch();
     }

If you would like to avoid this all together you can just put some code in that tells the program to stop when you want it to:

cout << "Press 1 to Exit:" << endl;
cin >> stop;

This article has been dead for over six months. Start a new discussion instead.