Hellooo guyz i need a little guidance with this prog

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.

THIS IS THE CODE.....................

[#include <18F4680.h>
#include <stdio.h>
#include <MATH.h>
#fuses HS
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

float mo (float a , float b , float c)
{
float ar;
ar=(a+b+c)/3;
return ar;
}


void main ()
{
do {
float x,z,y,mesos;
puts("dwse 3 arithmous");
x=getc();
y=getc();
z=getc();
mesos = mo(x,y,z);
printf("Mesos oros:%f",mesos);
return;
}while(TRUE);
}]

........................................... THANK YOU GUYZ
Last edited on
Please put the code between [code] [/code] tags :)
Oops. Never mind what was here.

-Albatross
Last edited on
closed account (z05DSL3A)
Albatross,

The ccs compiler is a C compiler.

Oops... my bad...

In that case, I recommend using scanf().
http://cplusplus.com/reference/clibrary/cstdio/scanf/

-Albatross
Last edited on
i cannot use scanf() brother.it does not recognize it even if i include stdio.lib!
stdio.lib? You mean stdio.h, right?

-Albatross
yeap
i even wrote a simple program to add 2 numbers but it was adding asci characters

sth is wrong with the type of the variables.i have them char.
But if change them to int the program wont do nothing
!

whats wrong?
First of all:

1
2
3
float ar;
ar=(a+b+c)/3;
return ar;


Can be reduced to:

return (a+b+c)/3;

Since you can't get scanf to work, and I don't think you want to write a little parser, you can just do it the easy way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);


Sorry if the last part is bad code, you get the concept..
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:
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
#include <18F4680.h>
#include <stdio.h>
#include <MATH.h>
#fuses HS
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

float mo (float a , float b , float c)
{
	return (a+b+c)/3;
}


void main ()
{
	do {
		float x,z,y,mesos;
		puts("dwse 3 arithmous");
		x=atoi(getc());
		y=atoi(getc());
		z=atoi(getc());
		mesos = mo(x,y,z);
		printf("Mesos oros:%f",mesos);
		return;
	}while(TRUE);
}


You must notice tho, that this code does not check for EOF or ERROR on getc() return values and thus can be further optimized.

Btw what language is that?
Guyz thx so much.i ll try it and i tell you as soon as i get a result ;-)))))))))
So I guess this is C forum...!!

Lets see:

1
2
3
4
5
6
7
8
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!
Topic archived. No new replies allowed.