very basic: scanf()

Dear Forum,
it is very basic calculation of adding two numbers. when I enter 1 2 I get the result

Numbers you entered is 1, 2, 78

I have no idea where from is 78, as well as why the adding is not performed. May I pleas for some help?

#include <iostream>
#include <conio.h>

int main()
{
int num1, num2, num3;
num3=num1 + num2;

printf("Enter two numbers separated by space \n");
scanf("%d %d", &num1, &num2);

printf("Numbers you get is %d, %d, %d \n", num1, num2, num3);

getch();
return 0;
}
You're adding undefined variables. Add them up after you put in the input. And I would use iostream over C input and output.

Also in the future please put your in code blocks.
printf and scanf doesn't work in iostream ,
instead of that type this :<stdio.h>

Thanks for replays.
I changed the sequence of commands and it is working well.

"And I would use iostream over C input and output"
I don't get what that is mean?

In Dev C++ it is works with <iostream.h>



Hi,

Also, one should make use of the return value from scanf so one can see if it worked. If you don't, it is dangerous as some of the values may contain garbage.

"And I would use iostream over C input and output"
I don't get what that is mean?


They mean use std::cin , std::getline, std::cout and others to do the same thing as scanf and printf

Hope all is well :+)
Topic archived. No new replies allowed.