Problems with pointers

Hello, i'm new here and also at programming in general, i been selfstudying C++ for about 2 hours now this language since i found it very easy to understand, learning here from examples but when i tried to make my first program making a program the Sum of 2 numbers using pointers and mixing a few stuff that i learned everything went fine except after i ask for the second number it gets stuck and can't continue the code in order to show the result.

If someone can give me a hint or help me trying to find the issue i'll greatly appreciate.

Also i can't understand the differences between scanf and cin except that scanf seems to me more complex and hard to use.

This is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
     int x, y, *p, *q, sum;
     p = &x, q = &y;
     string target[2] = {"first","second"};
     cout<<"Enter two integers to add \n\n\n\n;
     for(int x = 0; x<=2;x++)
     {
         cout<<"Enter"<<target[x]<<" value to add \n";
         if (x ==1 )
         {
             scanf("%d",&x);
         }
        scanf("%d",&y);
     }
     sum = *p + *q;
     cout<<"Sum of entered numbers = "<<sum;
    return 0;
} 


I just wanted to use for in order to understand how it works array of strings changin first and second when i ask for each number to sum. I had to use scanf in order to decrease a little bit my code.

After i ask for the second number it seems the code never reach sum = *p + *q; for any reason that i can't understand.

Thank you
Last edited on
All of <cstdio> or <stdio.h> was included mostly for backwards compatibility. Feel free to use std::cin. ;)

A few things. The first is that the bounds on your loop is off by one, as arrays go from to their_size-1. When you think about what your code does, hopefully you'll figure out exactly what I meant. ;)

The second is that the body if your loop isn't complete. Are you missing an else somewhere?

I think that's all. *sleepy yawn*

-Albatross
Last edited on
Oh, thank you for helping me, after to read your comments i double checked how arrays does work and i found the problem with the array that you hinted me. And i saw that i missed the else and i got that fixed.

This is how it looks my code now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
     int x, y, *p, *q, sum, k;
     string target[2] = {"first","second"};
     for(k = 0; k<=1;k++)
     {
         cout<<"Enter "<<target[k]<<" value to add \n";
         if (k == 0 ){scanf("%d",&x);}
         else scanf("%d",&y);
     }
     p = &x, q = &y;
     sum = *p + *q;
     cout<<"Sum of entered numbers = "<<sum;
    return 0;
}


This is how i left my code and it work correctly thanks to you Albatros.

Thank you very much problem solved. Now i can continue learning this language.
Last edited on
Topic archived. No new replies allowed.