Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have problem with pointer..

so when i input:
2 3,
2 5,
2 7,
2 9,
2 11,
1 1
Output :
Nilai ; 3,
next = 4;

why testt.next is 4 ??
i want testt.next = 5;

What I have tried:

C++
<pre>#include <iostream>
 
using namespace std;
 
struct test
{
  int next;
  int previous;
  int nilai = 0;
};

int main()
{
	test *coba, testt;
	coba = &testt;
	int z;
	string perintah;
	int y = 0;
	while(true)
	{
		cout << "Input : ";
		cin >> perintah;
		if(perintah == "2")
		{
			if(testt.nilai != 0){
				*(++coba);
			}
			cin >> (*coba).nilai;
			y++;
		}
		if(perintah == "1")
		{
			cin >> z;
			for(int i = 0; i < y-z; i++)
			{
				*(--coba);
			}
			cout << "Nilai ; " << (*coba).nilai << endl;
			cout << "next / previous ? : ";
			cin >> perintah;
			if(perintah == "next")
			{
				*(++coba);
				testt.next = (*coba).nilai;
				cout << testt.next;
				for(int i = 1; i < y-z; i++)
				{
					*(++coba);
				}
			}
		}
	}
  return 0;
}
Posted
Updated 11-Jul-18 21:16pm
v3

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900