code error

Discussion in 'C Programming' started by yanyo, Oct 9, 2005.

  1. yanyo

    yanyo Guest

    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;
    }

    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 - 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 - 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 - 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 - average) , 2 );
    }
    sd = pow(v/oTotal,.5);
    }
    printf("Other: $%10.2f +/-%10.2f\n", average, sd);



    }
     
    yanyo, Oct 9, 2005
    #1
    1. Advertising

  2. yanyo

    Mike Wahler Guest

    "yanyo" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 9, 2005
    #2
    1. Advertising

  3. yanyo

    Ian Guest

    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
     
    Ian, Oct 9, 2005
    #3
  4. > void showStaistics(void)
    > { float v;
    > int allTotal = 0;
    > int i=0;
    > for(i=0; i<5; i++){
    > allTotal += totals;
    > }
    >
    > 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 - 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 - 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 - 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 - 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?
     
    grocery_stocker, Oct 9, 2005
    #4
  5. "yanyo" <> 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) <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.
     
    Keith Thompson, Oct 10, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wes Stebbins
    Replies:
    1
    Views:
    5,841
  2. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    652
    Alphonse Giambrone
    Jun 28, 2003
  3. Yan
    Replies:
    0
    Views:
    1,173
  4. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,376
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  5. keithb
    Replies:
    1
    Views:
    960
    Bruce Barker
    Mar 29, 2006
Loading...

Share This Page