Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Recommended Posts

Newbie mistake.

It's okay though, we all make them.

Your function returning a double is fine, but you're using it just a little bit wrong. Think of a function like a black box that does stuff. It has an out end and an in end like this:

 

out function_name (in)

 

so in our example, readtext would be used like this

 

code:

ifstream myfile;

double data = readtext(myfile);

cout<< "My data is: "<<data<<endl;

ifstream can be thought of as "in file stream" ie. the file streams in to the program. It is a class, which can be thought of as a "kind of thing" or "platonic ideal" if you're into that kind of philosophy. Basically myfile is a single example of an ifstream, much like Miles the Company Cat is a single example of cat. We call this an "instance of a class." Ie. Miles is an instance of the class Cat.

 

Basically, it's incorrect to call ifstream a function.

 

Here's a more applicable example to what you're doing. Be advised I haven't tested this, merely typed it into a text box on a web form, so there could easily be a typo:

 

code:

#include <iostream>

#include <fstream>

using namespace std;

 

double readtext(ifstream & in); //function prototype (ifstream is passed by reference

 

void main() //this is your m_main

{

ifstream myfile; //a stream to use later

double alpha = 0.0; //alpha is LOCAL to main

 

alpha = readtext(myfile); //alpha's value is set

 

cout<<"Alpha is returned as: "<<alpha<<endl;

}

 

double readtext (ifstream & in)

{

double temp = 0.0; //temp is LOCAL to readtext

in.open("axis_alignment.txt"); //open data file

if (in.is_open())

{

cout<<"File has opened"<<endl;

in>>temp;

cout<<"Alpha has been read as: "<<temp<<endl;

in.close();

cout<<"File has closed"<<endl

return temp; //the value of temp is given main

}

else cerr<< "Unable to open file";

}

Link to comment
Share on other sites

Jon. First, thanks a bunch for the newbie readable explanation. I understand better now what the ifstream is doing. The tutorials and my reference book made it seem more like a function. They also didn't use a class as an argument, only int types. I'll use it like a class now.

 

I'm not sure if I understand how "in" is being used. I thought that "in" was basically an instance of ifstream, like your "myfile" example. Is the only difference that "myfile" is the instance used in m_main() and "in" is used in readtext()?

 

Why is the function prototype needed? Is it the same to simply place the readtext function above the m_main function?

Link to comment
Share on other sites

I was able to compile without errors with many of the suggestion in the sample code above. I used 'myfile' in m_main and the 'in' instance in the readtext function. After running the Chook with debug mastercam.exe I got what I thought was a stop based on a breakpoint. I'm not sure where I should see cout, command prompt? I did not see any of the messages that I included from your example above. So I have no idea at what point the debugger is failing. I have the following errors:

 

1) Debug Assertion Failed.

File: f:vs70builds3077vcMFCATLshipatlmfcincludeafxwin1.inl

Line: 29

 

2) Unhandled exception at 0x01fe8b69 (mfc71d.dll) in Mastercam.exe: User breakpoint

 

The first error comes up each time I run the Debugger Mastercam.exe.

 

The second I figure must be one of the couts that were included. But I can't tell how. I must not be debugging correctly.

Link to comment
Share on other sites

Function prototypes are technically unnecessary in this example, but explode in usefulness the bigger your project gets, and it's best to get it out of the way, and the right habbits working from the get go.

 

Here's why: You can't use a function before it is declared, and a prototype declares a function without defining it (writing it all out).

 

Let's say you have a main, and then functions A and B. Under some control paths, A will call into B, and under others, B will call into A. Which do you put first? Without a prototype, there isn't a correct answer. With prototypes, the answer is that it doesn't matter.

 

The variable in could have been named anything, I gave it a different name than myfile mostly to show you that it was local to the function readtext. One of the biggest newbie mistakes is to look at 2 different variables with the same name, and wonder why the data in them is different. It's because one of them lives inside one fuction, and one lives in another, and never the twain shall meet. The unsophisticated user assumes they are the same, but they're just given the same name.

 

To move data around, we use function calls (arguments) and return the new data.

 

As for how in is being used, the >> operator, also known as the stream extraction operator, pulls data from a stream, and sticks it into a variable (or another stream). Think of it as being a flow, much like you'd see 1+2+3 = 6, you can have in >> firstDouble >> secondDouble; to read two doubles from a stream.

 

The assertion errors you're getting aren't supposed to occur, and aren't the couts. Try hitting "Retry" on the dialog that pops up, and seeing what it shows you. A bit hard to say what's wrong with you're setup for sure without more info.

Link to comment
Share on other sites

Again, thanks for the newbie readable explanations. I believe I correctly deciphered 'myfile' vs. 'in' and their separate locations. I also believe I got the function call done correctly in main. A big thanks for all your help. Hopefully the next newbie will find this link and save you guys some time.

 

I'm working with Roger Martin on the debugger issue, because I think that is the last problem stopping me from getting this working.

 

Thanks again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...