Hello theforgottenone4,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Now that I have had the opportunity to test your program I see some of your problems and that does not include the new problems that have been created.
When I said that you should use "double" instead of "float" I meant everywhere not just in "main" using the code I showed you.
Now you have created the problem that "main" sends a "double" to a function which defines the variable as a "float". This will produce a warning that there could be possible data loss when you try to put something larger, (the "double"), into something smaller, (the "float"). This will not stop your program from running, but could produce an incorrect answer.
In "main" you have opened a file stream to your file, but in the function "myFunction" you start with opening the stream again. I did not sest that part, so I am not sure if that would even work.
Your choices are to open the file stream in "main" and pass that stream to the functions that need it, this is the better choice, or open the file stream in each function that will need it this creates redundancy which should be avoided.
The functions "perimeter" and "area" both return a value and work, but the functions "type" and "largesmall" both return a floating point number. Why? There is nothing in either function that requires a return value. These functions should be at the least "void" functions or return a "std::string". The code
return(0);
should be written as
return(0.0);
to be proper since it is a floating point number. Although you do not need it in the first place.
The whole of the "largeamall" function does not work wrll. The logic is wrong and does produce output that should bot be.
An example:
Side x: 5.25
Side y: 5.25
Side z: 5.25
The perimeter is: 15.75
The area is: 13.78
Equilateral Triangle
Side X is the largest side at 5.25.
All sides are equal, so side Z,X,Y are all the same size
Side X is the smallest side at 5.25
Press Enter to continue:
|
The line "All sides are equal, ..." says it all. Saying that "side x" is the longest and the shortest is a bit redundant. The if statements need rearranges or changed to form the proper output.
In the function "area" there is no square root in figuring the area. What I have found so far is A = 1/2 * B *H
b or A = 0.5 * B * H
b where B = base and H
b = height. Your use of the square root produces the wrong result. There is the use of square root when figuring the height, but alas I do not have a good understanding of that yet.
Out of the three variables which one do you consider the base and do you need something to figure the height before you use it? It does look like the square root is used in figuring the height. In which case what you have may work, but I am not sure.
Should you use the functions "sqrt" or "pow" you will need to include the header file "cmath". Do not relay on this being done for you through another header file.
Some parts of the program need to be rewritten and you need to decide how to deal with the file stream.
You say
but there's no output in my text file |
. Of course there is no output because of the way you have opened the file stream twice. This is usually a problem. This is just one function, but should give you an idea:
1 2 3 4 5 6 7 8 9 10
|
void myFunction(std::ofstream& outFile, double side1, double side2, double side3)
{
std::cout << "\n Side x: " << side1 << std::endl;
std::cout << " Side y: " << side2 << std::endl;
std::cout << " Side z: " << side3 << std::endl;
outFile << "\n" << std::left << std::setw(19) << " Side x: " << std::right << std::setw(5) << side1 << std::endl;
outFile << std::left << std::setw(19) << " Side y: " << std::right << std::setw(5) << side2 << std::endl;
outFile << std::left << std::setw(19) << " Side z: " << std::right << std::setw(5) << side3 << std::endl;
}
|
By passing the file stream to the function it stays open in "main" and you just keep adding to the file. If you open the stream in the function you will have to tell it to append the file otherwise it would open the file and overwrite what is there starting from the beginning of the file and that is not what you want.
In the end it would be better to collect your data and call one function for your output be it to "cout" the file or both.
Hope that helps,
Andy