Thread: C++ using function, nested decisions and sentinel loops question

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    C++ using function, nested decisions and sentinel loops question

    I thought I finished my homework/project and I'm assuming its a mess. It makes sense to me, but can someone help me out? I'm suppose to input A, X, V or Q (Area, cross section, area surface). Then a radius and height. The output is suppose to be two output arguments. a float representing thr radius and float for the height. Below is my coding. I'd really appreciate the help.

    Code:
    
    
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std; //intriduces namespace std
    
    
    const float PI = 3.14159f;
    void getdata (float &);
    float volume (float);
    float surface_area (float);
    float cross_section (float);
    float circum (float);
    float height (float);
    void putdata (float, float, float);//writes the radius plus two resuts
    
    
    int main (void) {
    	float radius, c, h, v, a, x;
    	getdata (radius);
    	c = circum (radius);
    	h = height (radius);
    	v = volume (radius);
    	a = surface_area (radius);
    	x = cross_section(radius);
    	putdata (radius, c, h);
    	system ("pause");
    	return 0;
    }
    
    
    void getdata (float & radius)
    {
    	cout<<cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n"<<endl;
    
    
    cin >> input;
    }
    float volume (float r & h)
    {return PI*h*r*r;
    }
    
    
    float surface_area (float r & c)
    {return (PI*r*r*2) + (c*h);
    }
    
    
    float cross_section (float r)
    {return PI*r*r;
    }
    
    
    void putdata (float radius, float c, float h, float v, float a, float x)
    
    
    {cout<<"Raduis ="<<radius<<endl;
    
    
    cout<<"Circumference ="<<c<<endl;
    
    
    cout<<"Volume ="<<v<<endl;
    
    
    cout<<"Surface Area ="<<a<<endl;
    
    
    cout<<"Cross Section ="<<x<<endl;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I didn't really get a question in all that.

    What "help" do you require?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. These function definitions
    Code:
    float volume (float r & h)
    float surface_area (float r & c)
    are invalid and don't match the prototypes:
    Code:
    float volume (float);
    float surface_area (float);
    2. You need to include an appropriate header file to call system function.
    Last edited by DRK; 05-06-2014 at 01:24 AM. Reason: Note about system()

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Line 32, two cout's

    getdata is not returning anything. Functions do not work like that. I think you would be interested in using a class.

    Code:
    float surface_area (float r & c)

    I think you are trying to pass by reference but only for one variable? You are also missing a comma....

    You really need to write a plan for each function before programming. You are going to save yourself a lot of time. Take the advice or learn the hard way, either way keeping coding

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jocdrew21 View Post
    getdata is not returning anything. Functions do not work like that. I think you would be interested in using a class.
    Nonsense. A function can return something or it can not. Nothing is wrong with either approach.
    Substituting with a class when you are just going to call one member function and which does not even contain any member data is pointless.

    Onwards to errors...
    Code:
    const float PI = 3.14159f;
    void getdata(float &);
    float volume(float);
    float surface_area(float);
    float cross_section(float);
    float circum(float);
    float height(float);
    void putdata(float, float, float);//writes the radius plus two resuts
    Don't remove the parameter names from the prototype declarations. This is considered bad practice.
    They also don't match the definitions. That's an error.

    Code:
    void getdata(float & radius)
    {
    	cout << cout << "Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n" << endl;
    	cin >> input;
    }
    Don't output twice to cout. It should only be once.
    radius parameter is unused and input is undeclared. You should either return the input value or read into the correct parameter (e.g. radius).

    Code:
    float volume(float r & h)
    {
    	return PI*h*r*r;
    }
    Syntax error in function definition. "float r & h" is not valid syntax. Did you mean to say that the function accepts two arguments? If so, you mean to write "float r, float h".

    Code:
    float surface_area(float r & c)
    {
    	return (PI*r*r * 2) + (c*h);
    }
    Same syntax error as before. h is undeclared. Did you forget to pass it as an argument?

    Code:
    void putdata(float radius, float c, float h, float v, float a, float x)
    Misleading name. Probably should be something along the lines of "printdata".

    Last, but not least, format and indent your code properly:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std; //intriduces namespace std
    
    const float PI = 3.14159f;
    void getdata(float &);
    float volume(float);
    float surface_area(float);
    float cross_section(float);
    float circum(float);
    float height(float);
    void putdata(float, float, float);//writes the radius plus two resuts
    
    int main(void)
    {
    	float radius, c, h, v, a, x;
    	getdata(radius);
    	c = circum(radius);
    	h = height(radius);
    	v = volume(radius);
    	a = surface_area(radius);
    	x = cross_section(radius);
    	putdata(radius, c, h);
    	system("pause");
    	return 0;
    }
    
    void getdata(float & radius)
    {
    	cout << cout << "Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n" << endl;
    	cin >> input;
    }
    
    float volume(float r & h)
    {
    	return PI*h*r*r;
    }
    
    float surface_area(float r & c)
    {
    	return (PI*r*r * 2) + (c*h);
    }
    
    float cross_section(float r)
    {
    	return PI*r*r;
    }
    
    void putdata(float radius, float c, float h, float v, float a, float x)
    {
    	cout << "Raduis =" << radius << endl;
    	cout << "Circumference =" << c << endl;
    	cout << "Volume =" << v << endl;
    	cout << "Surface Area =" << a << endl;
    	cout << "Cross Section =" << x << endl;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Functions do not work like that. I think you would be interested in using a class.
    O_o

    The reference parameter indicates that the function writes the result to the argument, but the implementation is still flawed.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @Elysia, I think you misunderstood me. I said he was not returning anything in getdata yet asking for input in the function. Therefore the function is not really doing anything.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The function has an in parameter, named radius. It could be used to get input back to the caller. But as it stands right now, the function isn't doing anything, no.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops question
    By shinobi_sol in forum C Programming
    Replies: 2
    Last Post: 10-26-2013, 05:32 PM
  2. A question from a newbie regarding nested function
    By Sharifhs in forum C Programming
    Replies: 19
    Last Post: 08-10-2010, 06:44 AM
  3. Quick question on nested for loops
    By GCNDoug in forum C++ Programming
    Replies: 12
    Last Post: 10-17-2007, 08:54 PM
  4. overloaded *operator, decisions, and loops
    By ronin in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2003, 10:22 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM