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

petercdcn

macrumors newbie
Original poster
Apr 8, 2010
4
0
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,
 

Attachments


lloyddean

macrumors 65816
May 10, 2009
1,001
16
Des Moines, WA
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.
 

petercdcn

macrumors newbie
Original poster
Apr 8, 2010
4
0
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.
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.'
 

petercdcn

macrumors newbie
Original poster
Apr 8, 2010
4
0
Thanks everyone, problem solved.
Glad to use Mac Forum, first time to use it.