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.

Attachments
instance fsapA_R16_D128_S4_9
res 1 0 480 137.29 172.03 1 4
res 2 0 480 63.348 32.983 4
res 3 0 480 188.49 170.05 1 2 3 4
res 4 0 480 77.649 46.49 1 2 3 4
res 5 0 480 86.005 124.16 3
res 6 0 480 184.72 174.99 1 2 3 4
res 7 0 480 66.077 160.03 1 2 3 4
res 8 0 480 1.5015 167.41 1 2 3 4
res 9 0 480 110.76 4.0466 1 2 3 4
res 10 0 480 136.52 39.88 1 2 3 4
res 11 0 480 89.893 130.1 1 2 3 4
res 12 0 480 94.818 49.884 1 2 4
res 13 0 480 31.628 186.52 3
res 14 0 480 123.66 73.853 1 3 4
res 15 0 480 125.13 108.87 1 2 3 4
res 16 0 480 61.755 52.002 1 2 3 4 
dem 9 60 8.0261 5.3894 1 0 480 1000
dem 10 150 32.257 37.064 1 0 480 1000
dem 11 30 97.311 2.5238 1 0 480 600
dem 12 30 89.105 25.623 1 0 480 1000
dem 13 150 88.821 12.613 1 0 480 1000
dem 14 150 69.202 3.3234 1 0 480 700
dem 15 60 66.208 28.973 1 0 480 1000
dem 16 60 37.189 75.198 1 0 480 600

>>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.


}
This article has been dead for over six months. Start a new discussion instead.