7 Replies - 1123 Views - Last Post: 16 January 2008 - 08:09 PM Rate Topic: -----

#1 tyguy2008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 07

Problem with project

Posted 14 January 2008 - 03:57 PM

Hey guys i am makeing a project for school that reads names of teachers from a .txt file and stores them in a vector. I seem to be having a problem with printing them out. Here is a copy of the code i have.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main () 
{

	int menu_choice;
	vector<string> teacher;
	string line;
	
	//--------- load vectors with name, subject, and room----------
	
	ifstream fileInput("teachers.txt");
	
	if(fileInput.is_open())
	{
		while(getline(fileInput,line)
		{
			teacher.push_back(line);
		}
	}
	fileInput.close();
	//------------------MENU-----------------------
	cout<< "Welcome to the Scavenger Hunt Randomizer."<<endl;
	cout<<endl;
	cout<< "What would you like to do?"<<endl;
	cout<< "(1) See/edit the list of current teachers"<<endl;
	cout<< "(2) Run the program"<<endl;
	cin>> menu_choice;
	
	switch(menu_choice)
	{
		case 1:
			for(int i=0;i< teacher.size();i++)
			{
			cout<< teacher[i]<< endl;
			}
			break;
			

		case 2:



			break;
	}
}



If you guys could help me find the error i would be extremely greatful.

Thanks guys,
tyguy2008

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with project

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Problem with project

Posted 14 January 2008 - 06:15 PM

it always helps to diagnose problems if you could paste the compiler errors along with your code.

However, just by skim-reading, here's one which jumped out at me - You're missing a curvy bracket at the end of this line
		while(getline(fileInput,line) 


There might be other errors too, but its best to tackle them one at a time. If you continue to get problems, show us the compiler's output, and we'll carry on from there.
Was This Post Helpful? 0
  • +
  • -

#3 tyguy2008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 07

Re: Problem with project

Posted 14 January 2008 - 06:28 PM

View PostBench, on 14 Jan, 2008 - 06:15 PM, said:

However, just by skim-reading, here's one which jumped out at me - You're missing a curvy bracket at the end of this line
		while(getline(fileInput,line) 




ok if you look under that line there is and there are no errors... the program just isnt printing out the list of teachers it reads in and stores in the vector.... I had a friend run it on his comp and it works fine... but i have a feeling that because im on a MAC there is some problem with vectors or something.... anyone familiar with c++ on MACs?
Was This Post Helpful? 0
  • +
  • -

#4 nirvanarupali   User is offline

  • D.I.C Stomach
  • member icon

Reputation: 14
  • View blog
  • Posts: 1,120
  • Joined: 01-August 07

Re: Problem with project

Posted 14 January 2008 - 08:02 PM

I spot an error, the same as Bench has told you. Did you copy and paste this codes? because you cannot cannot compile that codes unless you put the missing curvy bracket on while loop.

What compiler are you using on Mac?
C++ ISO, is not platform dependent so you don't worry what OS you are using.
Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Problem with project

Posted 15 January 2008 - 04:10 AM

I still see it as an error (And so does my compiler)


Although, assuming you've just mis-pasted the code without the bracket and the rest is the same - one possibility is that your program is not able to find a file called teachers.txt

Try modifying your if block to report back whether it failed to open the file
	if(fileInput.is_open())
	{
		while(getline(fileInput,line))
		{
			teacher.push_back(line);
		}
	}
	else
	{
		cout << "Unable to open file" << endl;
	} 

Was This Post Helpful? 0
  • +
  • -

#6 tyguy2008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 07

Re: Problem with project

Posted 16 January 2008 - 11:56 AM

Hey guys,
I'm using Xcode and i can compile it fine..... i figured out that it is a problem with opening the file but i've checked the name and path 100 times and can't seem to figure out why the file cannot be opened.
Was This Post Helpful? 0
  • +
  • -

#7 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Problem with project

Posted 16 January 2008 - 06:14 PM

If you're having file name troubles, Try using C++ to create the file for you, in a separate "toy" program. Feel free to use this code

#include <iostream>
#include <fstream>

int main()
{
	using namespace std;

	ofstream outfile("teachers.txt");
	if( outfile.is_open() )
		outfile << "This is an attempt to force a file called" 
				<< " teachers.txt to be created, with only"
				<< " this line of text inside";
	else
		cout << "Error writing file";
} 

You might well find that this creates a second file in your program's folder which appears to have the same name.
- I suspect the problem is some subtle difference between what your O/S is showing you to be the file name, and what the file name really is (Maybe the difference is an upper/lowercase char, or perhaps a hidden extension). This program will force a file to be created under the name you want. - You can check that it worked by opening the file in a text editor. If the file doesn't appear where you expect it to appear, then it may be an environment issue.

This post has been edited by Bench: 16 January 2008 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 tyguy2008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 07

Re: Problem with project

Posted 16 January 2008 - 08:09 PM

Hey thanks, the program u gave me didnt directly help my problem but i figured it out =)
Who would have known that you have to put the file in the debug folder haha.... Thanks for all ur help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1