Hi Im writing this sorting program. A user can enter a First Name and Last name (10 for now). It stores them in a string. Then a user can enter 10 numbers. The program sorts the numbers and then displays the names assciated with the numbers in order.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int compare(int, int);
void sort(int[], const int);
void swap(int *, int *);
void getname(void);                  // get student name
string  Name [25];

void getname(void)
{
   
	 
    for(int i=0; i<10; i++)
	{
    getline(cin, Name[i]);
	}
    return;
  }


int compare(int x, int y)
{
     return(x > y);
}

void swap(int *x, int *y)
{
     int temp;
     temp = *x;
     *x = *y;
     *y = temp;
}

void sort(int table[], const int n)
{
     for(int i = 0; i < n; i++)
     {
          for(int j = 0; j < n-1; j++)
          {
               if(compare(table[j], table[j+1]))
                    swap(&table[j], &table[j+1]);
          }
     }
}

int quantity=10;
int* tab;

int main()
{
cout<<" Enter your first and last name: ";
	getname(); 
 

tab = new int [quantity];
cout << "Input numbers: \n\n";
for (int i = 0; i < quantity; i++)
{
    int x = i;
    cout << "#" << ++x << ": ";
    cin >> tab[i];
}

cout << "\nBefore sorting: ";
for (int i = 0; i < quantity; i++)
{
     cout << tab[i] << " ";
}

cout << "\nAfter sorting: "<<endl;
sort(tab, quantity);
for(int i = 0; i < quantity; i++)
{
     
	tab[i]==Name[i];
	cout << tab[i] <<"    "<<Name [i]<<endl;
}
return 0;
}

I have two problems the "Enter name does not appear 10 time" it only appears once, I want it to display 10 times. Also the strings and the numbers are not associated. I need the first number entered to be associated to the first string entered.

Recommended Answers

All 2 Replies

Some comments:
There's probably no reason to use global variables. If the program expands, then they make things very confusing.

string getname(void)
{
   cout << "Enter a name: ";
string Name;
    getline(cin, Name);
return Name;
  }

This is called "parallel sorting". I posted how to do it here:
http://www.daniweb.com/forums/thread181356.html

Dave

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.