Author |
Message |
Vertico
|
Posted: Sun Sep 16, 2007 7:57 pm Post subject: Count error. Basic starting program. |
|
|
Schools in and I could use some help. I have never touched C before so forgive me if I have done something stupidly wrong.
Basically its a math error that I cant seem to figure out. I know something is wrong with the count variable. It jumps up to 50 for some reason I am unaware of. Can someone help me out here, I am pretty new to C so I assume I just declared something wrong perhaps? Or perhaps its just the compiler? I am useing VS2005
/*Displays a-z on the left hand side, and a running average of the letters to the right. When the left
hand letter reaches B it will display the average as a number instead of a letter.*/
#include<stdio.h>
#include<conio.h>
int main (void)
{
char letter = 'a';
char average = 'a';
char count = '2';
while(letter<='z')
{
if(letter=='b')
{
printf("%c %d\n",letter++, average);
}
else
{
printf("%c %c\n",letter++, average);
}
//Calculates the running average by adding the last average and current letter, then dividing all by count
average= (average + letter)/count;
count+=1;
}//ends while
printf("Press any key to continue%a\n");
_getch();
return 0;
}
The final output should look like this:
a a
b 97
c b
d b
e c
f c
g d
etc...
instead i get:
a a
b 3
c
d
e
f
etc... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Saad
|
Posted: Sun Sep 16, 2007 8:05 pm Post subject: RE:Count error. Basic starting program. |
|
|
The problem is that using a char to represent 2.
When you assign '2' to char it stores the ascii value of '2' which is 50. Use int to represent count |
|
|
|
|
|
Vertico
|
Posted: Sun Sep 16, 2007 8:20 pm Post subject: Re: Count error. Basic starting program. |
|
|
Ok, so that does explain the 50, but after changing count to an int, I still get absurd numbers in the count variable. |
|
|
|
|
|
Tony
|
Posted: Sun Sep 16, 2007 8:28 pm Post subject: RE:Count error. Basic starting program. |
|
|
does your new line of code look _exactly_ like
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Saad
|
Posted: Sun Sep 16, 2007 8:29 pm Post subject: RE:Count error. Basic starting program. |
|
|
You should learn the basic data types C uses.
int - Used to represent an intger number
eg int count = 5;
float - Used to represent floating point numbers
eg float foo = 1.2f;
double - Used to represent big floating point number double PI = 3.14159265
char - Used to represent a character
eg char letter = 'a';
The problem you have is you're using char to store the average, you should be using float. |
|
|
|
|
|
md
|
Posted: Mon Sep 17, 2007 9:14 am Post subject: RE:Count error. Basic starting program. |
|
|
You're also doing your calculations after your output; if you move it so you update the average and count before you output anything it may work closer to how you want it to. |
|
|
|
|
|
Vertico
|
Posted: Tue Sep 25, 2007 2:50 pm Post subject: Re: Count error. Basic starting program. |
|
|
I got the code working, thank you for your assistance. After that first error with the char, everything was fine except the VB2005 I was using on my laptop had some screws loose. Gave me some crazy outputs like I mentioned above, but the same compiler on the computer in class worked perfectly. |
|
|
|
|
|
|