Thread: Help Please!!!!!

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    8

    Help Please!!!!!

    I have this bit of code but im not sure what is wrong with it.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    
    {
    int salesperson=0,int count[50],int quarter=1;
    float quarter1_sales=0;
    float quarter2_sales=0;
    float quarter3_sales=0;
    float quarter4_sales=0;
    float sales=0;
    float salesperson_total=0;
    
    for(count=1;count<=50;count++)
       {
        printf("Please enter the salesperson's Id number:");
        scanf("%d",&salesperson[count]);
    
        for(quarter=1;quarter<=4;quarter++)
    
           {
            printf("Please enter the quarter in which the sales took place:");
            scanf("%d",&quarter);
    
            while( quarter!=1 && quarter!=2 && quarter!=3 && quarter!=4 )
            {
            printf("Please enter a quarter number between 1 and 4");
            scanf("%d",&quarter);
            }
               printf("Please enter the sales amount for that quarter");
               scanf("%f",&sales);
    
             if(quarter==1){
                quarter1_sales+=sales;
                scanf("%f",&quarter1_sales);
                }
    
             else if(quarter==2){
                quarter2_sales+=sales;
                scanf("%f",&quarter2_sales);
                }
             else if(quarter==3){
                quarter3_sales+=sales;
                scanf("%f",&quarter3_sales);
                }
             else(quarter==4){
                quarter4_sales+=sales;
                scanf("%f",quarter4_sales);
                }
    
    
           salesperson_total+=quarter1_sales+=quarter2_sales+=quarter3_sales+=quarter4_sales;
           scanf("%f",&salesperson_total);
    
           }
    
    printf("The quarter 1 total is = %f",&quarter1_sales);
    printf("The quarter 2 total is = %f",&quarter2_sales);
    printf("The quarter 3 total is = %f",&quarter3_sales);
    printf("The quarter 4 total is = %f",&quarter4_sales);
    printf("The salesperson total is = %f",&salesperson_total);
    
    }
       return 0;
    
    }
    Could anyone advise me?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What exactly are the errors the compiler is giving?

    But here is what I found:
    1) You assign count as an array but use it as a normal integer.
    2) You assign salesperson as an integer but try to use it as an array.
    3) You assign quarter the value of 1 but have the while loop exit when quarter is == to 1.
    4) Indentation needs work
    5) The final else statement had a condition but no if.
    Code:
    else(quarter==4){
    should be
    Code:
    else{
    Here is the code as I corrected it. It compiles fine but I haven't tried to run it.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void)
    {
      int salesperson[50], count, quarter=0;
      float quarter1_sales=0;
      float quarter2_sales=0;
      float quarter3_sales=0;
      float quarter4_sales=0;
      float sales=0;
      float salesperson_total=0;
    
      for(count=1;count<=50;count++)
      {
        printf("Please enter the salesperson's Id number:");
        scanf("%d",&salesperson[count]);
    
        for(quarter=1;quarter<=4;quarter++)
        {
          printf("Please enter the quarter in which the sales took place:");
          scanf("%d",&quarter);
    
          while( quarter < 1 && quarter > 4 )
          {
            printf("Please enter a quarter number between 1 and 4");
            scanf("%d",&quarter);
          }
    
          printf("Please enter the sales amount for that quarter");
          scanf("%f",&sales);
    
          if(quarter==1){
            quarter1_sales+=sales;
            scanf("%f",&quarter1_sales);
            }
          else if(quarter==2){
            quarter2_sales+=sales;
            scanf("%f",&quarter2_sales);
            }
          else if(quarter==3){
            quarter3_sales+=sales;
            scanf("%f",&quarter3_sales);
            }
          else{
            quarter4_sales+=sales;
            scanf("%f",quarter4_sales);
            }
         salesperson_total+=quarter1_sales+=quarter2_sales+=quarter3_sales+=quarter4_sales;
         scanf("%f",&salesperson_total);
       }
    
       printf("The quarter 1 total is = %f\n",&quarter1_sales);
       printf("The quarter 2 total is = %f\n",&quarter2_sales);
       printf("The quarter 3 total is = %f\n",&quarter3_sales);
       printf("The quarter 4 total is = %f\n",&quarter4_sales);
       printf("The salesperson total is = %f\n",&salesperson_total);
      }
      return 0;
    }
    Last edited by Thantos; 01-17-2004 at 11:22 AM.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    8
    After it has prompted for the quarter sales it doesnt do anything else. It's just blank screen.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    put a getchar(); before the return 0;

    edit: Forgot you are using scanf(). insert the following before the return 0;
    Code:
    while ( getchar() != '\n');
    getchar();
    Last edited by Thantos; 01-17-2004 at 11:53 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Interesting how you've managed to regress from the previous answer I gave.

    quarter1_sales+=sales;
    scanf("%f",&quarter1_sales);
    So what's the point of the scanf() here?
    All you're doing is carefully destroying the results of your calculations by reading something in from the user.

    I think it will work better without those extra scanf calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed