Hey I'm trying to get this code going but i can't get my if(choice==2) to output anything any ideas?

#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

int main()
{
    int i,num[20],n,j,choice,tmp;

    cout<< "Please enter 20 integers";
    for (i=0; i<20; i++)
    {
        cout<<"\nEnter next value:";
        cin>>num[i];

    }

    cout<<"\n1.Display original data.\n";
    cout<<"2.Sort the data into descending order\n";
    cout<<"3.Display the sorted data (Only if you've already sorted)\n";
    cout<<"4.Get the address of the first element of array.\n\n";
    cin>>choice;

    if (choice==1)
    {

        for (i=0; i<20; i++)
        {
            cout<<"\n"<<num[i]<<endl;
        }
        return 0;
    }
    if (choice==2)
        {
            for (i=0; i<n-1; i++)
            {
                for (j=0; j<n-1-i; j++)
                {
                    if (num[j+1] < num[j])    /* compare the two neighbors */
                    {
                        tmp = num[j];         /* swap a[j] and a[j+1]      */
                        num[j] = num[j+1];
                        num[j+1] = tmp;

                        cout<<"Here are your numbers:"<<tmp<<endl;
                    }
                }
            }
        }

}
if (choice==1)
{
    //HERE CHOICE = 1
    for (i=0; i<20; i++)
    {
        cout<<"\n"<<num[i]<<endl;
    }
    return 0;
}
    // HERE CHOICE STILL EQUALS 1!
if (choice==2)
    {
        for (i=0; i<n-1; i++)
        {
            for (j=0; j<n-1-i; j++)
            {
                if (num[j+1] < num[j])    /* compare the two neighbors */
                {
                    tmp = num[j];         /* swap a[j] and a[j+1]      */
                    num[j] = num[j+1];
                    num[j+1] = tmp;

                    cout<<"Here are your numbers:"<<tmp<<endl;
                }
            }
        }
    }

Please see my comments in your code for a hint...

Ok i figured out part of my problem i didn't set n equal 20. Though I'm still not getting an output of the users input in the correct order...

Ok then stupid question to end if(choice==1) if add another } would that close it off so that it owuldn't affect if(2). Cause i'm honestly stumped on that one

Yes, but to make your code easier to follow it might be worth writing "else if (choice==2)"...

Hmm... Ok I've finally got it running but when it outputs it's only outputting 11 numbers and it should be 20 did i mess up the bubble sort to the point where i would lose data?

You have two loops

for (i=0; i<n-1; i++)
{
   for (j=0; j<n-1-i; j++)
  {

but I can't see where you initialise n. Try printing the value of n to the console just before that line and see what happens...

EDIT: Also, I think you will not need the "-1" in each of those loops, but we'll come to that once we work out what is happening with n.

I initialized n here:

1.n=20
2.for (i=0; i<n-1; i++)
3.{
4.for (j=0; j<n-1-i; j++)
5.{

Oh dang I almost got it i have it actually working right i initialized n and then took out the -1's and it works but it doesn't show the last two numbers of the 20 inputs that are sorted only shows 18

--Okk... I lied i ran it again and used big numbers like 120 and such and it doesn't output right at all it just kinda puts them where it wants them instead of sorting them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.