I'm writing a program in C++ to ask the user for a list of ten names and cities of residence in the format <firstname> <Surname> <city>.
I am using isspace to sort out the number of spaces, but then i have to sort the list by city and then in each city group alphabetically by name.
I have written numerous codes that have all failed me! This is the latest code I have written. There is one error message "undefined reference to 'WinMain@16'"
I have read in a number of places that including int main() solves this issue, however each time I do this, that error is replaced by about 20 others!! :/
If anyone has any ideas on how I might actually get this code working I would be most thankful!!:)
This is the code:
#include <cstring>
#include <algorithm>
const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
Yes, perhaps that is what is generating the error.
Try putting int main() and if that doesn't work, int main(int argc, char* argv[]) and repost the errors
#include <cstring>
#include <algorithm>
int main(){
const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
}
I am told there are 2 errors:
line 10: error: a function-definition is not allowed here before '{' token
line 14: error: a function-definition is not allowed here before '{' token
When I include int main(int argc, char* argv[]) my code looks like:
#include <cstring>
#include <algorithm>
int main(int argc, char* argv[]) {
const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
}
I am told there are the exact same 2 errors as just using int main ():
line 10: error: a function-definition is not allowed here before '{' token
line 14: error: a function-definition is not allowed here before '{' token