I can't figure out why this doesn't exit the loop, MAN!

This is a discussion on I can't figure out why this doesn't exit the loop, MAN! within the C++ Programming forums, part of the General Programming Boards category; This code doesn't ever exit the loop and I'm not exactly sure why. Code: #include <iostream> #include <fstream> #include <vector> ...

  1. #1
    Shadow12345
    Guest

    I can't figure out why this doesn't exit the loop, MAN!

    This code doesn't ever exit the loop and I'm not exactly sure why.

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <conio.h>
    
    using namespace std;
     
    
    
    struct Triangle {
            float a, b, c, d, e, f, g, h, i;
    };
    vector<Triangle*> TriangleVector;
    
    int main(void) {
            ofstream fout;
            ifstream fin;
            fout.open("Formatted coords.txt");
            fin.open("Coordinates.txt");
            if(fout.fail()) {
                    cout << "Could not load coordinates" << endl;
                    cout << "Hit a key to escape" << endl;
                    getch();
                    return 0;
            }
    
            if(fin.fail()) {
                    cout << "Could not load coordinates" << endl;
                    cout << "Hit a key to escape" << endl;
                    getch();
                    return 0;
            }
      streampos Location = fin.tellg();
            int x = 0;
    
            while(!fin.eof()) {
            TriangleVector.push_back(new Triangle);
            fin.ignore(2, NULL);
            fin >> TriangleVector[x]->a;
            fin >> TriangleVector[x]->b;
            fin >> TriangleVector[x]->c;
            fin.ignore(5, NULL);
            fin >> TriangleVector[x]->d;
            fin >> TriangleVector[x]->e;
            fin >> TriangleVector[x]->f;
            fin.ignore(5, NULL);
            fin >> TriangleVector[x]->g;
            fin >> TriangleVector[x]->h;
            fin >> TriangleVector[x]->i;
            fout << TriangleVector[x]->a << endl;
            fout << TriangleVector[x]->b << endl;
            fout << TriangleVector[x]->c << endl;
            fout << TriangleVector[x]->d << endl;
            fout << TriangleVector[x]->e << endl;
            fout << TriangleVector[x]->f << endl;
            fout << TriangleVector[x]->g << endl;
            fout << TriangleVector[x]->h << endl;
            fout << TriangleVector[x]->i << endl;
      fin.seekg('\n');
      fin.seekg(1);
    
            cout << TriangleVector[x]->a << endl;
            cout << TriangleVector[x]->b << endl;
            cout << TriangleVector[x]->c << endl;
            cout << TriangleVector[x]->d << endl;
            cout << TriangleVector[x]->e << endl;
            cout << TriangleVector[x]->f << endl;
            cout << TriangleVector[x]->g << endl;
            cout << TriangleVector[x]->h << endl;
            cout << TriangleVector[x]->i << endl;
    
            getch();
            x++;
    
            }
    		cout << "You have reached the end of the file" << endl;
            getch();
            fin.close();
            fout.close();
            cout << "Hit a key to escape" << endl;
            getch();
                   
            return 0;
    }
    EDIT:
    When I take out the fin.seeg(1) it exits the loop but it only reads the first line then the rest is jibberish and I'm not sure why
    What it is doing is reading the same line of the text file over and over agqin inst4ead of actually going to the next line and reading from there.
    Last edited by Shadow12345; 10-02-2002 at 06:57 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Your problem is these lines:
    >fin.seekg('\n');
    >fin.seekg(1);

    How about reading an entire line into a string and then parsing it? That tends to save you a great deal of trouble.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Shadow12345
    Guest
    Could you show me how to do this?
    Last edited by Shadow12345; 10-02-2002 at 09:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. Cannot exit 'while' loop
    By s_ny33 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2005, 03:59 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. we of the cage
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-21-2002, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21