class Diatom:public Algae
{
public:
Diatom(){}
~Diatom(){}
static int WriteDiatom()
{
unsigned long int *pMaxLength = new unsigned long int;
*pMaxLength = 3000; //Placed on the heap
char Species[*pMaxLength];
char Locationfound[*pMaxLength];
char Description[*pMaxLength];
int *pMaxFile = new int;
*pMaxFile = 80;
char filename[*pMaxFile];
cout << "Enter File Name: (Make Sure That Every File Name Created it Unique) ";
cin.get(filename, *pMaxFile);
ofstream fout(filename);
delete pMaxFile;
pMaxFile = 0;
Top:
cout << endl;
cout << "Enter The Taxanomic Name For The Diatom: ";
cin.ignore(1, '\n');
cin.getline(Species, *pMaxLength);
fout << "Species: ";
fout << Species << endl;;
cout << "Enter The Location That The Species Was Found: ";
cin.ignore(1,'\n');
cin.getline(Locationfound, *pMaxLength);
fout << "Location :";
fout << Locationfound << endl;
cout << "Add a Description Regarding the Diatom: ";
cin.ignore(1, '\n');
cin.getline(Description, *pMaxLength);
fout << "Description: ";
fout << Description << endl;
cout << endl;
cout << endl;
cout << "To Add Another Diatom type (1) or (0) to go back to the Menu and Start A new File" << endl;
int Var;
cin >> Var;
if(Var == 1)
{
goto Top;
}
else
{
fout.close();
delete pMaxLength; // delete pointer
pMaxLength = 0; // set to null to avoid stray
return menu();
}
}
};
Error In File Print Out
Page 1 of 14 Replies - 841 Views - Last Post: 04 August 2012 - 12:44 PM
#1
Error In File Print Out
Posted 03 August 2012 - 10:35 PM
The program compiles and runs fine, however when I open the file the data prints to the first letter of character types is missing from the file. How do I fix that?
Replies To: Error In File Print Out
#2
Re: Error In File Print Out
Posted 04 August 2012 - 02:24 AM
http://www.cplusplus...istream/ignore/
So, what do you think all those ignore calls are doing?
37 cin.ignore(1,'\n');
38 cin.getline(Locationfound, *pMaxLength);
Bearing in mind that getline() uses \n as a delimiter anyway.
Then there is this horror...
Try using a loop, such as
Why bother with all the new/delete?
Quote
The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.
So, what do you think all those ignore calls are doing?
37 cin.ignore(1,'\n');
38 cin.getline(Locationfound, *pMaxLength);
Bearing in mind that getline() uses \n as a delimiter anyway.
Then there is this horror...
29 Top:
30 cout << endl;
31 cout << "Enter The Taxanomic Name For The Diatom: ";
...
52 if(Var == 1)
53 {
54 goto Top;
55 }
Try using a loop, such as
do {
cout << endl;
cout << "Enter The Taxanomic Name For The Diatom: ";
// rest of looping code
} while ( Var == 1 );
Why bother with all the new/delete?
12 unsigned long int *pMaxLength = new unsigned long int; 13 *pMaxLength = 3000; //Placed on the heap 14 15 char Species[*pMaxLength]; 16 char Locationfound[*pMaxLength]; 17 char Description[*pMaxLength]; // is just char Species[3000]; char Locationfound[3000]; char Description[3000];
#3
Re: Error In File Print Out
Posted 04 August 2012 - 09:57 AM
Salem_c, on 04 August 2012 - 02:24 AM, said:
http://www.cplusplus...istream/ignore/
So, what do you think all those ignore calls are doing?
37 cin.ignore(1,'\n');
38 cin.getline(Locationfound, *pMaxLength);
Bearing in mind that getline() uses \n as a delimiter anyway.
Then there is this horror...
Try using a loop, such as
Why bother with all the new/delete?
Quote
The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.
So, what do you think all those ignore calls are doing?
37 cin.ignore(1,'\n');
38 cin.getline(Locationfound, *pMaxLength);
Bearing in mind that getline() uses \n as a delimiter anyway.
Then there is this horror...
29 Top:
30 cout << endl;
31 cout << "Enter The Taxanomic Name For The Diatom: ";
...
52 if(Var == 1)
53 {
54 goto Top;
55 }
Try using a loop, such as
do {
cout << endl;
cout << "Enter The Taxanomic Name For The Diatom: ";
// rest of looping code
} while ( Var == 1 );
Why bother with all the new/delete?
12 unsigned long int *pMaxLength = new unsigned long int; 13 *pMaxLength = 3000; //Placed on the heap 14 15 char Species[*pMaxLength]; 16 char Locationfound[*pMaxLength]; 17 char Description[*pMaxLength]; // is just char Species[3000]; char Locationfound[3000]; char Description[3000];
Hey thanks, sorry for the messy code, I was just trying to bang something out real quick. I will most certainly change goto, into a do..while statement.
#4
Re: Error In File Print Out
Posted 04 August 2012 - 12:02 PM
> Hey thanks, sorry for the messy code, I was just trying to bang something out real quick.
Unless you manage to get something "quick" to work the first or second time, the safest thing to do is just delete it and start again more slowly.
Quick hacks have a habit of rapidly turn into piles of crap, and no amount of attempts at improvement will make something good.
Unless you manage to get something "quick" to work the first or second time, the safest thing to do is just delete it and start again more slowly.
Quick hacks have a habit of rapidly turn into piles of crap, and no amount of attempts at improvement will make something good.
#5
Re: Error In File Print Out
Posted 04 August 2012 - 12:44 PM
[My apologies if the following sounds like I'm a bigot against scientists and mathematicians writing code.]
In my few months here at DIC, I've seen those "quick hacks" become institutional centerpieces. I've asked: "Why not take time to clean up the code and do it right?" The response has always been "the code has been worked on by X number of scientists/mathematicians/etc over the past Y number of years. Changing the code now will just confuse them because they are scientists/mathematicians/etc and their main job is to solve problems, not write code."
While you have the time now, take time to do it right.
In my few months here at DIC, I've seen those "quick hacks" become institutional centerpieces. I've asked: "Why not take time to clean up the code and do it right?" The response has always been "the code has been worked on by X number of scientists/mathematicians/etc over the past Y number of years. Changing the code now will just confuse them because they are scientists/mathematicians/etc and their main job is to solve problems, not write code."
While you have the time now, take time to do it right.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|