1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
using namespace std;
//Global constants for the dimensions of the arrays
const int MAX_SIZE = 30;
const int MAX_CITIES = 300;
//Function Prototypes
void SortCities(char [MAX_CITIES][MAX_SIZE], char[MAX_CITIES][MAX_SIZE], int [MAX_CITIES], int);
void DisplayCities(char [MAX_CITIES][MAX_SIZE], char[MAX_CITIES][MAX_SIZE], int [MAX_CITIES], int);
int main()
{
ifstream Input;
char City[MAX_CITIES][MAX_SIZE];
char State[MAX_CITIES][MAX_SIZE];
int Population[MAX_CITIES];
int count;
Input.open("cities.txt");
count=0;
//Reads in from cities.txt
while (!Input.eof())
{
Input.getline(City[count], MAX_SIZE);
Input.getline(State[count], MAX_SIZE);
Input >> Population[count] >> ws;
count++;
}
Input.close();
SortCities(City, State, Population, count);
DisplayCities(City, State, Population, count);
}
//Functions
void SortCities(char City[MAX_CITIES][MAX_SIZE], char State[MAX_CITIES][MAX_SIZE], int Population[MAX_CITIES], int count)
{
char tempstr[MAX_SIZE], lengths[MAX_SIZE];
int temp, min;
for(int i=0; i<MAX_SIZE; i++)
{
min = i;
for(int x=i+1; x<MAX_SIZE; x++)
{
if(strcmp(lengths[x], lengths[min]) < 0) // THIS IS WHERE I GET ERROR
{
min = x;
strcpy(tempstr[i], lengths[i]);
strcpy(lengths[i], lengths[min]);
strcpy(lengths[min], tempstr[i]);
}
return;
}
void DisplayCities(char City[MAX_CITIES][MAX_SIZE], char State[MAX_CITIES][MAX_SIZE], int Population[MAX_CITIES], int count)
{
cout << "Large cities in the United States" << endl << endl;
for(int i=0; i<MAX_CITIES; i++)
{
for(int x=0; x<MAX_SIZE; x++)
cout << City[i][x] << ", " << State[i][x] << " : " << Population[i] << endl;
cout << City[i] << ", " << State[i] << " : " << Population[i] << endl;
}
|