Hello guyz i am student and i use MPLAB with ccs compiler and a PIC18F4680.
I wrote a simple program that calculates the the (a+b+c)/3.I use hyper terminal to see and give the output and input data.But i only see the message "dwse 3 arithmous"then i give 3 numbers and then nothing.it doesnt go further down.Whats wrong??PLZ help!i dont have any build error.
char *input;
memset(input, NULL, sizeof(input));
puts("Enter x");
gets(input);
x = atoi(input);
memset(input, NULL, sizeof(input));
puts("Enter y");
gets(input);
y = atoi(input);
memset(input, NULL, sizeof(input));
puts("Enter z");
gets(input);
z = atoi(input);
OMG! You should never post code like this in a beginners forum, it's like giving them an armed grenade and telling them to store it away. gets is an EVIL function, every man page warns against using it.
His code was not "so bad". Here's my version:
char *buff;
memset(buff, NULL, sizeof buff); // ---> sizeof buff == 4b; where are they?
// bad news even in C! You can do this: char *buff = 0;
// insted for that matter....
puts("Enter x: ");
gets(buff); // really ugly whats gonna happen here...
The thing is that char *buff; up there points nowhere. No memory for that buffer when using gets(buff);where to store what???
I'd recommend:
1 2 3 4 5 6 7 8 9
char buff[64]; // 64 is arbitrary.... can be 100 or as big as u need it...
//Use memset safe if u like....
memset(buff, 0, sizeof buff); // --->sizeof buff == 64 b and buff now points to that area
//allocated in the stack....
gets(buff);// i'd recommend fgets(buff, sizeof buff, stdin); much more safe than gets()
//MS version gets_s(buff, sizeof buff): " _s" stands for secure.
I'm asumming that u want to read in floats not ints, in that case you use atof() not atoi().
1 2 3 4 5 6 7 8 9 10 11 12 13
char buff[64];
float num = 0F;
memset(buff, 0, sizeof buff); // sizeof(buff) with parenths when one argument is the same as
// sizeof buff; 0 == NULL;
printf("Enter x: ");
fgets(buff, sizeof buff, stdin);
num = atof(buff);// You can also do num = atof(gets(buff)); or with fgets() both return a (*ptr) to
// the buff.....
//Now u can do some computation on that number...
printf("The square of %.2f is %.2f.\n", num, num * num );
Hope this helps!
Now this u shouldnt do:
1 2 3
int a = atoi( getc() );
For various reasons, here one or two: atoi() expects a char const *(ptr) and getc() like fgetc() returns an int.
The ultimate recommendation would be unless it's absolutelt mandatory that u keep using that is that u get a good compiler MinGW developer studio is a good free one!