コード例 #1
0
ファイル: main.cpp プロジェクト: cognar/CS162A03
void addStar()
{
    char name[NAME_SZ] = "";
    long temp = 0;
    double lumin = 0, mass = 0, radius = 0;
    Star *newStar;

    if(strcmp(Star::filePath_, "") == 0)        //if no file is open, do nothing.
    {
        cout << "No file to write to, please load a file.\n";
        return;
    }
    cout << "Please enter the following information: \nName: ";
    cin >> name;
    newStar = new Star(name);       //instantiate star with name
    if(newStar == 0)        //Abort if memory allocation fails.
    {
        cout << "Memory allocation failed.\n";
        return;
    }
    cout << "Temperature: ";
    cin >> temp;
    newStar->SetTemperature(temp);
    cout << "Luminosity: ";
    cin >> lumin;
    newStar->SetLuminosity(lumin);
    cout << "Mass: ";
    cin >> mass;
    newStar->SetMass(mass);
    cout << "Radius: ";
    cin >> radius;
    newStar->SetRadius(radius);
    linkList.AddLinkToBack(newStar);        //add star to linked list
    newStar->AppendToFile();
}
コード例 #2
0
ファイル: main.cpp プロジェクト: cognar/CS162A03
void readFromFile()
{
    ifstream inFile;
    char name[NAME_SZ] = "";
    long temp = 0, starCount = 0;
    double lumin = 0, mass = 0, radius = 0;
    Star *newStar;

    clearStars();       //clear any residual data
    cout << "Please enter the file path.\n";
    cin >> Star::filePath_;
    inFile.open(Star::filePath_);
    while(inFile >> name)
    {
        name[strlen(name)-1] = '\0';     //strips comma from name.
        newStar = new Star(name);
        if(newStar == 0)        //Abort if memory allocation fails.
        {
            cout << "Memory allocation failed.\n";
            break;
        }
        inFile.ignore();    //ignore coma, name entry actually grabs first comma, all others are caught by ignore()
        inFile >> temp;     //read temperature
        newStar->SetTemperature(temp);
        inFile.ignore();
        inFile >> lumin;    //read luminosity
        newStar->SetLuminosity(lumin);
        inFile.ignore();
        inFile >> mass;     //read mass
        newStar->SetMass(mass);
        inFile.ignore();
        inFile >> radius;   //read radius
        newStar->SetRadius(radius);
        inFile.ignore();
        starCount++;
        linkList.AddLinkToBack(newStar);
    }
    inFile.close();
    inFile.clear(std::ios_base::goodbit);       //reset flags to prevent errors on certain compilers
    cout << "Successfully loaded " << starCount << " star entries.\n";
}