4 Replies - 841 Views - Last Post: 04 August 2012 - 12:44 PM Rate Topic: -----

#1 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

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?


 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();
        }
        
        }
        
        
        
};



Is This A Good Question/Topic? 0
  • +

Replies To: Error In File Print Out

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Error In File Print Out

Posted 04 August 2012 - 02:24 AM

http://www.cplusplus...istream/ignore/

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];


Was This Post Helpful? 1
  • +
  • -

#3 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Re: Error In File Print Out

Posted 04 August 2012 - 09:57 AM

View PostSalem_c, on 04 August 2012 - 02:24 AM, said:

http://www.cplusplus...istream/ignore/

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.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1914
  • View blog
  • Posts: 5,717
  • Joined: 05-May 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1