XCode is not getting input from the file. C++ help needed.

Discussion in 'Mac Programming' started by petercdcn, Apr 8, 2010.

  1. petercdcn macrumors newbie

    Joined:
    Apr 8, 2010
    #1
    Hey everyone,
    I am just starting my C++ class here, and also new to Xcode programming. The following code can work in all other programming platform, but not Xcode. Can anyone help?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    int const maxSize = 100;
    int const maxLength = 30;
    
    struct NameType {
    	int grade;
    	char lastName[maxLength];
    	char firstName[maxLength];
    };
    
    //function prototypes
    bool readData( NameType[], int& );
    
    int main ( ) {
    
        NameType names[maxSize];
    	int size = 0;
    	
    	bool success = readData(names, size);
    	cout << success << endl;
    // Should print out the entries in the data file
    	cout << size << endl;        
    	
        return 0;
    }
    
    bool readData( NameType names[], int& size ) {
    	ifstream infile("sampleInput.txt");
    	if( !infile ) {
    		cout << "File cannot open" << endl;
    		return false;
    	}
    	
    //can't pass the infile part test. only in Xcode...
    	while (size < maxSize && infile >> names[size].lastName
    		   >> names[size].firstName
    		   >> names[size].grade       )     
    		size++;
    	return true;
    }
    Here is my sampleInput.txt file:

    frog freddie 60
    duck donald 65
    mouse minnie 95
    mouse mickey 50
    ghost casper 95
    mouse abby 80

    Any thought? Xcode bug??
    thanks,
     

    Attached Files:

  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    In the "Project" menu select "Edit Active Executable <your project name here>".

    Make sure the "General" view tab is selected.

    Set the radio-button group "Set the working directory to" "Project Directory" if the file "sampleInput.txt" is sitting in the same folder as your Xcode project file.
     
  3. petercdcn thread starter macrumors newbie

    Joined:
    Apr 8, 2010
    #4
    Thanks for you reply, but that's not the problem. If the program can't get to the data file. It will print out value "File cannot open."

    The program is finding the file fine, but it is not reading it correctly. If I change the path to absolute, it still won't print out the right 'size.'
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    Which version of Xcode are working with?

    The other problem chown33 pointed out is this one.
     
  5. petercdcn thread starter macrumors newbie

    Joined:
    Apr 8, 2010
    #6
    That helped, just need to delete those two things.
    thank you.
     
  6. petercdcn thread starter macrumors newbie

    Joined:
    Apr 8, 2010
    #7
    Thanks everyone, problem solved.
    Glad to use Mac Forum, first time to use it.
     

Share This Page