Pointers

I am trying to run a program that intends to access data through pointers.
But it not working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using namespace std;
int main()
{
    int testscores[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int* p = testscore;
    for (int i = 0; i < 10; i++)
        cout << (p++)<<" ";
    cout << endl;
    int var = 5;
    int* pointVar;
    pointVar = &var;
    cout << "var = " << var << endl;
    cout << "Address of var = " << (1)______________ << endl
        << endl;
    cout << "pointVar = " << (2)_________ << endl;
    cout << "Content of the address pointed to by pointVar  = " << (3)____________________ << endl;
    return 0;
}
First of all, I'm assuming you meant to say "testscores" on line 6?
Also, what exactly do
(1)______________ ,
(2)_________ ,
(3)____________________
mean? I've never seen expressions like that before (I'm by no means an expert, though). I'm guessing they're placeholders of some sort? Were you intending to put variables there?

Pointers:
https://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/articles/EN3hAqkS/

Read the articles, they will help you gain a better understanding of how to use pointers.

If my guess is correct, this is what you want:
I did my best to try and explain everything, but I'm sure someone else could probably explain it better.
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
#include <iostream>

int main(int argc, char const* argv[])
{
    int testscores[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
    // p is now a pointer that points to testscores
    // "testscores" is actually a pointer to a memory address
    // where the array is stored
    int* p = testscores;
    
    for (int i = 0; i < 10; i++)
    {
    	std::cout << (p++) << " ";
    }
    std::cout << std::endl;
    
    int var = 5;
    int* pointVar;   // pointVar is now a pointer
    pointVar = &var; // not pointVar points to the address of var
    
    std::cout << std::endl;
    
    // prints the value stored in var
    std::cout << "var = " << var << std::endl;
    
    // prints the address of var
    std::cout << "Address of var = " << pointVar << std::endl;
    
    // prints the value stored in pointVar
    std::cout << "pointVar = " << pointVar << std::endl;
    
    // prints the value of the variable at the address pointed to by pointVar
    std::cout << "Content of the address pointed to by pointVar = "
    		  << *pointVar << std::endl;
	
    return 0;
}


If you need any more help, feel free to ask! And read those articles, they helped me a lot when I was first starting out with pointers.

Best,
max
Last edited on
thanks but could you help me out with a few more
Am trying to make these code work perfectly but having a hard time with it
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
#include <iostream>

using namespace std;

 

class Car {

    string brand;

    string model;

    int year;   

    string getModel() {

        return model;

    }

    string getBrand() {

        return brand;

    }

    int getYear() {

        return year;

}

 

}

// Constructor definition outside the class

Car::Car(string x, int y, int x) {

    brand = x;

    model = y;

    year = z;

}

 

int main() {

    // Create Car objects and call the constructor with different values

    Car carObj1();

    Car carObj2("Ford", "Mustang");

 

    // Print values

    cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";

    cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";

    return 0;

}





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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>

using namespace std;

 

class Time {

    int hour;

    int minute;

    int second;

public:

    Time(int x=0, int y=0, int z=0)

    {

        hour = x;

        minute = y;

        second = z;

    }

    int getHour() {

        return hour;

    }

    int getMinute() {

        return minute;

    }

    int getSecond() {

        return second;

    }

    Time add(Time &t);

    void input()

    {

        cin >> hour >> minute >> second;

    }

    void output()

    {

        cout << hour << ":" << minute << ":" << second << endl;

    }

};

Time Time::add(Time& t) {

    int newhour, newminute, newsecond;

    newhour = hour + t.hour;

    newminute = minute + t.minute;

    newsecond = second + t.second;

    Time time(newhour,newminute,newsecond);

    return time;

}

 

int main() {

    Time myTime(10,20,30), newTime(15,45,40);

    cout << "From: ";

    myTime.output();

    cout << "After: ";

    newTime.output();

    cout << "Now is: ";

    Time now = myTime.add(newTime);

    now.output();

    return 0;

}



then this too
Define a class Student. It should provide member variables of student ID, student name, home town, and GPA.

These member variables should be inaccessible by the "." operator directly.

Provide public member functions, to set all these variables, and get the contents of these variables.

In the main function, let the user input the user's ID, name, hometown and GPA.

The print our all the input information in the required format:

For example, if you input: 00112233, Daniel Corye, Ottawa, and 78, the output should be

---------------

Daniel Corye from Ottawa with a student ID of 00112233 gets 78% in the study.

--------------------
Look, I'm not going to do your homework for you. I am willing to help you–up to a point, but I'm not going to just give you the answers. (Ok, I know I did for the first one, but I was feeling nice).

Because if I do that, then you're not going to actually learn how to use these things, you'll just copy and paste the answers, and use them without really trying to figure out how they work so you can use them for other things too.

Now, your compiler should be giving you a number of errors (at least 9) for the first program, so you can get started on correcting those. What compiler are you using, by the way?

A word of advice: In a class, all members (functions, variables, etc) are private, which means that they can only be accessed through member functions of that class (and friend functions, but we won't go into that). They can't be accessed by main unless they have been declared public.

Here's an example of a class with two private member variables and two public member functions.
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
class Example
{
    private:
        // these are private, they cannot be accessed outside of the class
        int var1;
        int var2;
    public:
        // these are public, they can be accessed from anywhere in the program
        Example ()
        {
            var1 = 15;
            var2 = 120;
        }
        void function1 ()
        {
            std::cout << "Var1: " << var1 << std::endl;
        }
        void function2 ()
        {
            std::cout << "Var2: " << var2 << std::endl;
        }
}; // note: you need to put a semicolon after the class declaration.

// sample driver program to run the class
int main (int argc, char const *argv[])
{
	// initialize object of the Example class
	Example object;
	
        // function calls
	object.function1 ();
	
	object.function2 ();
	
	return 0;
}


Try to work through it. If you have more questions, I will try to answer them.
Best,
max
Last edited on
Sorry if you thought I was giving you my homework
I already did it to a point were I go stuck
I wrote the codes thinking it was gonna work but when it didn't I became confused and got lost
Define a class Student. It should provide member variables of student ID, student name, home town, and GPA.


1
2
3
4
5
class student
{
  std::string id_, name_, home_town_;
  double gpa_; 
};


Provide public member functions, to set all these variables, and get the contents of these variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
class student
{
  std::string id_, name_, home_town_;
  double gpa_; 
public:
  std::string get_id() const { return id_; }; 
  void set_id(std::string_view id) { id_ = id; }

  double get_gpa() const { return name_; }
  void set_gpa(double gpa) { gpa_ = gpa; }

  // etc.
};
Last edited on
Oh, I see. Sorry about the rant, it looked like you were giving me your homework to do or something like that.

Well, here is what I was able to do with the first one. I am not sure of the requirements, but I put some comments inside the program so you can change it as much as you want.
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
#include <iostream>
using namespace std;
class Car
{
private:
	string brand;
	string model;
	int year;   
public:
	// Car constructor prototype, required
	Car (string x, string y, int z);
	
	string getModel () 
	{
        return model;
        }

        string getBrand () 
	{
        return brand;
        }

        int getYear () 
	{
        return year;
	}
};
// You still need a constructor prototype inside the class.
Car::Car (string x, string y, int z)
{
        brand = x;
        model = y;
        year = z;
}

int main (int argc, char const* argv[]) 
{	
	// You need to initialize both of these with three values,
	// because if you don't it won't work.
        Car carObj1 ("Chevrolet", "ZL1 Camaro", 1967);
        Car carObj2 ("Ford", "Mustang", 1965);

        // If you're going to access the member variables like this (below),
	// you'll need to make them public (see class definition above).
	// Or, just use a structure so everything is public by default.
	
        //cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
        //cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
	
	// If you absolutely need to use a class for this, 
	// you need to call the member functions (see below) to get the variables.
	
	string brand = carObj1.getBrand ();
	string model = carObj1.getModel ();
	int year = carObj1.getYear ();
	
	// Then you can simply print the variables like so:
	cout << "Brand:	" << brand << endl
	     << "Model:	" << model << endl
	     << "Year:	" << year << endl;
        return 0;
}

See if you can do the second one on your own.
Best,
max
Ok, for your second program there, I think you should know that while you can perform standard I/O (input/output) from a class member function, it is not considered good programming practice to do that. Well, input anyway. It is fairly common to use a function to output variables, however, it is still better to use main for that. But you should use main for getting user input, then pass it to the class functions.

Also, note the positioning of the opening brackets–it is easier to read if you do it this way:
1
2
3
4
5
6
7
8
9
10
// Like this:
int main ()
{
    /*code*/         
}

// Not this:
int main() {
/*code*/
}

Just a tip, if you're going to go into professional programming, you'll need to make your programs more easy to read.

For example:
1
2
#include<iostream>
using namepace std;int main(){cout<<"Hello world!"<<endl;return 0;}

That compiles and runs, but it is really hard to read. And it's just a short program, what if you had to read one that was 500+ lines long? It is much easier if the program is written neatly, like this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello world!" << endl;
    
    return 0;
}



That's all really, I can't see anything really wrong with it. It does what its supposed to. With the add function, and all that. So I'd say it's good to go as-is.

Best,
max
TO WHOM IT MAY CONCERN

I was sent a PM by the user @haibinz, and he informed me that @icezy is a student in his C++ programming course, and the questions that he posted are from their midterm exam! Just wanted to alert anyone who might be posting here.

max
Topic archived. No new replies allowed.