理由C ++

时间:2014-04-29 00:33:11

标签: c++ cout

我似乎无法让我的数据保持在正确的位置。它最初有效,但是当我将我的代码转移到我的家用电脑时,理由就开始了。谁能帮我吗??

#include <iostream>
#include <iomanip>
#include <ctime>
#include "fixgets.c"

using namespace std;

int main (void)
{
int userID, blackhole_mass, radius, totalMass=0;
long int date;
char blackhole_name[16];
char FirstName[11];
char LastName[16];

time(&date);


FILE *blackholeFile;

if ((blackholeFile = fopen( "Holter.txt", "r")) == NULL)
{
cout << "File could not be opened" << endl;
}

//Heading

cout << setw(12) << "\n'Schwartzchild Radius Database' by Jonathan Holter" << setw(4)            
<< " " << ctime(&date);



cout << left;
cout << setw(12) << "First Name"
<< setw(11) << "Last Name" << setw(9)  << "User ID" << setw(17) << "Black Hole Name" <<    
setw(17) << "Black Hole Mass" << "Schwartzchild Radius" << endl << endl;


fixgets(FirstName,11,blackholeFile);
fixgets(LastName,16,blackholeFile);
fscanf(blackholeFile,"%d\n", &userID);
fixgets(blackhole_name,16,blackholeFile);
fscanf(blackholeFile,"%d\n", &blackhole_mass);

while (!feof (blackholeFile))
{

radius = blackhole_mass * 2;

totalMass += blackhole_mass;

cout << left << setw(12) << FirstName << setw(11) << LastName << right
<< setw(9)  << userID << left << "  " << setw(17) << blackhole_name
<< right << setw(10) << blackhole_mass << setw(22) << radius << endl << endl << endl;

fixgets(FirstName,11,blackholeFile);
fixgets(LastName,16,blackholeFile);
fscanf(blackholeFile,"%d\n", &userID);
fixgets(blackhole_name,16,blackholeFile);
fscanf(blackholeFile,"%d", &blackhole_mass);

}

cout << right << setw(48) << "  " << setw(10) << right << "Total Amount of Mass 
Recorded:  " << totalMass;

fclose(blackholeFile);
return 0;
}   

这是我的代码的副本。

1 个答案:

答案 0 :(得分:0)

您已使用FILE*scanf个功能。我建议一直回到C输入和输出功能。使用printf。控制格式更容易,我认为也更容易阅读。