hi, I searched for this error and found that it arises from function calling itself over and over, where does my code do this?
#include <iostream>
using namespace std;
//function prtototypes
void ReadShapeDimensions();
float calculateBasicVolume(double r ,double h);
void printResult();
int ReadInputShapeChoice(){
int choice;
float volume;
cout<<"Volume calculator\n\nPlease select a shape:\n(1)Cylinder\n(2)Cone\n";
cin>>choice;
ReadShapeDimensions();{
double r, h;
cout<<"Please enter the radius of the shape\n";
cin>>r;
cout<<"Please enter the height/length of the shape\n";
cin>>h;
calculateBasicVolume(r,h);
if (choice=1){
volume=3.14*r*r*h;
}
else if (choice=2){
volume=0.33*3.14*r*r*h;
}
printResult();{
cout<<"Volume = "<<volume<<"u^3";
}
}
int main();{
ReadInputShapeChoice();
return 0;
}
}
output:
1>------ Build started: Project: Workshop13_task2, Configuration: Debug Win32 ------
1> task2.cpp
1>c:\users\pin\documents\visual studio 2012\projects\workshop13_task2\workshop13_task2\task2.cpp(20): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\users\pin\documents\visual studio 2012\projects\workshop13_task2\workshop13_task2\task2.cpp(23): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\users\pin\documents\visual studio 2012\projects\workshop13_task2\workshop13_task2\task2.cpp(35): warning C4717: 'ReadInputShapeChoice' : recursive on all control paths, function will cause runtime stack overflow
1>task2.obj : error LNK2019: unresolved external symbol "void __cdecl ReadShapeDimensions(void)" (?ReadShapeDimensions@@YAXXZ) referenced in function "int __cdecl ReadInputShapeChoice(void)" (?ReadInputShapeChoice@@YAHXZ)
1>task2.obj : error LNK2019: unresolved external symbol "float __cdecl calculateBasicVolume(double,double)" (?calculateBasicVolume@@YAMNN@Z) referenced in function "int __cdecl ReadInputShapeChoice(void)" (?ReadInputShapeChoice@@YAHXZ)
1>task2.obj : error LNK2019: unresolved external symbol "void __cdecl printResult(void)" (?printResult@@YAXXZ) referenced in function "int __cdecl ReadInputShapeChoice(void)" (?ReadInputShapeChoice@@YAHXZ)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>c:\users\pin\documents\visual studio 2012\Projects\Workshop13_task2\Debug\Workshop13_task2.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
First of all, when you post code, pleas use code tags to make it easier for us to read your code. They show us as "<>" in the Format box on the right of the text input box.
Next, it appears you don't quite understand how to use functions. Maybe you should read this tutorial before continuing.
Make sure of the following:
- When defining your functions, don't put a ';' between the"()" and the"{"
- Make sure each function definition ends with a "}"
- If your function is defined with a return value, make sure there is a return statement that actually returns the value.
I don't see a specific recursion in your code, but it's because of the other errors.
Hi thanks for your reply, I've taken a look through the tutorials you pointed out thank you for that.
I've tried to edit the code as required, and no recursion errors, but I still get an expected ';' on line 15 after the '()' and cant seem to figure out why, I thought it was because I had declared r and h inside the function, so I declared them outside but that doesnt work.
seems like if you got rid of some unnecessary returns and brackets and functions.. it might work. try this code
oh and some variables were declared twice.. I edited this post
#include <iostream>
using namespace std;
float ReadInputShapeChoice(){
int choice;
float volume;
double r,h;
cout<<"Volume calculator\n\nPlease select a shape:\n(1)Cylinder\n(2)Cone\n";
cin>>choice;
cout<<"Please enter the radius of the shape\n";
cin>>r;
cout<<"Please enter the height/length of the shape\n";
cin>>h;
if (choice=1){volume=3.14*r*r*h;}
else if (choice=2){volume=0.33*3.14*r*r*h;}
cout<<"Volume = "<<volume<<"u^3";
float num_to_return;
num to return = volume;
return num_to_return; // I know this bit is redundant
}
int main();{
float num_returned; // really, all this stuff w num_to_return is...
num_returned = ReadInputShapeChoice(); // ...not necessary
return 0;
}
now that should work lol i've tweaked it a few times
sorry i should have mentioned, we were given the function prototypes so we have to include them, otherwise i would have written something similar to that, thanks very much for your help though.
num_x is assigned a value with cin >>
then its value is passed to "sample_function(int)"
(it's called "base" within the function and changes to
it do not affect the original variable "num_x".. it has
been "passed by value" not by reference.)
the function then returns its local variable "multiple"
and assigns its value to "num_y"