469,579 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

code error

hi, im trying to figure out whats the problem with this program i get a
runtime error but i dont see where the problem is i tried changing
declaration but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.
#include<stdio.h>
#include<math.h>

char menu_action(void);
int menu_faith(void);
void showStaistics(void);
int bdonations[100];
int cdonations[100];
int jdonations[100];
int mdonations[100];
int odonations[100];
int totals[5];

int bTotal = 0;
int cTotal = 0;
int jTotal = 0;
int mTotal = 0;
int oTotal = 0;

int main(void)
{
char option,faith;
//array to keep track of donations of each religion

int lastDonation;
int religion;
while(1)
{
//show main menu
option = menu_action();
if( option == 'Q' || option == 'q'){
showStaistics();
return 0;
}

if( option == 'D' || option == 'd')
{
lastDonation = getUserDonation();
//get the religion
religion = menu_faith();
if( religion == 0 )
{
if( bTotal < 100 ){
bdonations[bTotal++] = lastDonation;

totals[0] += lastDonation;
}

}
else
if( religion == 1 )
{
if( cTotal < 100 ){
cdonations[cTotal++] = lastDonation;

totals[1] += lastDonation;
}
}
else
if( religion == 2 )
{
if( jTotal < 100 ){
jdonations[jTotal++] = lastDonation;

totals[2] += lastDonation;
}
}
else
if( religion == 3 )
{
if( mTotal < 100 ){
mdonations[mTotal++] = lastDonation;

totals[3] += lastDonation;
}
}
else
if( religion == 4 )
{
if( oTotal < 100 ){
odonations[oTotal++] = lastDonation;

totals[4] += lastDonation;
}
}
}
else
if( option == 'S' || option == 's')
{
showStaistics();
}

}

}
char menu_action(void)
{
char option;
while(1)
{
printf("\n Main Menu\n");
printf("\n ---------\n");
printf("D: New donation\n");
printf("S: Show statistics so far\n");
printf("Q: Show statistics and quit\n");
printf("\nChoose one of the following menu options :");

fflush(stdin);
scanf("%c",&option);

if( option != 'D' && option != 'd' && option != 'S' &&
option != 's' && option != 'Q' && option != 'q' )
{
printf("\nPlease enter a valid input\n");
}
else
{
break;
}
}
return option;
}

int getUserDonation(void)
{
int donation;
printf("\nPlease enter your donation: ");
fflush(stdin);
scanf("%d",&donation);
return donation;
}
int menu_faith(void)
{
char option;
while(1)
{
printf("\nPlease enter one of the following faiths:\n");
printf("B: Buddhist\n");
printf("C: Christian\n");
printf("J: Jewish\n");
printf("M: Muslim\n");
printf("Z: any other\n");
fflush(stdin);
scanf("%c",&option);

if( option == 'B' || option == 'b' )
return 0;
if( option == 'C' || option == 'c' )
return 1;
if( option == 'J' || option == 'j' )
return 2;
if( option == 'M' || option == 'm' )
return 3;
if( option == 'Z' || option == 'z' )
return 4;
else
{
printf("\nPlease enter a valid input\n");
}
}
return option;
}

