Thread: why is this writing the last line twice to the file

  1. #1
    Shadow12345
    Guest

    why is this writing the last line twice to the file

    why is this writing the last line twice to the file
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <conio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    struct vertex {
    	float  x;
    	float  y;
    	float  z;
    };
    
    
    vector<vertex> vertices;
    
    int main(void) {
    int numverts = 0;
    int intmesh = 0; //THIS IS ADDED TO THE END OF THE OUTPUT FILE IN ORDER TO CREATE SEPARATE FILES FOR EACH MESH
    char charmesh[20];
    char outputfile[256];
    char outfileext[256];	//THIS IS THE EXTENSION FOR THE OUTPUT FILE (.CUF)
    char inputfile[256];
    char type = 'v';
    float x, y, z;
    ifstream fin;
    ofstream fout;
    
    cout.setf(ios::fixed);			//THIS PROBABLY ISN'T NECESSARY
    
    cout << "Enter the name of an output file (without the extension, no numbers)" << endl;
    cin.getline(outputfile, 256, '\n');
    cout << "Enter which mesh number this is" << endl;
    cin >> intmesh;
    _itoa(intmesh, charmesh, 10);	//CONVERT THE MESHNUMBER TO AN ALPHA TO ADD TO END OF FILENAME
    strcat(outputfile, charmesh);		//APPEND THE NUMBER TO THE END OF THE OUTPUT FILE
    cin.ignore();								
    cout << "Now enter the name of the extension (with the period)" << endl;
    cin.getline(outfileext, 256, '\n');
    strcat(outputfile, outfileext);
    
    cout << "Enter the name of the file to be read" << endl;
    cin.getline(inputfile, 256, '\n');
    
    fout.open(outputfile, ios::out);
    fin.open(inputfile, ios::in);
    if(fin.fail()){
    cout << "Error, input file could not be opened" << endl;
    return 0;
    }
    
    //READ IN THE CRAP FROM THE FILE
    while(fin.good() && type == 'v') {
    	fin >> type;
    	if(type == 'v') {
    		fin >> x >> y >> z;
    		fout << x << " " << y << " " << z << endl;
    	}
    	
    }
    
    fin.close();
    fout.close();
    
    return 0;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Change the last while to a do while.

    Example
    Code:
    do {
    
    } while(something);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM