Thread: Help - Collect data from Switch loop inside While loop

  1. #1
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8

    Help - Collect data from Switch loop inside While loop

    Hi all,

    First time using these board so my apologies if i don't post correctly! (i have read the posting rules so hopefully should be ok!

    The problem i've got is that I need to display the wages entered in the correct order at the end of the loop. For a reason i can't find it will only display the last data entered. If anyone could help me with where i've gone wrong that would be great thanks!

    Code:
    #include<stdio.h>#include<windows.h>
    #include<windowsx.h>
    
    
    /* Author James King, Wages1 Version 3.1 (Option A) for Caroline Cripps CORC1067*/
    
    
    int main()
    {
         /*variables and objects here*/
    
    
         char dept,exit,name[40];
         float totWage [40],wage,hours,overtime,com,wklyWage,avgGross;
         int count=0,index=0;
    
    
    
    
            system("cls");
    
    
         /*Executable instructions here*/
    
    
         printf("Welcome to the Wages1 program.");
    
    
         while (exit!='n'&&exit!='N')
         {
    
    
         printf("\n\nEnter Employee Code: ");
         scanf("%s",&dept);
    
    
    
    
             switch (dept)
             {                   /*START of FACTORY code*/
                 case 'F':
                 case 'f':
    
    
                      printf("\nEnter name: ");
                      scanf("%s",&name);
                      printf("Enter hours worked: ");
                      scanf("%f",&hours);
                      printf("Enter rate of pay: œ");
                      scanf("%f",&wage);
    
    
                      /*Displayed outputs*/
    
    
                      printf("\n-----Factory Dept-----");
                      printf("\nName: %s",name);
                      printf("\nEmployee Position: %d",count+1);
    
    
                              if (hours>35)
                               {
                                   overtime=(wage*1.5)*(hours-35);
                               printf("\nTotal Overtime: œ%0.2f",overtime);
                               }
    
    
                          totWage[index]=(35*wage)+overtime;
                          printf("\nWages earned for employee %d: œ%0.2f",count+1,totWage[index]);
                          count++;
    
    
    
    
                          break;
    
    
               /*end of Factory dept code*/
    
    
                   case 'S':      /*START of SALES code*/
                   case 's':
    
    
                        printf("\nEnter name: ");
                        scanf("%s",&name);
                        printf("Enter hours worked: ");
                        scanf("%f",&hours);
                        printf("Enter rate of pay: œ");
                        scanf("%f",&wage);
                        printf("Enter Commission Earned: œ");
                        scanf("%f",&com);
    
    
                        /*Displayed outputs*/
    
    
                        printf("\n-----Sales Dept-----");
                        printf("\nName: %s",name);
                        printf("\nEmployee Position: %d",count+1);
                        totWage[index]=(hours*wage)+com;
                        printf("\nWages earned for employee %d: œ%0.2f",count+1,totWage[index]);
                        count++;
    
    
    
    
                        break;
    
    
              /*end of Sales dept code*/
    
    
                      case 'A':         /*START of ADMIN code*/
                      case 'a':
                           printf("\nEnter name: ");
                           scanf("%s",&name);
                           printf("Enter hours worked: ");
                           scanf("%f",&hours);
                           printf("Enter rate of pay: œ");
                           scanf("%f",&wage);
    
    
                           /*Displayed outputs*/
    
    
                           printf("\n-----Administration Dept-----");
                           printf("\nName: %s",name);
                           printf("\nEmployee Position: %d",count+1);
                           totWage[index]=hours*wage;
                           printf("\nWages earned for employee %d: œ%0.2f",count+1,totWage[index]);
                           count++;
    
    
    
    
                           break;
    
    
              /*end of Administration dept code*/
    
    
              default:
                      printf("\n-----UNKNOWN ENTRY-----");
                      printf("\nPress 'Y' to continue");
    
    
    
    
            }
            /*end of SWITCH loop*/
    
    
            wklyWage=wklyWage+totWage[index];
            avgGross=(float)(wklyWage/count);
    
    
                printf("\n\n\nAre there more employee's? ");
    
    
                printf("\nEnter 'y' to continue or 'n' to finish.");
                printf("\n\n: ");
                scanf("%s",&exit);
            }
    
    
    
    
            /*end of WHILE loop*/
    
    
            printf("\n\nThe average pay is œ%0.2f",avgGross);
            printf("\nThe Wage bill for this week is œ%0.2f",wklyWage);
    
    
    /*THIS LOOP WONT DISPLAY ARRAY IN A LIST ONLY THE LAST DATA ENTERED THEN RANDOM DATA
    ---It should display the wages entered in order next to the number of the employee who entered it----*/
    
    
            for (index = 0; index < count; index++)
                         {
    
    
                         printf("\nEmployee no%d earned %0.2f",count+1, totWage[index]);
    
    
                         }
    
    
    
    
    
    
         getchar();
         getchar();
         return 0;
    
    
    }
    Thanks for looking! Only started using C a month ago, so there may well be a few problems. If there are I'm sorry!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    totWage[index]=(35*wage)+overtime;
    printf("\nWages earned for employee %d: œ%0.2f",count+1,totWage[index]);
    count++;

    You're using two different variables.
    index is never anything other than 0.

    Try totWage[count]

    > float totWage [40]
    Initialise them all to 0 with
    Code:
    float totWage [40] = { 0 };
    Other than that, it's a good first post.
    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.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    by the way did you notice 'exit' ? - you have to be careful when naming variables which 'might' be keywords
    Last edited by rogster001; 12-01-2012 at 06:09 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you input the names, you pass the parameter incorrectly.
    For example this code
    Code:
    printf("\nEnter name: ");
    scanf("%s",&name);
    should be

    Code:
    printf("\nEnter name: ");
    scanf("%s",name);
    because name is an array.

    As for what you are asking, you always assign the computed value to the first element.
    For example
    Code:
    totWage[index]=..
    index has the value zero from the initialization, but will never be incremented as it should be. What I mean, is that you assign something to the array, then you should increment the index by one, so that you won't overwrite the first element of the array again and again.

    Hope this helps.

    Also welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Brilliant thank you very much for the quick reply! Wasn't sure if i'd posted to much code as i know its quite long !

  6. #6
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Quote Originally Posted by Salem View Post
    totWage[index]=(35*wage)+overtime;
    printf("\nWages earned for employee %d: œ%0.2f",count+1,totWage[index]);
    count++;

    You're using two different variables.
    index is never anything other than 0.


    Try totWage[count]

    > float totWage [40]
    Initialise them all to 0 with
    Code:
    float totWage [40] = { 0 };
    Other than that, it's a good first post.
    I tried using count as the variable but i found i had no way of recalling whree it was saved because i'd have to initialize to 0 for the FOR loop at the end?

    again, newbie so please explain if my logic doesn't work (fairly likely)

  7. #7
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Quote Originally Posted by rogster001 View Post
    by the way did you notice 'exit' ? - you have to be careful when naming variables which 'might' be keywords
    Wasn't aware it could've been a keyword! thanks for the heads up ill change it!

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Quote Originally Posted by James King View Post
    Brilliant thank you very much for the quick reply! Wasn't sure if i'd posted to much code as i know its quite long !
    haha, good on you for taking in the recommendations for posting, that sort of length is i think ok for most people here to spend time looking at - we see a lot worse! ;->
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    system() is declared in <stdlib.h> which you didn't #include. Always #include the appropriate headers for the Standard library functions you use.
    Once you #include <stdlib.h>, "exit" is a identifier used for a function. You must not use "exit" as an identifier for your own purposes.

    Don't use objects (variables) of type float. Prefer objects of type double in almost all occasions (exceptions are when teacher insisted on float even after you tried to dissuade her; and when you want an array with some million elements).

    In line 29, you're using exit (you should rename this variable!) without having initialized it. It may begin life with a 'N' and your loop will never run. Initialize it (at line 13) to something reasonable.

    To read one single character with scanf() use the specifier "%c". The specifier you use at line 34 is invalid for char objects. You may also want to get rid of whitespace before the character: use a space before the conversion: scanf(" %c", &dept);

    To scanf an object of type double (after you replace your floats) use the specifier "%lf"

    Your immediate problem, I think
    I didn't see any change of the variable index inside your main loop. Isn't that supposed to index the array?

  10. #10
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Ok, all recommendations added. It now adds up the wages perfectly! thanks!, Except now my original totals and average calculations have gone insane... any idea's?

  11. #11
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Quote Originally Posted by qny View Post
    system() is declared in <stdlib.h> which you didn't #include. Always #include the appropriate headers for the Standard library functions you use.
    Once you #include <stdlib.h>, "exit" is a identifier used for a function. You must not use "exit" as an identifier for your own purposes.

    Don't use objects (variables) of type float. Prefer objects of type double in almost all occasions (exceptions are when teacher insisted on float even after you tried to dissuade her; and when you want an array with some million elements).

    In line 29, you're using exit (you should rename this variable!) without having initialized it. It may begin life with a 'N' and your loop will never run. Initialize it (at line 13) to something reasonable.

    To read one single character with scanf() use the specifier "%c". The specifier you use at line 34 is invalid for char objects. You may also want to get rid of whitespace before the character: use a space before the conversion: scanf(" %c", &dept);

    To scanf an object of type double (after you replace your floats) use the specifier "%lf"

    Your immediate problem, I think
    I didn't see any change of the variable index inside your main loop. Isn't that supposed to index the array?
    Thanks for the reply!

    i've changed variable 'exit' to just 'ex' now, How do i initialise a char? just initialise it to a random letter like this?

    Code:
     char dept,ex='a',name[40];
    ..?

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by James King View Post
    How do i initialise a char? just initialise it to a random letter like this?

    Code:
     char dept,ex='a',name[40];
    Yes, that is correct.

  13. #13
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Anyone got any idea's on why the avgGross and wklyWage are displaying a long string of numbers? (and 2 decimal places because of the 0.2f i presume?)

    I assume its because of the
    Code:
    wklyWage = wklyWage + totWage[index];
    calculation but i'm not sure whats wrong with it...
    Last edited by James King; 12-01-2012 at 06:57 AM. Reason: afterthought

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by James King View Post
    ... why the avgGross and wklyWage are displaying a long string of numbers?
    ... i'm not sure whats wrong with it...
    You need to initialize wklyWage (to 0).

  15. #15
    Registered User James King's Avatar
    Join Date
    Dec 2012
    Location
    Cornwall, UK
    Posts
    8
    Thank you! you are a life-saver!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a switch statement inside of a loop.
    By chumpp in forum C Programming
    Replies: 3
    Last Post: 06-30-2012, 11:01 PM
  2. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  3. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  4. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM