The online racing simulator
Anyone Know Basic C++?
(19 posts, started )
Anyone Know Basic C++?
Ok, long story short, I'm trying to do some basic C++ and I'm stuck. I'm trying to output stuff to a txt file, but the way they want us to do it we need to split everything into functions. So I have the commant to open the output stream:

ofstream output("triangle.dat");

Then further down I have a command for the program to go to a void function, and then after the main function I have the void function itself:

void IsoscOrScale (int A, int B, int C) {
if (A == B || B == C || A == C)
output << "Isosceles ";
else
output << "Scalene ";

The problem I'm having is that since this is outside of the main function, it doesn't recognize the command to output those strings. I can re-define the output stream in this void function, and then it works, but I think re-defining it starts fresh, so it erases all the other stuff in the outputted .txt file and I end up with JUST that string.

If that doesn't make sense I can elaborate more, but basically how do I get it to continue to write to the output file when it goes down to a separate function? Thanks for the help from anyone who might know.
To make the output object visible in the function, you have to either make it global (declared outside of main()), or better yet, pass it to your function as a reference parameter (e.g. ostream& output).
It's an OO language so I pressume you are learning OO with it, so I would either extend off the stream class or create an object with its own member variable pointer to the stream in the constructor.
What happens is that you don't have the output variable in the function.
You have few ways you can solve the problem.
The best solution would be to use classes, but probably requires most work. You would need to make a class that handles the file writing operations and opening and closing of the file.
One way is to make the output variable a global variable. Not recommended!
Another one is to send the output as a parameter to the function. Which is probably the solution you should use here.
Its not just In C, but from your clear as mud description () I get the following.

You have an object that writes to the file. Then you call a method to write some other stuff, and you cant write to the file from that.

Solution is simple, and two-fold:

1) either pass the writer object as an argument to the method (so it would look something like this *pseudocode*: void krazyFucnktion (Object writer, [other args]){}

2) close the writer object, and declare a new one inside the method.
This is actually better, cleaner.

The idea is write stuff, close stream, open file again for writing, write more stuff... etc. Starting to pass everything as arguments to all funcions will give you HUGE headaches as you begin coding complicated stuff (I reviewed a 1st year student's code that had a function with 27 (yes TWENTY SEVEN) args.
Thanks for the quick replies guys. I'm just starting out with all this. for this application, I'll just make the output declaration global and we'll see if that works.
I wont give you the code as A) It appears to be educational, B) I dont use C++ and C) last time I posted some C++ code on these boards somebody corrected it ;P

So in pseudocode then

class myObject
method constructor
self.fileHandle=fileOpen
}
method weirdFunctionNameThing
if ( A==B || A==C || B=C )
write self.fileHandle , whateverOutput
}
method deconstructor
self.fileHandle.closeFile
}
}

n=new myObject()
n.weirdFunctionNameThing()

EDIT: this is meant to the OP, not becky, as becky's code is pretty damn on the money.
DONT.

Thats as elegant as a drunk elephant in a china shop.

DO NOT start by creating bad habits.

You can also create a huuuuge string that will have all the info you want to write to the file. your smaller method/function can simply return a string.

IMPORTANT: RETURN a string. do not change the string build built directly - thats a no-no.
Your method declaration should be somewhat like this:

String myMethod (int arg1, int arg2, int arg3){}

I had a teacher that flunked a student in his very FIRST class cause...
...

he declared a global...
int i;

and that was a 2nd year student.
So I should instead just have something in the order of:

output << IsoscOrScale(A, B, C);

which would refer to the function:

string IsoscOrScale (int A, int B, int C) {
if (A == B || B == C || A == C)
return "Isosceles ";
else
return "Scalene ";
}

Thing is, how do I employ a "string" function. It doesn't want to work when I use string - only int.
I dunno how the finer details of c++ are, but in the usual manner there are no "string" functions:

This is java (should be VERY close to c++)


String myFunction (String argument){
return argument+"this is my argument";
}
...

String myString = "";

myString = myFunction ("hello World");

this code should return "hello Worldthis is my argument"


a function RETURNS a value, you can do whatever you want with it - or nothing at all, even.

Note as the function is attributed to a string - bascially the function does something or other, then returns a string. That string is then "put" inside the myString variable.
you need to
#include <string>
to make string work.
Think of a function as a black-box factory.

Oil, rubber and steel go in, cars emerge on the outside.

That is all the "outside" program sees. Only the dudes inside the factory actually see what goes on. All the main prog cares is what goes in, out comes out, and what to do with it.
Quote from geeman1 :you need to
#include <string>
to make string work.

You'll need to add
using std::string;

or
using namespace std;

as well. Including String just lets you use its methods, it doesn't declare it so you can use it.
Alright I got it working. My stupid problem was I had "using namespace std;" AFTER my function declaration, lol. Thanks for the help eveyrone!

No more questions, but is it possible to have it read numbers from an input file directly as absolute values? Like instead of 'input >> A >> B >> C' something like 'abs(input) >> A >> B >> C' That's not how you'd do it obviously, but is this possible?
Quote from Crashgate3 :You'll need to add
using std::string;

or
using namespace std;

as well. Including String just lets you use its methods, it doesn't declare it so you can use it.

Or you can declare the variables as std::string instead of string.
Yeah, but that gets *really* annoying if you need to use it more than about twice
Quote from Crashgate3 :Yeah, but that gets *really* annoying if you need to use it more than about twice

True
I always wanted to know these kind of stuff like coding etc.

But now I know that it's nothing for me

Anyone Know Basic C++?
(19 posts, started )
FGED GREDG RDFGDR GSFDG