void showStaistics(void)
{ float v;
int allTotal = 0;
int i=0;
for(i=0; i<5; i++){
allTotal += totals[i];
}

printf("\n\nTotals :\n");
printf("--------\n");

printf("All: $%5d\n", allTotal);
printf("Budhist: $%5d\n", totals[0]);
printf("Christian: $%5d\n", totals[1]);
printf("Jew: $%5d\n", totals[2]);
printf("Muslim: $%5d\n", totals[3]);
printf("Other: $%5d\n", totals[4]);
//averages and std deviations
//All
//christian
if(cTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[1]/cTotal;
v = 0;
for(i=0; i<cTotal; i++){
v += pow( (cdonations[i] - average) , 2 );
}
sd = pow(v/cTotal,.5);
}
printf("Christian: $%10.2f +/-%10.2f\n", average, sd);

//Jew
if(jTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[2]/jTotal;
v = 0;
for(i=0; i<jTotal; i++){
v += pow( (jdonations[i] - average) , 2 );
}
sd = pow(v/jTotal,.5);
}
printf("Jew: $%10.2f +/-%10.2f\n", average, sd);

//Muslim
if(mTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[3]/mTotal;
v = 0;
for(i=0; i<mTotal; i++){
v += pow( (mdonations[i] - average) , 2 );
}
sd = pow(v/mTotal,.5);
}
printf("Muslim: $%10.2f +/-%10.2f\n", average, sd);

//budhist
if(oTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[4]/oTotal;
v = 0;
for(i=0; i<oTotal; i++){
v += pow( (odonations[i] - average) , 2 );
}
sd = pow(v/oTotal,.5);
}
printf("Other: $%10.2f +/-%10.2f\n", average, sd);

}
Nov 15 '05 #1
4 1836

"yanyo" <x_******@hotmail.com> wrote in message
news:90******************************@localhost.ta lkaboutprogramming.com...
hi, im trying to figure out whats the problem with this program i get a
runtime error
I doubt that. It won't even compile for me:

--------------------Configuration: FileSplit - Win32
Debug--------------------
Compiling...
test.c
test.c(40) : warning C4013: 'getUserDonation' undefined; assuming extern
returning int
test.c(24) : warning C4101: 'faith' : unreferenced local variable
test.c(193) : error C2065: 'average' : undeclared identifier
test.c(194) : error C2065: 'sd' : undeclared identifier
test.c(201) : warning C4244: '+=' : conversion from 'double ' to 'float ',
possible loss of data
test.c(203) : warning C4244: '=' : conversion from 'double ' to 'int ',
possible loss of data
test.c(217) : warning C4244: '+=' : conversion from 'double ' to 'float ',
possible loss of data
test.c(219) : warning C4244: '=' : conversion from 'double ' to 'int ',
possible loss of data
test.c(233) : warning C4244: '+=' : conversion from 'double ' to 'float ',
possible loss of data
test.c(235) : warning C4244: '=' : conversion from 'double ' to 'int ',
possible loss of data
test.c(249) : warning C4244: '+=' : conversion from 'double ' to 'float ',
possible loss of data
test.c(251) : warning C4244: '=' : conversion from 'double ' to 'int ',
possible loss of data
Error executing cl.exe.

c.obj - 2 error(s), 10 warning(s)

but i dont see where the problem is i tried changing
declaration
Don't guess. Test. Use a debugger. Try starting smaller
and only continue to add more code when what you have works correctly.
but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.
It's a source code problem. First fix those errors. (And
you should deal with those warnings too (most can be fixed
by changing your type 'float' objects, to type 'double',
which you should be doing anyway.)

More comments below.
#include<stdio.h>
#include<math.h>

char menu_action(void);
int menu_faith(void);
void showStaistics(void);

int bdonations[100];
int cdonations[100];
int jdonations[100];
int mdonations[100];
int odonations[100];
int totals[5];

int bTotal = 0;
int cTotal = 0;
int jTotal = 0;
int mTotal = 0;
int oTotal = 0;

