tags:

views:

103

answers:

3

hi all,

i'm writing my c++ project and in visual studio everything goes good but when i'm compiling it on ubuntu many things get wrong. example:

int main (int argsNum, char* args[]){
    Country* country = new Country("USA");
    Military* military = new Military("Army",country);
    Shalishut* shalishut = new Shalishut(military);
    Manager* manager = Manager::GetInstance();

    FileReader* fileReader = FileReader::GetInstance();
    fileReader->ReadCityConfig(args,country);
    fileReader->ReadRoadConfig(args,country);
    fileReader->ReadMilitrayCampConfig(args,military);

    military->ShowBases();

    return 0;
}

void FileReader::ReadMilitrayCampConfig(char* args[], Military* military){
    string line;
    char inputFileName [MAX_FILE_NAME_LEN];
    strcpy (inputFileName,args[3]);
    ifstream myfile (inputFileName); //inputFileName
    char* campName;
    string cityName;

    if (myfile.is_open()){
        while (!myfile.eof()){ //until the end of file
            getline (myfile,line); //separate each line.
            if ((line.size() != 0) && (line[0] != '#')) {
                campName = strtok(&line[0],",");
                cityName = (string)strtok(NULL,",");
                Shalishut::FixName(campName);  Shalishut::FixName(&cityName[0]);
                if (!(military->IsBaseExist(campName))){
                    if (military->GetCountry()->IsCityExist(cityName)){
                        Base* baseToAdd = new Base(campName,cityName);
                        if (baseToAdd != NULL){ 
                            military->AddBaseToMilitary(baseToAdd);
                            military->GetCountry()->FindCity(cityName)->AddBaseToCity(baseToAdd);
                        }
                    }
                    else cout << "ERROR: City named \"" << cityName << "\" does not exist, can't add base \"" << campName << "\" !" << endl<<endl;
                }
                else cout << "ERROR: Base Named \"" << campName << "\" is already exist in Military, can't create base!" << endl<<endl;
            }       
        }
        myfile.close();
    }
    else throw ExceptionMilitaryCampConfigFileFault(); /*cout << "ERROR: Unable to open MilitaryConfig file!"<< endl;*/
}

bool Country::IsCityExist(const string cityName){
    map<string ,City*>::iterator itCities;
    itCities = m_cities.find((string)cityName);
    if (itCities != m_cities.end()) return true;
    else return false;
}

void Shalishut::FixName(char* name){
    int i;
    name[0] = toupper(name[0]);
    for (i=1 ; name[i] ; i++){
            name[i] = tolower (name[i]);
        }
    }
}

The problem is that the program reads the cities and the roads, but when it reads the military camp i got:

" does not exist, can't add base "Hazerim" !

even though in the config file i have base in the same name. remind: in visual studio it works perfectly!

+1  A: 

Assuming the error message is actually ERROR: City named _____ does not exist, can't add base "Hazerim" I would look carefully at the capitalization/spelling of the cities and city-for-base in your inputs. They probably don't match.

Also using strtok on a std::string is just asking for trouble, as it's destructive and strings don't expect their internal state to be blown away randomly. There are method like find_first_of that will help you parse C++ strings.

Mark B
A: 

Like others have said:

  • double check line endings (maybe run dos2unix on input files in lieu of a more robust / error=prone solution)

  • make sure the case of everything is correct, file names are case sensitive

  • be aware of where it is looking for files, make sure everything is in the CWD

+1  A: 

I'd advise not messing around with std::string internals. I don't know that it's legal, and it certainly could cause problems. Use .c_str() to get the C-style string and copy it to a char [], or use string functions to parse the input.

To debug, put insome output statements so you can see what the string values are, or learn a bit about gdb and step through a short initialization run.

That cityname = (string)... is just plain ugly. Since you're not using cityname out of that scope, you can declare string cityname(...);, and cityname will always be initialized and will be defined close to where it's used.

David Thornley