Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

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

  • sampleInput.txt
    92 bytes · Views: 109
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.
 
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.'
 
Thanks everyone, problem solved.
Glad to use Mac Forum, first time to use it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.