C++ structure homework's questions

Hi everyone,
Thanks for your time to read my post. I am having problem with my program and until now it is the set up function. I tried to pass the parameter in but the complier said that too many argument so that i know there is something wrong.
Can everyone help me out.
Thanks again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  //
//  main.cpp
//  Phuong_assignment_A
//
//  Created by Phuong Pham on 9/29/16.
//  Copyright © 2016 Phuong Pham. All rights reserved.
//

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
const double PI = 3.14159265358979323846;
struct Cone
{
    double height, radius;
    
};
void input();
void setUp();
double getVolume();
void outPut();

int main(int argc, const char * argv[])
{
    double height=0, radius=0;
    Cone *ptr = new Cone();
    
    input();
    setUp(height,radius);
    outPut();
    delete ptr;

    return 0;
}
void input ()//double &height, double &radius)

{
    
    Cone getinfo_height, getinfo_radius;
    cout << "Input your height"<< endl;
    cin >> getinfo_height.height;
    cout << "Input your radius"<< endl;
    cin >> getinfo_radius.radius;

}

void setUp(double &height,double &radius)
{
    
    Cone *ptr;
    height=ptr->height;
    radius=ptr->radius;
}
double getVolume()
{
    Cone *ptr;
    double Volume;
    Volume = (ptr->height * pow(ptr->radius, 2)*PI)/3.0;
    cout << "Volume is: "<<Volume;
    return Volume;
}

void outPut()
{
    Cone *ptr;
    getVolume();
    cout <<"Height: "<<ptr->height<<endl<<"Radius: "<<ptr->radius<<endl<<"Volume: "<<getVolume()<<endl;
}


Code fixed, hope it will be useful for people who later study this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
//  main.cpp
//  Phuong_assignment_A
//
//  Created by Phuong Pham on 9/29/16.
//  Copyright © 2016 Phuong Pham. All rights reserved.
//

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
const double PI = 3.14159265358979323846;
struct Cone
{
    double height, radius;

};
void input (double &height, double &radius);
void setUp( Cone *ptr,double &height,double &radius);
double getVolume(Cone *ptr);
void outPut(Cone *ptr);

int main(int argc, const char * argv[])
{
    double height=0, radius=0;
    Cone *ptr = new Cone();
    input(height,radius);
    setUp(ptr,height,radius);
    outPut(ptr);
    delete (ptr);
    return 0;
}
void input (double &height, double &radius)

{
    cout << "Input your height"<< endl;
    cin >> height;
    cout << "Input your radius"<< endl;
    cin >> radius;
}

void setUp( Cone *ptr,double &height,double &radius)
{
    ptr->height=height;
    ptr->radius=radius;
}
double getVolume(Cone *ptr)
{

    double a=ptr->height;
    double b=ptr->radius;
    double Volume;
    Volume = (PI * pow(b, 2) * a)/3.0;
    return Volume;
}

void outPut(Cone *ptr)
{
    getVolume(ptr);
    cout <</*"Height: "<<height<<endl<<"Radius: "<<radius<<endl<<"Volume: "<<*/getVolume(ptr)<<endl;
}
Last edited on
Line 20 is different from line 48.
Hi, i fixed my set up function.
now the complier run into my function getVolume(), which has problem.
1
2
3
4
5
6
7
8
9
10
double getVolume()
{
    Cone *ptr;
    double a=ptr -> height;
    double b=ptr -> radius;
    double Volume;
    Volume = (PI * pow(b, 2) * a)/3.0;
    cout << "Volume is: "<<Volume;
    return Volume;
}

i debugged it and it seems to that the variable height and radius has no value in there even though i used pointer to get data.
Please help.
Line 3: What does ptr point to? Hint: It's an uninitialized variable and its value is garbage.

Lines 4-5: You can;t dereference a pointer that doesn't point to anything.
Line 3: in my understanding that the ptr is pointing to the height and radius in Cone Struct.
Line 4,5: i just simply think that i am trying to pass the value of height and radius to my new variable a and b.
Can you explain for me more detail about this pointer how to grab the data from struct or pass data to struct by pointer.
1
2
3
4
5
6
7
8
9
10
11
void input (double &height, double &radius)

{
    
    Cone *ptr;
    cout << "Input your height"<< endl;
    cin >> ptr->height;
    cout << "Input your radius"<< endl;
    cin >> ptr->radius;

}
i think the problem is from here. There are garbage in my height and radius when i tried to pass the value in by pointer.
Same problem. line 5: ptr is an uninitialized variable. It doesn't point to anything.
http://www.cplusplus.com/doc/tutorial/pointers/

Going back to your original post:
Line 27 creates a new instance of Cone and assigns the value to ptr. However, ptr is a local variable within main. Lines 51,57,66 are different instances of ptr. They are NOT the same as ptr in main().

Line 40: getinfo_height, getinfo_radius are local variables. They go out of scope (are destroyed) when input() exits.
Thank you so much.
I did fixed my program successfully.
Topic archived. No new replies allowed.