4 Replies - 1331 Views - Last Post: 29 March 2009 - 01:56 PM Rate Topic: -----

#1 Hayling   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Outputting a user-defined function to a file

Post icon  Posted 28 March 2009 - 12:23 AM

Ok so I defined my function:

void PosType(char position)
{
	switch (position)
	{
		case 'A':
			cout << "Management";
			break;
		case 'B':
			cout << "Supervisor";
			break;
		case 'C':
			cout << "Clerical";
			break;
		default:
			cout << "Invalid input.";
	}
}



and I can call the function in my main() no problem. It prints out correctly:

char Class;
cin >> Class; // the letter A, B, or C

cout << "Class is: \n";
PosType(Class);



but a part of my assignment is to output my user-defined function PosType(Class) to a file. So I use:

ifstream fin;
ofstream fout;

fin.open("infile.dat");
fout.open("outfile.dat");

fin >> Class; // read class from infile.dat (say it's A for this instance)
fout << PosType(Class); // output user defined function to output.dat (A would output 'Management' to output.dat)



this outputs all kinds of errors. So I try to assign PosType(Class) to a variable:

char x = PosType(Class);
fin >> Class;
fout << x;



but this errors out saying:

error C2440: 'initializing' : cannot convert from 'void' to 'char'

AHHH this is driving me nuts. I can't get it to work. Can somebody please help me? I tried to simplify all the code for this post just so the actual problem could be addressed.

Is This A Good Question/Topic? 0
  • +

Replies To: Outputting a user-defined function to a file

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Outputting a user-defined function to a file

Posted 28 March 2009 - 06:05 AM

View PostHayling, on 27 Mar, 2009 - 11:23 PM, said:

error C2440: 'initializing' : cannot convert from 'void' to 'char'


Your function signature is:
void PosType(char)



The function returns nothing when called (void) so it can be no surpise that you are unable to assign that return value to a char variable here.
char x = PosType(Class);



Don't use "Class" (a reserved word) for a variable name

I don't really understand the rest of what you are asking (I suspect because you aren't too sure yourself) so it's hard to tell what you are trying to do.

Get rid of the obvious mistakes and really think about what you are trying to do and then ask in the clearest and simplest words you can.
Was This Post Helpful? 1
  • +
  • -

#3 Hayling   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Re: Outputting a user-defined function to a file

Posted 28 March 2009 - 08:36 PM

Ok after hours of consulting my book and racking my brain I finally figured it out. The solution wasn't all that complicated. I just needed to be outputting to the file within my function (fout) instead of outputting to the screen (cout).

void PosType(char position)
{
	ofstream fout;
	fout.open("outfile.dat",ios::app); // Specify output stream within function, set to append
	
	switch (position)
	{
		case 'A':
		case 'a':
			fout << setw(16) << "Management"; // fout instead of cout
			break;
		case 'B':
		case 'b':
			fout << setw(16) << "Supervisor";
			break;
		case 'C':
		case 'c':
			fout << setw(16) << "Clerical  ";
			break;
		default:
			fout << setw(16) << "Error";
	}
	fout.close( ); // Close output stream
}



Now I can just call the function within my main() and it automatically outputs to the file:

fin >> position;
PosType(position);



Oh and I took your advice on changing the variable name! Thanks!

This post has been edited by Hayling: 28 March 2009 - 08:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Outputting a user-defined function to a file

Posted 29 March 2009 - 06:16 AM

View PostHayling, on 28 Mar, 2009 - 07:36 PM, said:

Ok after hours of consulting my book and racking my brain I finally figured it out. The solution wasn't all that complicated. I just needed to be outputting to the file within my function (fout) instead of outputting to the screen (cout).


You do understand that "fout" only works because you gave your ofstream that name here:
	ofstream fout;


don't you?

'fout' only works because you provided the ofstream that name in your program.

If you had called the ofstream "fidoTheWonderDog" then you would have had to write fidoTheWonderDog where you have written fout.

Sorry if I am telling you what you already know.
Just wanted to be sure you are understanding why your answer works.

This post has been edited by janotte: 30 March 2009 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 Hayling   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Re: Outputting a user-defined function to a file

Posted 29 March 2009 - 01:56 PM

Yes, I knew that...but thanks anyway :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1