Loop W/integer counter

This is what I have so far but how do I set the value of my integer to 2401 and make an integer counter that will count to 110 but I also have to have this in the body of the loop?
++(*ptr); cout<<*ptr<<" is stored at " <<ptr<<endl;


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
#include <iostream>

using namespace std;

int main() 
{
  int i = 0;
  int newint = 1, thatpoint = 2;
  int *ip = &newint;

  cout << "Value of *ip: " << *ip <<endl;
  cout << "Address of newint pointing : " << hex << ip << endl;
  ip = &thatpoint;
  cout << "After pointing to newint" <<endl;
  cout << "Value of *ip: " << *ip <<endl;
  cout << "Address of thatpoint pointing: " << hex << ip <<endl;
  cout << "You have now received two Pointer Addresses"<<endl;

  *ip = 2401;
	for(i = 0; i < 110; i++) {
        ++(*ptr); //The mandatory body of the loop
	cout<<*ptr<<" is stored at " <<ptr<<endl; //The mandatory body
    }
	return 0;
}


Last edited on
you set the int to 2401 on line 19. ip here changes thatpoint's value.

your loop counts from 0 to 109, 110 iterations. in that loop, i+1 is a counter from 1 to 110.

ptr does not exist. what is that supposed to be?

can you explain exactly what you want to do here?



My professor gave us the ptr body for the loop but didn`t tell us too intialize it so im going to assume it means pointer. I`m trying to write a loop with an integer counter that counts to 110. In his words he said "in the body of the loop does this: ++(*ptr); cout<<*ptr<<"is stored at " <<ptr<<endl;".
Last edited on
You set the value *ip to 2401 before you started the loop. If you want *ip to count to end at 110, you need to set it to 0.

also you didn't create *ptr, you can change it to *ip. Or, you can change *ip to *ptr. Or, you can create *ptr the same way you did *ip int *ptr {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
#include <iostream>

using namespace std;

int main()
{

  int newint{1};
  int thatpoint {2};
  int *ptr = &newint;

  cout << "Value of *ptr: " << *ptr <<endl;

  cout << "Address of newint is :" <<ptr << endl;
    cout<<endl;
  ptr = &thatpoint;
  cout << "After pointing to thatpoint" <<endl;
  cout << "Value of *ptr: " << *ptr <<endl;
  cout << "Address of thatpoint pointing: " << ptr <<endl;
  cout << "You have now received two Pointer Addresses"<<endl;

    *ptr =0;
	for(int i = 0; i < 110; i++) {
        ++(*ptr); //The mandatory body of the loop
	cout<<*ptr<<" is stored at " <<ptr<<endl; //The mandatory body
    }
	return 0;
}


also, when you set cout<<hex it will stay that way unless you change it back to cout <<dec.

You can copy and paste this below to see what I mean rand() is a random number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{

int num =rand();
int num2 = rand();

cout<<"num is: "<<num<<endl;
cout<<"num2 is: "<<num2<<endl;

cout<<"cout<< hex<< num :"<< hex<< num <<endl;

cout<<"cout is still in hex. Here is cout<<num2  :"<< num2 <<endl;

cout<<" Change cout back to dec. Here is cout<< dec << num2 : "<< dec << num2 <<endl;


}


Also, I removed hex because it works on my machine. I don't know about yours. If you need it just remember to switch bac to decimal "dec" when needed.
Last edited on
Topic archived. No new replies allowed.