19 The following program has careless errors on several line
19. The following program has careless errors on several lines. Find and correct the errors and show the output where requested.
#include <iostream>
Int main()
{
int* ptr;
int* temp;
int x;
ptr = new int;
*ptr = 4;
*temp = *ptr;
cout << ptr << temp;
x = 9;
*temp = x;
cout << *ptr << *temp;
ptr = new int;
ptr = 5;
cout << *ptr << *temp; // output: ____________________
return 0;
}
Solution
//Program
#include <iostream>
using namespace std; //include the namespace to include iostream header
int main()
{
int* ptr;
int* temp;
int x;
ptr = new int;
*ptr = 4;
*temp = *ptr;
cout << ptr <<\"\\t\" <<temp<<endl;
x = 9;
*temp = x;
cout << *ptr <<\"\\t\"<< *temp<<endl;
ptr = new int;
*ptr = 5; // Change pointer to *ptr instead of ptr because integer cant convert into int* assignment
cout << *ptr <<\"\\t\"<< *temp; // output: 5 9
return 0;
}
Output:
0x3a66100 0
4 9
5 9
