Hi, i have been working on trying to do this for a while now. I applied for a job in c++ and the person gave me a task to test my skill... needless to say that i didn't get the job but i am still working on it to figure out how it should work... and the main problem i have been having is reading from the file correctly. my code is as below and i have attatched a test file for use... the overall program is supposed to look at the res and dem and then try to work out the best use of resources. i got a little ahead of myself when i applied for the job i will admit but now is the time for learning from that. if anyone has any ideas as to how to get it to work right at the moment i just need it to display right it needs to read as follows :
res <id><start time><finish time><location x><location y>< skills(n)>
dem <id><duration><skill><start time><finish time><location x><location y><revenue>

#include <iostream>
#include <fstream>
#include <istream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
typedef unsigned int uint;

int main()
{
char filename[256];
cin.getline(filename, 256);

ifstream currentfile;
currentfile.open(filename);
string type;
uint id;
uint starttime;
uint finishtime;
float locx;
float locy;
string instancename;
uint duration;
uint skill;


while (currentfile)
{
	if (currentfile == "instance")
	{
		currentfile >> instancename;
	}
	if (currentfile == "res")
	{
		type = "res";
		currentfile >> id >> starttime >> finishtime >> locx >> locy;
	}
	if (currentfile == "dem")
	{
		type = "dem";
		currentfile >> id >> duration >> skill >> locx >> locy >> starttime >> finishtime;
	}

if (type == "res")
{
	cout << type << "\n";
	cout << id << "\n";
	cout << starttime << "\n";
	cout << finishtime << "\n";
	cout << locx << "\n";
	cout << locy << "\n";
}
if (type == "dem")
{
	cout << type << "\n";
	cout << id << "\n";
	cout << duration << "\n";
	cout << skill << "\n";
	cout << locx << "\n";
	cout << locy << "\n";
	cout << starttime << "\n";
	cout << finishtime << "\n";
}

}
	system("PAUSE");
	return 0;

}

it reads the file but doesn't store it to the variables they way i need it to.... once i get that part sorted i need to work on making class arrays so that i can use the information to analyse the data for the rest of the application.

any help would be appreciated... i have searched a few of the threads here..... searched the internet... and even all my course material and nothing seems to help that much. Any help is appreciated.

>>if (currentfile == "instance")

currentfile is an ifstream object, so it can not be compared to a string. Do you mean filename instead of currentfile ? If yes, then you will have to use strcmp() to compare the two character arrays.

>>if (currentfile == "instance")

currentfile is an ifstream object, so it can not be compared to a string. Do you mean filename instead of currentfile ? If yes, then you will have to use strcmp() to compare the two character arrays.

i tried what you suggested and that didnt work so i found some code to read the file which does work and it reads the file into a string which is as follows:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	char filename[256];
	string instancename;
	string line;
	cout << "Please enter the location and name of the file that you want to open:\n";
	cin.getline(filename,256);

	ifstream myfile (filename);
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
	cout << line << "\n";
	}
    myfile.close();
  }

  else cout << "Unable to open file"; 
	system("PAUSE");
  return 0;
}

is there any way to get it to read from a string into the different variables as i mentioned before?

use the >> operator instead of getline() Here is one way to do it.

int main()
{
    uint id;
    uint starttime;
    uint finishtime;
    float locx;
    float locy;
    string temp;
    string instancename;
    uint duration;
    uint skill;

    ifstream in("test1.txt");
    if( !in.is_open() )
    {
        cout << "Can't open the file\n";
        return 1;
    }
// read header line
    in >> temp; // "instance"
    in >> instancename;
//
// now put the below in a loop until end-of-file
//
    in >> temp; // "res"
    in >> id;
    in >> starttime; 
    // etc. etc.


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.