C++ project help!

Discussion in 'OT Technology' started by KoolKeith, Nov 17, 2006.

  1. KoolKeith

    KoolKeith Guest

    My program is supposed to open this file and output whats in it, my for loop is an infinite loop, why? This is what i have:

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;

    const int MaxNumItems = 50; //Dont worry why they are such values
    const int MaxLineLength = 42;
    int main()
    {


    ifstream inFile;
    string filename;
    char items[MaxNumItems][MaxLineLength];
    int row = 0;
    int column = 0;


    cout <<"Enter the File Name"<<endl;
    cin >> filename;
    cout << endl;

    inFile.open(filename.c_str()); //OPENS FILE
    if (!inFile) { //PROGRAM EXITS IF FILE
    CANNOT OPEN
    cout <<"Can't Open The Input File.";
    exit(1);
    }

    cout << "1"<<endl;
    for(int row = 0; row < MaxNumItems; row++) {
    for(int column = 0; column < MaxLineLength; column++) {
    inFile.get(items[row][column]);
    if (items[row][column] == '\n'){
    break;
    }
    }
    items[row][column]='\0';
    if (!inFile.eof()){
    break;
    }
    }




    for(int row = 0; row < 50; row++) {
    for(int column = 0; column < 42; column++) {
    cout << items[row][column];
    }
    }

    return 0;

    }
     
  2. KoolKeith

    KoolKeith Guest

    The txt file contains:

    10900
    10,567.90-Used pickup truck
    5000+Deposit
    75.00-Dooflingies for employees
    350.50-Toxic waste shipping
     
  3. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    why not use getline?


    oh it looks like you are doing more than just echoing a file, perhaps elaborate on what you need to do
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use getline and post your code with code brackets. if youre declaring int row, column; at the beginning take out the int in for (int row;row < blah , row++). I'm not sure they retain their value after that for loop to put the null character. either way use getline and a vector for the line array so you can make it dynamic in case you want to use other files.
     

Share This Page