0

I want the user to assign a value for int temp1 and int temp2. However, the compiler is saying I need to initialise one of the two variables (temp2).

Why is it only asking me to initialise temp2 and not temp1? When I do assign a value to temp2 the program then ignores whatever value the user enters.

Is my code sloppy and if so, is there a way I can fix this?

(I've included the whole program in case it is relevant however the error i'm receiving is in the inputDetails() function.)

#include <iostream>
using namespace std;

//Prototype

void inputDetails(int* n1, int* n2);
void outputDetails(int num1, int num2, int* pNum, int* n1, int* n2, int** ppNum);

//Functions

int main()
{
int num1;
//num1 pointer
int* n1 = &num1;

int num2;
//num2 pointer
int* n2 = &num2;

//get pNum to point at num1
int* pNum;
pNum = new int;
*pNum = num1;

//pointer to pNum
int** ppNum = &pNum;

//call functions
inputDetails(n1, n2);
outputDetails(num1, num2, pNum, n1, n2, ppNum);

//change pNum to point at num2
delete pNum;
pNum = new int;
*pNum = num2;

//call function again
outputDetails(num1, num2, pNum, n1, n2, ppNum);
delete pNum;

system("PAUSE");
return 0;
}

void inputDetails(int* n1, int* n2)
{
int temp1, temp2;
cout << "Input two numbers" << endl;
cin >> temp1, temp2;
*n1 = temp1;
*n2 = temp2;
}

void outputDetails(int num1, int num2, int* pNum, int* n1, int* n2, int** ppNum)
{
cout << "The value of num1 is: " << num1 << endl;
cout << "The address of num1 is: " << n1 << endl;
cout << "The value of num2 is: " << num2 << endl;
cout << "The address of num2 is: " << n2 << endl;
cout << "The value of pNum is: " << pNum << endl;
cout << "The dereferenced value of pNum is: " << *pNum << endl;
cout << "The address of pNum is: " << ppNum << endl;
}
Adi K
  • 21
  • 5
  • Listen to your compiler. Your compiler is very smart: `*pNum = num1;`. What's `num1` initialized to, at this point? Nothing. Hence, the compiler is telling you that. This may do no harm, still, there's no point to this assignment, whatsoever, so simply get rid of it. – Sam Varshavchik Jan 04 '17 at 03:38
  • `*pNum = num1;` means to copy the existing value of `num1` into the `new int` you just allocated. (Which is undefined behaviour since you did not initialize `num1`). It does not mean "get pNum to point at num1" – M.M Jan 04 '17 at 03:40
  • new and delete are generally only needed for arrays and structs. For single integers and pointers a declaration is sufficient; new without a corresponding delete will cause a memory leak **beware**. – Arif Burhan Jan 04 '17 at 03:53
  • @M.M Thanks for pointing out this flaw, it seems I don't fully understand how to use pointers correctly. What I wanted to do was create a pointer which I could use to look at num1 and then reassign it to look at num2 later in the program. – Adi K Jan 04 '17 at 03:54
  • @AliAhmad the code for that would be `int *pNum = &num1;` You do not need to allocate any other `int` in order to look at an int that already exists. – M.M Jan 04 '17 at 03:55

1 Answers1

4

Why is it only asking me to initialise temp2 and not temp1?

The following doesn't do what you think it does (it inadvertently uses the comma operator):

cin >> temp1, temp2;

To read two values from cin, use:

cin >> temp1 >> temp2;
Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012