问题
We are doing a project involving storing and comparing various cities. We have become stuck after adding a new city into the database, it goes to the bottom of the list - we want it to go into the database, sorted alphabetically. As only one value can be added at a time, all the other entries will already be alphabetical.
Below is the relevant code to this.
The problem area is the // Insertion Sort when adding // out bit.
The error codes are:
[BCC32 Error] File1.cpp(250): E2294 Structure required on left side of . or .*
[BCC32 Error] File1.cpp(250): E2108 Improper use of typedef 'node'
[BCC32 Error] File1.cpp(250): E2188 Expression syntax
All corresponding to the line
while (strcmp(cityTemp.fname, node[l]) < 0) && (l <= ct )
If you could point us in the right direction, that would be great. Thanks
// -------------------------------------------------------------------------- //
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <iomanip>
#define PI 3.14159265
using namespace std;
// -------------------------------------------------------------------------- //
class node
{
private:
char city[100];
char country[100];
float longitudeDegree;
float longitudeMinutes;
char eastWest[2];
float latitudeDegree;
float latitudeMinutes;
char northSouth[2];
//useful for link list!
node *next;
// -------------------------------------------------------------------------- //
public:
//Constructor for class node
// ctn = city node
// cyn = country node
// longD = longitude degree
// longM = longitude minutes
// ew = east / west
// latD = latitude distance
// latM = latitude minutes
// ns = north / south
node(char ctn[], char cyn[], float longD,
float longM, char ew[], float latD, float latM, char ns[])
{
//Copy char array ctn to class array city
//string copy is part of string header
strcpy(city, ctn); //copy to string city name
strcpy(country, cyn); //copy to string country name
longitudeDegree = longD;
longitudeMinutes = longM;
strcpy(eastWest, ew);
latitudeDegree = latD;
latitudeMinutes = latM;
strcpy(northSouth, ns);
cout << "Hello from node with name: " << city << endl;
}
// -------------------------------------------------------------------------- //
// Get function to return city stored in class //
char* getCity()
{
return city;
}
char* getCountry()
{
return country;
}
float getLongDe()
{
return longitudeDegree;
}
float getLongMin()
{
return longitudeMinutes;
}
char* getEastWest()
{
return eastWest;
}
float getLatDe()
{
return latitudeDegree;
}
float getLatMin()
{
return latitudeMinutes;
}
char* getNorthSouth()
{
return northSouth;
}
};
// -------------------------------------------------------------------------- //
class menu
{
private:
int fnum, mnum, ct, xx, fg, ans, ans2;
float longitudeDegree;
float longitudeMinutes;
float latitudeDegree;
float latitudeMinutes;
char country[100];
char eastWest[2];
char northSouth[2];
//array of pointers of type class node
node *db[200]; //denotes how many pointers to use for the amount of cities in database
char fname[200];
char pt[200];
char sfnum[200];
char yn[2]; //yes/no
public:
//constructor
menu()
{
//Read the serialized data file
ct= Readit();
}
// -------------------------------------------------------------------------- //
void start()
{
// Add a city //
case 1:
cout << "Enter the name of the city:";
cin.getline(fname,100);
fg=Findit(ct,fname);
if(fg>0)
{
cout << "This entry is already in the database: " << fname <<endl;
}
else
{
cout << "Enter the Country of " << fname << endl;
cin >> country;
//ans2 = '-1';
do
{
cout << "Enter the Longitude Degrees (0 - 180) of " << fname <<endl ;
cout << "Enter degrees between 0 - 180 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=180)));
longitudeDegree = ans2;
//ans2 = '-1';
do
{
cout << "Enter the Longitude Minutes (0 - 60) of " << fname << endl;
cout << "Enter minutes between 0 - 60 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=60)));
longitudeMinutes = ans2;
ans = 'Z'; //default to an answer not in while loop
do
{
cout << "East or West?\n";
cout << "You must type a capital 'E' or a capital 'W'.\n";
cin >> ans;
}
while((ans !='E')&&(ans !='W'));
eastWest[0] = ans;
eastWest[1] = '\0';
//ans2 = '-1';
do
{
cout << "Enter the Latitude Degree (0 - 90) of " << fname << endl;
cout << "Enter degrees between 0 - 90 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=180)));
latitudeDegree = ans2;
//ans2 = '-1';
do
{
cout << "Enter the Latitude Minutes (0 - 60) of " << fname << endl;
cout << "Enter minutes between 0 - 60 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=60)));
latitudeMinutes = ans2;
ans = 'Z'; //default to an answer not in while loop
do
{
cout << "North or South?\n";
cout << "You must type a capital 'N' or a capital 'S'.\n";
cin >> ans;
}
while((ans !='N')&&(ans !='S'));
northSouth[0] = ans;
northSouth[1] = '\0';
ct++;
db[ct]=new node(fname,country,longitudeDegree,longitudeMinutes,
eastWest,latitudeDegree,latitudeMinutes,northSouth);
/* // Insertion Sort when adding //
node *cityTemp;
cityTemp=db[ct];
//cityTemp = new node (fname, country, longitudeDegree, longitudeMinutes, eastWest, latitudeDegree, latitudeMinutes, northSouth);
int j, l;
// Find place to insert the new city //
// All other cities will already be in alphabetical order
l=0;
while (strcmp(cityTemp.fname, node[l]) < 0) && (l <= ct )
//strcmp(cityTemp.fname, node[l].fname)<0) && (l<=ct)
{
l++;
}
// Move down rest
for (j = l, j <= ct);
{
j++;
}
db[j+1] = db[j];
db[j] = cityTemp; */
}
break;
}
// -------------------------------------------------------------------------- //
// Function to convert string to lower case ascii //
char *strLower( char *str )
{
char *temp;
for ( temp = str; *temp; temp++ )
{
*temp = tolower( *temp );
}
return str;
}
// -------------------------------------------------------------------------- //
// Function to search through the names stored in nodes //
int Findit(int ctt,char fnamef[])
{
int nn=0;
int xx;
for(int k=1;k<=ctt;k++)
{
xx=strcmp(strLower(db[k]->getCity()),strLower(fnamef));
//xx is zero if names are the same
if(xx==0)
nn=k;
}
return nn;
}
// -------------------------------------------------------------------------- //
// Function to do serialization of nodes and store in a file //
void Storeit(int ctt)
{
fstream outfile;
outfile.open("cityList.txt",ios::out);
outfile<<ctt<<endl;
for(int k=1;k<=ctt;k++)
{
//outfile<<db[k]->getName()<<endl;
outfile<<db[k]->getCity()<<endl;
outfile<<db[k]->getCountry()<<endl;
outfile<<db[k]->getLongDe()<<endl;
outfile<<db[k]->getLongMin()<<endl;
outfile<<db[k]->getEastWest()<<endl;
outfile<<db[k]->getLatDe()<<endl;
outfile<<db[k]->getLatMin()<<endl;
outfile<<db[k]->getNorthSouth()<<endl;
}
outfile.close();
}
// -------------------------------------------------------------------------- //
int Readit()
// Function to open the file, read in the data and create the nodes
{
int ctx=0;
fstream infile;
infile.open("cityList.txt",ios::in);
//infile>>ctx;
infile.getline(sfnum,200);
ctx=atoi(sfnum);
/*
for(int k=1;k<=ctx;k++)
{
//infile>>fname;
infile.getline(fname,200);
//infile>>weight;
infile.getline(sfnum,200);
weight=atof(sfnum);
//infile>>height;
infile.getline(sfnum,200);
height=atof(sfnum);
db[k]=new node(fname,height,weight);
}
*/
// Read in file to variables i.e. longitudeDegree = atof(sfnum)
for(int k=1;k<=ctx;k++)
{
//infile>>fname;
infile.getline(fname,100);
//infile>>country ;
infile.getline(sfnum,100);
strcpy(country,sfnum);
//infile>>longitudeDegree;
infile.getline(sfnum,100);
longitudeDegree=atof(sfnum);
//infile>>longitudeMinutes;
infile.getline(sfnum,100);
longitudeMinutes=atof(sfnum);
//infile>>eastWest ;
infile.getline(sfnum,100);
strcpy(eastWest,sfnum);
//infile>>latitudeDegree;
infile.getline(sfnum,100);
latitudeDegree=atof(sfnum);
//infile>>latitudeMinutes;
infile.getline(sfnum,100);
latitudeMinutes=atof(sfnum);
//infile>>northSouth ;
infile.getline(sfnum,100);
strcpy(northSouth,sfnum);
db[k]=new node(fname,country, longitudeDegree,
longitudeMinutes, eastWest, latitudeDegree, latitudeMinutes, northSouth);
}
infile.close();
return ctx;
}
};
// End of class menu
回答1:
Don't you mean to use db[l]
- the variable - rather than node
- the type.
回答2:
Azorath wrote: (strcmp(cityTemp.fname, node[i]) < 0) && (l <= ct )
and after some give and take we've got, with some confusion between i, I and l....
(strcmp(cityTemp.fname, db[i]->getcity()) < 0) && (l??? <= ct )
yes? (how does Erik know that db[i] is in the code?)
I'm taking a guess that cityTemp is an instance of node, not a pointer to node, and that db[] is an array of pointers to nodes, yes?
One thing wrong is "cityTemp.fname" - there is no "fname" member in the class. Its looking for a structure containing a member "fname". Do you mean cityTemp.city?
Try that and trport what you get...
来源:https://stackoverflow.com/questions/5569109/c-alphabetical-insertion-sort