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

CuteBoA

macrumors newbie
Original poster
Mar 12, 2010
21
0
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main () {
	
	ifstream fin;
	fin.open ("Marks.txt");
	
	string n;
	double m;
	 
	 cout << "Number of students: " << numStuds;
	
	fin.close();
	
	return 0;
}



so, basically im trying to read the datas that are in the file, and then put it on the the arrays, and output how many datas there are..
if the datas are like this :
james 89
doni 80
ben 87

but, when i output it, the number of students stays 0. why is that?
 
So name and mark are arrays. You need to subscript them to use >>. you also need to put a lot more than you have in the loop. You want to read into name and mark sub n.

-Lee
 
I see you've edited your original post to reflect changes implied with lee1210 response.

My question would be - have you determined if it successfully opened the file "Marks.txt"?
 
yes, ive checked it...
the program works fine when i tried to compile it in microsoft's computer..
so, whats wrong with it?
 
but, when i output it, the number of students stays 0. why is that?

I had this problem for my C++ course. My teacher warned us against using the 'string' function when using the array. He basically told us you'll get an F.

Long story short, we had to use

Code:
#iostream
#fstream
using namespace std;

char students[50];
int grades[50];
.
.
.

void input_stu_n_grades(char students[], int grades[], ....){
fin.open("filename.txt")
if {
.
.

}
else
.
.
}

void output_file(students, grades, ....){
.
.
}

void output_screen(....){
.
.
.
}

int main(){

input_stu_n_grades(students, grades, ....);
output_file
output_screen

return 0;
}

Along those lines... Try using it that way?
 
How can u use char to read it?

because we need to read " Jean" but not only "J" right?
how does it make sense then?

How can it run and work perfectly fine with microsoft's compiler when it doesnt even work here when i used XCODE? any thought about it?
 
Turbo C (by Borland) is really old. How are you running it "in Turbo C" on your mac..? I'm not aware of any port of BGI (Borland Graphics Interface, if I remember correctly) to current Macs. Perhaps you can check here?
 
yes, ive checked it...
the program works fine when i tried to compile it in microsoft's computer..
so, whats wrong with it?

I believe the file is NOT being opened successfully not because it doesn't exist but because it is not where you, or the code, thinks it is!

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 "Marks.txt" is sitting in the same folder as your Xcode project file.

Run it again, using the Debugger is necessary.
 
I believe the file is NOT being opened successfully not because it doesn't exist but because it is not where you, or the code, thinks it is!

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 "Marks.txt" is sitting in the same folder as your Xcode project file.

Run it again, using the Debugger is necessary.
what do u mean by Set the radio-button group "Set the working directory to" "Project Directory" if the file "Marks.txt" is sitting in the same folder as your Xcode project file.?? i cant find it anywhere


EDIT : OMG.. ive found it and it WORKS properly.. thanks alot lloyddean for your help!! because this so important as my teacher's assignment!! thanks alot!!!!
so, whenever i want to call a file , I will need to set the working directory to " PROJECT DIRECTORY"? is it like that?

thanks!!!
 
what do u mean by Set the radio-button group "Set the working directory to" "Project Directory" if the file "Marks.txt" is sitting in the same folder as your Xcode project file.?? i cant find it anywhere


EDIT : OMG.. ive found it and it WORKS properly.. thanks alot lloyddean for your help!! because this so important as my teacher's assignment!! thanks alot!!!!
so, whenever i want to call a file , I will need to set the working directory to " PROJECT DIRECTORY"? is it like that?

thanks!!!

During development when invoking CLI tools from within Xcode - YES!

If invoked from the terminal change your working directory to the one containing the data file, or supplying a complete path/name pair, and then invoking the executable and it'll of course work as expected.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.