Hey,

I'm trying to only read the int's and the symbol - (negative) from the file.

right now my code is where m,d, and y are dates.
Everything works fine. Except I do not read the - symbol.

Any one know how to solve this?

fin.open(ifile.c_str());
fin >> m1 >> c >> d1 >> c >> y1;
fin >> m2 >> c >> d2 >> c >> y2;
fin >> m3 >> c >> d3 >> c >> y3;
fin.close();

For example date -10/-1/1999

will be read as 10/1/1999

for my assignment I must read the negative symbol.

Thanks

What are the data types of the variables?

With the mdy variables as ints and the c as a char, it seems to work for me - I get the negative values.

A full code sample illustrating your problem will help us help you.

Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	

    
	if ( !ifile.size() == 0 )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());

That should work. Please post the exact data file contents you're trying to read.

edited original post.

ifile is a string.

I've also tried

if ( !ifile.empty() )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

You're asking a question that's not exactly what I think you mean. if ( (! ifile.size( ) ) == 0 ) means, logically negate the value the size function returns, giving you a boolean true or false value, then compare that to zero, which is considered false.

If you're trying to find out if the user actually entered some string as the file name, try

if( ifile.size( ) == 0 )
{
   //error handler
}
else
{
    fin.open( ifile.c_str( ) );
}

If failed to open..

if (fin.fail())
   cout<<"file does not exist";

And, the test you're doing is only testing the filename the user entered, it does not test if there's actually a file or if it has any content.

(this should have come after my next reply - they got out of order somehow)

I've tried those. They don't work.
This is my problem.
Lets say the user enters hello.txt
but hello.txt does not exist.
How can I make the program say "Does not exist"
That is what I have been trying to do.
all the previous methods don't do that, they just make the dates really long numbers.

Hope that makes more sense.

Still can't figure it out.

What about..this...

//...
fin.open(ifile);
if(fin.fail())
   std::cout<<"does not exist";
//...

every text should cover this

fin.open ( "somefile.txt" );
if( !fin )  // or   if( fin.fail( ) )
{
     //error handler  goes here
}
else
{
    //file opened successfully, start reading
}

Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	

    
	if ( !ifile.size() == 0 )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());

just try this one...........................

ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	
        fin.open(ifile.c_str());
       
	 if(fin.fail())
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());

Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.
Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.

void extract ( string file_name, ifstream& fin )
{
	int i = 0;
	string temp;

	
	fin.open( file_name.c_str( ) );
	fin >> temp;
	cout << temp;
	
	while ( !fin.eof( ) )[B]//THIS PART DOES NOT WORK I've also tried while (fin) while (!fin.fail( )) can't seem to get it to work[/B]
	{
		//STUFF
	
	}

What I want it to do is end the program, when there is no more info in the file.

Thanks.

alright I figured that out.

It seems as though that method will only work when followed by

fin >> somevariable;

is there anyway I can use getline (fin, string);

instead?

Thanks.

while ( getline ( fin, str ) )
{
//do any other processing
}

If the read action is successful, you enter the loop body. If the read action fails, loop ends.

I'm seem to have a LOT of problems with this program.
The loop issue is fixed, now it will not read all the numbers that exist.

The Main:

string file1, temp[1000];
	float n1[1000], n2[1000];
	int i = 1, t;

	ifstream fin;
	ofstream out;
	
	cout << "\t\t Welcome to Grade Calculator!\n\n";
	cout << "What file has your scores on it: ";
	getline( cin, file1 );
	
	fin.open( file1.c_str( ) );
	out.open( "results.html" );

	if ( fin.fail( ) ||  file1.size( ) == 0 )
	{
		cout << "No file was found or entered." << endl;
		system ("PAUSE");
		return 1;
	}

	out << "<html>" << endl;
	out << "<body>" << endl;
	out << "<pre>"  << endl;
	out << "\t\tGrade Report" << endl;
	out << "Activity     Your Grade       Possible" << endl;

	while ( getline ( fin, temp[i] ) )
	{
		fin >> n1[i] >> n2[i];
		i++;
	}

	while ( i != 0 )
	{
		if (temp[i] == "end")
		{
			out << "Percentage" << setw(13) << getpart(fin, temp[i-1].substr(0, ' ')) << endl;
		}
		else if ( n1[i] != -1 || n2[i] != -1 )
		{
			out << setw(11) << temp[1] << setw(14) << n1[i] << setw(16) << n2[i] << endl;
		}

		i--;
	}

	out << "</html>" << endl;
	out << "</body>" << endl;
	out << "</pre>" << endl;

	fin.close( );
	out.close( );

	system ("PAUSE");

	return 0;

The getpart function

float getpart (ifstream& fin, string label)
{
	string temp;
	float score = 0, total = 0, average, tem, n1, n2; 
	
	while (getline ( fin, temp ) ) 
	{
		if (temp != "end")
			if (temp.substr( ) == label)
			{
				fin >> n1 >> n2;
				score = score + n1;
				total = total + n2;
			}
	}

	average = score / total;
	
	return average;
}

The file it is reading from

Quiz 1
3 5
Quiz 2
4 10
Attd. 11-23
10 10
Quiz 3
5 5
end
-1 -1
Lab 1
10 100
Lab 2
20 20
Lab 3
30 130
end
-1 -1
Test 1
145 150
Test 2
200 210
end
-1 -1
Program 1
40 50
Program 2
90 100
Program 3
100 100
Program 4
90 100
end
-1 -1
Lab Test 1
35 50
Lab Test 2
25 50
end
-1 -1
Final Exam
234 400
end
-1 -1

Basically what the program does is organize the grades in another file .html
ie
Quiz 1 score total
and then calculate the percent in the end of each section
Percent: 100

I ran some tests, and it looks like the array is only storing 2 variables, of which the first one is correct the second is wrong.

this is the part where I actually store information, anyone know whats wrong?

Thanks.

while ( getline ( fin, temp[i] ) )
	{
		fin >> n1[i] >> n2[i];
		i++;
	}
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.