int main(void)
{
char option,faith;
//array to keep track of donations of each religion
There's no array here.

int lastDonation;
int religion;
while(1)
{
//show main menu
option = menu_action();
if( option == 'Q' || option == 'q'){
showStaistics();
return 0;
}

if( option == 'D' || option == 'd')
{
lastDonation = getUserDonation();
//get the religion
religion = menu_faith();
if( religion == 0 )
{
if( bTotal < 100 ){
bdonations[bTotal++] = lastDonation;

totals[0] += lastDonation;
}

}
else
if( religion == 1 )
{
if( cTotal < 100 ){
cdonations[cTotal++] = lastDonation;

totals[1] += lastDonation;
}
}
else
if( religion == 2 )
{
if( jTotal < 100 ){
jdonations[jTotal++] = lastDonation;

totals[2] += lastDonation;
}
}
else
if( religion == 3 )
{
if( mTotal < 100 ){
mdonations[mTotal++] = lastDonation;

totals[3] += lastDonation;
}
}
else
if( religion == 4 )
{
if( oTotal < 100 ){
odonations[oTotal++] = lastDonation;

totals[4] += lastDonation;
}
Read about the 'switch' statement. It's probably more
appropriate here than all those 'if/else's
}
}
else
if( option == 'S' || option == 's')
{
showStaistics();
}

}

}
char menu_action(void)
{
char option;
while(1)
{
printf("\n Main Menu\n");
printf("\n ---------\n");
printf("D: New donation\n");
printf("S: Show statistics so far\n");
printf("Q: Show statistics and quit\n");
printf("\nChoose one of the following menu options :");

fflush(stdin);


'fflush' has no defined behavior for an input stream. Its
behavior is only defined for output or 'update' streams.
I'll stop here. Seriously, I think your main problem is
that you're trying to do too much at once. Start *much*
smaller. Only add more code when what you have works
properly.

-Mike
Nov 15 '05 #2
Ian
yanyo wrote:
hi, im trying to figure out whats the problem with this program i get a
runtime error but i dont see where the problem is i tried changing
declaration but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.

Don't you have a debugger?

Ian
Nov 15 '05 #3
> void showStaistics(void)
{ float v;
int allTotal = 0;
int i=0;
for(i=0; i<5; i++){
allTotal += totals[i];
}

printf("\n\nTotals :\n");
printf("--------\n");

printf("All: $%5d\n", allTotal);
printf("Budhist: $%5d\n", totals[0]);
printf("Christian: $%5d\n", totals[1]);
printf("Jew: $%5d\n", totals[2]);
printf("Muslim: $%5d\n", totals[3]);
printf("Other: $%5d\n", totals[4]);
//averages and std deviations
//All
//christian
if(cTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[1]/cTotal;
v = 0;
for(i=0; i<cTotal; i++){
v += pow( (cdonations[i] - average) , 2 );
You have 'v' as float, but power returns type double. I think you might
have some funky conversion rules going on.
}
sd = pow(v/cTotal,.5);
}
printf("Christian: $%10.2f +/-%10.2f\n", average, sd);

//Jew
if(jTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[2]/jTotal;
v = 0;
for(i=0; i<jTotal; i++){
v += pow( (jdonations[i] - average) , 2 );
}
sd = pow(v/jTotal,.5);
}
printf("Jew: $%10.2f +/-%10.2f\n", average, sd);

//Muslim
if(mTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[3]/mTotal;
v = 0;
for(i=0; i<mTotal; i++){
v += pow( (mdonations[i] - average) , 2 );
}
sd = pow(v/mTotal,.5);
}
printf("Muslim: $%10.2f +/-%10.2f\n", average, sd);

//budhist
if(oTotal == 0 ){
average = 0;
sd = 0;
}
else{

average = totals[4]/oTotal;
v = 0;
for(i=0; i<oTotal; i++){
v += pow( (odonations[i] - average) , 2 );
}
sd = pow(v/oTotal,.5);
}
printf("Other: $%10.2f +/-%10.2f\n", average, sd);

}


Ohhh my hips. I think bloating. Look at me from the side. Do I look fat?

Nov 15 '05 #4
"yanyo" <x_******@hotmail.com> writes:
hi, im trying to figure out whats the problem with this program i get a
runtime error but i dont see where the problem is i tried changing
declaration but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.

[snip]

You say you're getting a runtime error. The code you posted doesn't
compile ("average" and "sd" are undeclared). Therefore what you
posted isn't the code that gets the runtime error.

If you post code here, *always* post the *exact* code that you
compiled. Anything else is a waste of time.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Thelma Lubkin | last post: by
17 posts views Thread by wana | last post: by
18 posts views Thread by __frank__ | last post: by
9 posts views Thread by Jens Jensen | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.