Need program that uses Pointers for Int, double & Char

Coding that I have so far works great, with the exception that when I have to increment the pointer int gives me like an error negative

Here is what I got so far:
-------------------------
#include <iostream>
using namespace std;

int main()
{
int *ip, a;
double *fp, b;
char *cptr, c;

cout << "Enter an integer: ";
cin >> a;

cout << "\nEnter a double: ";
cin >> b;

cout << "\nEnter a character: ";
cin >> c;

ip = new int;
*ip = a;
fp = new double;
*fp = b;
cptr = new char;
*cptr = c;

*(++ip);
cout << "\n\n\nThe new integer is: " << *ip << "\n";

cout << "The new double is: " << *fp+1 << "\n";

*cptr = *cptr+1;
cout << "The new character is: " << *cptr << "\n";



cout << "\n\n\n\n\n\n\t\t\t";
system ("pause");
return 0;
}

------------------------------
OUTPUT:
Enter an integer: 2
Enter a double: 6.2
Enter a character: z

The new integer is: -33686019 //this is what I see that's wrong with output
The new double is:7.2
The new character is::{
THis might be a little easier to read on the forum:

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
#include <iostream>
using namespace std;

int main()
{
int *ip, a;
double *fp, b;
char *cptr, c;

cout << "Enter an integer: ";
cin >> a;

cout << "\nEnter a double: ";
cin >> b;

cout << "\nEnter a character: ";
cin >> c;

ip = new int;
*ip = a;
fp = new double;
*fp = b;
cptr = new char;
*cptr = c;

*(++ip);
cout << "\n\n\nThe new integer is: " << *ip << "\n";

cout << "The new double is: " << *fp+1 << "\n";

*cptr = *cptr+1;
cout << "The new character is: " << *cptr << "\n";



cout << "\n\n\n\n\n\n\t\t\t";
system ("pause");
return 0;
}


1
2
3
4
5
6
7
8
OUTPUT:
Enter an integer: 2
Enter a double: 6.2
Enter a character: z

The new integer is: -33686019 //this is what I see that's wrong with output
The new double is:7.2
The new character is::{
on line 26 you are increasing the ponter, not the value pointed
*(++ip) means that ip will point at a different adress that u have not assigned. What u want to do is (*ip)++; which will increase the object ip points to by 1.
Last edited on
Topic archived. No new replies allowed.