hi all plzz help

i am just starting coding in vc++ 2010 express
i am tring for a simple scanf program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* scanf example */
#include <stdio.h>
#include "conio.h"
int main ()
{
  int a;
  int i;
  int ans;
  printf ("1 NUM ");
  scanf ("%d",a);  
  printf ("2nd");
  scanf ("%d",&i);
  ans=a*i;
  printf("* is %d",ans);
 return 0;
_getch();
}

it gives message success=1
but after some time when i play
Run-Time Check Failure #3 - The variable 'a' is being used without being initialized.
and when i say to continue it continues and
Unhandled exception at 0x5ab1e30e (msvcr100d.dll) in scanf.exe: 0xC0000005: Access violation writing location 0xcccccccc.

i want to ask what wrong i am doing and when this Unhandled exception comes?
That's because you passed an integer instead of a pointer to an integer in the first scanf call.
And you just demonstrated why you should not use scanf/printf/etc. in C++. They're not type-safe.
Last edited on
i dont understand can u make my code right to understand
please
This is a variant with the error fixed:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
  cout << "First number: ";
  int first,second;
  cin >> first;
  cout << "Second number: ";
  cin >> second;
  cout << "Product: " << first*second << endl;
}
i tried code it works but if i want to stop to see answer which command should i use
as i was using _getch() in my code?
You can try a double cin.get();. If it works for you - good.
However doing this in a more portable way is a nightmare.

See also: http://www.cplusplus.com/forum/beginner/1988/
Last edited on
hey thanks Athar i tried many programs using that all worked perfectly
i tried with 3 num other signs and much.....................
THANKS TOOOOOOOOOO MUCH

i tried this program but build failed so what i have to do to find square root
program of square success but not square root can u help me
i tired
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include"conio.h"
using namespace std;
void main()
{
	int num,squareroot;
	cout << "enter number:";
        cin >> num;
	squareroot=num/squareroot;
	cout << "squareroot is:" << squareroot << end1;
	_getch();
}
Last edited on
You have an error in line 4, (must be int main()) and another one in line 10 (must be endl, not end1).
Another problem is that you're using squareroot uninitialized in line 9. That's not even close to how you calculate a square root anyway. Just use sqrt: http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
Last edited on
thanks
is there any other library in c++ for cuberoot or 4th root etc.
/* scanf example */
#include <stdio.h>
#include "conio.h"
int main ()
{
int a;
int i;
int ans;
printf ("1 NUM ");
scanf ("%d",a);-------------------> i think error on this line... scanf("%d", &a) might be the right
printf ("2nd");
scanf ("%d",&i);
ans=a*i;
printf("* is %d",ans);
return 0;
_getch();
}
i tried code it works but if i want to stop to see answer which command should i use
as i was using _getch() in my code?


pressing ctrl + f5 replaces _getch() in your code
/* scanf example */
#include <stdio.h>
#include "conio.h"
int main ()
{
int a;
int i;
int ans;
printf ("1 NUM ");
scanf ("%d",a);-------------------> i think error on this line... scanf("%d", &a) might be the right
printf ("2nd");
scanf ("%d",&i);
ans=a*i;
printf("* is %d",ans);
_getch();
return 0;

}
use code like this it will stop
Last edited on
Topic archived. No new replies allowed.