word sorting using array swap

Discussion in 'C' started by Yasiir2, Apr 13, 2009.

  1. Yasiir2

    Yasiir2 New Member

    Joined:
    Apr 13, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Dear Friends

    I wrote a code that prompt user to give 5 different words or name and than it will print given names or words in ascending order .code is running successfully but I am using Dec.C++ 4.9.9.2 and in this compiler my code is not running it close the window after getting 5 strings.please help me in this code.
    Code:
    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<conio.h>
    using namespace std;
    int main()
    {
     char array[5];
     char temp;
     for(int i=0;i<5;i++)
     {
      cout<<"enter string :"<<i+1<<": ";
      cin>>array[i];
        }
     for(int i=0;i<5-1;i++)
     {
      for(int j=i;j<5;j++)
      {
       if(array[i]>array[j])
       {
        temp=array[i];
        array[i]=array[j];
        array[j]=temp;
       }
      }
     }
     cout<<"sorting string\n";
     for(int i=0;i<5;i++)
     {
     cout<<array[i]<<endl;
     
    Last edited by a moderator: Apr 13, 2009
  2. Yasiir2

    Yasiir2 New Member

    Joined:
    Apr 13, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    sorry i forgot to mention that the code is running in trubo c but not in Dev C++.
     
  3. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    Hi Yasiir,

    I'm not familiar with Dev C++, but I can try with the program part.

    Is this the complete code?
    I can see that the last for loop is not completed.
    Also the int main is not completed.

    I don't know if this will be true for Dev C++ or not,
    Does Dev C++ open a new window for I/O of the program?
    If Yes, then I believe that after you display the sorted results, you'll have to wait for user input (action) to close the window, so that the last output is displayed to the user and the window still remains open, instead of the window closing directly, in which case the results will be displayed but it will not be visible to the user because the window gets closed.

    Let me know If I'm right or not...

    - Nimesh
     
    shabbir likes this.
  4. Yasiir2

    Yasiir2 New Member

    Joined:
    Apr 13, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Dear Nameesh

    Yes you are right that that Dev C++ open a new window for IO of the programme and it is also true that it close directly after propmting 5 character.However I tried more and got the solution.I just add one more header file i.e stdio.h and conio.h and getch(); at the end and my code is correct now.But please explain me as I am in beggining of C++.why my window clos directly after taking 5 char.??

    Thanks
    Code:
    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<conio.h>
    using namespace std;
    int main()
    {
     char array[5];
     char temp;
     for(int i=0;i<5;i++)
     {
      cout<<"enter string :"<<i+1<<": ";
      cin>>array[i];
        }
     for(int i=0;i<5-1;i++)
     {
      for(int j=i;j<5;j++)
      {
       if(array[i]>array[j])
       {
        temp=array[i];
        array[i]=array[j];
        array[j]=temp;
       }
      }
     }
     cout<<"sorting string\n";
     for(int i=0;i<5;i++)
     {
     cout<<array[i]<<endl;
    }
     getch();
    }
     
    Last edited by a moderator: Apr 16, 2009
  5. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    It's the normal behavior for the program.

    You said to display it, so it displays the output and quits.

    How will the program know that the user wants to watch the output for hours and then close the window.

    So, we have to write and extra statement, for the user to give an additional input, any input, to indicate that he has done his output viewing and needs to close the window.

    So, the statement... getch();

    You can also add an output line like... "Press any key to close this window"
     
  6. Yasiir2

    Yasiir2 New Member

    Joined:
    Apr 13, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks sir
    I got the point.
    I expect you will support me always like this.

    Once again thank you.

    Yasir
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice