Thread: newb need help with increment and decrement

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Question newb need help with increment and decrement

    Hey guys I'm a Newb (TAKING MY 1ST C PROGRAMMING CLASS AT A UNIV.) I REALLY need help with this, it's printing and counting but it's counting too far, it's supposed to stop at 4:0:3 and then break and then count again. and then it doesn't include the "Do you want to have lunch" inside the loop, it just reverts back to the breakfast.... I've been at this thing for 3 DAYS and STILL no progress.. what am I doing wrong??? would really appreciate the help






    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define hrs 20
    #define sec 15
    #define min 12

    Code:
           
        int  main(void)   
       {
          int total_sec;
          int set_time;
          int start_timer;
          int quit;
          int h=0,m=0,s=0;
          int yes;
          int no;
          int choice;
          int i,j,k;
          int ch,cm,cs;
          int cnt=0;
        
        
        for(;;)
          {
             printf(" Choose one of the following menu options:\n");
             printf("1: set_time\n");
             printf("2: start_timer\n");
             printf("3: quit\n");
             scanf("%d", &choice);
          
             switch(choice)
             {
               
                case 1: printf("what is the current time?\n");
                   scanf("%d:%d:%d",&h,&m,&s); 
                 
                 
                  
                   break;
                
                      
                case 2: printf("how long would you like me to count?\n");
                   scanf("%d:%d:%d",&ch,&cm,&cs );
                   printf("HERE WE GO!\n");
                   printf("\n");
                   total_sec=(ch*hrs)+(cm*sec)+(cs);
                    //printf("\n%d\t%d\t%d\t%d\n",ch, cm, cs, total_sec);
                   for(i=0;i<total_sec;i++)
                   {
                         
                      
                      printf("%d:%d:%d\n",h,m,s); 
                   
                      s++;
                                             
                      if(s==sec)
                      {
                         s = 0;
                         m++;                 
                      
                         if(m==min)
                         {
                            m=0;
                         
                            h++;
                         }
                       
                      }
                   case 3: printf("Have you had your breakfast yet?\n");
                       printf("1:  yes\n");
                           printf("0:  no\n");
                           scanf("%d", &choice);
                      
                   if(choice==1);
                   {  
                   printf("Good\n");
                   
                   }      
                 else printf("Done Counting!\n");
                  
                  break;
                  
                 
                 case 4: printf("Have you had your lunch yet?\n");
                     printf("1: yes\n");
                     printf("0: no\n");
                     scanf("%d", &choice);
                     if(choice==0);
                     {
                     printf("You have %d%d%d till the end of lunch time!\n");
                     }
                     else printf("Good!\n");
                    
                    break;
                    
                 case 5: printf("Thank You!\n");
         
                   }       
                      
                   break;
         
                 
                  printf("Sorry, that is not a valid menu choice.\n");
                               
          }       
          
          return 0;
       }



    I'm not asking for anybody to do my work for me, just tell me what I need to fix and some suggestions...Here's what the criteria is..




    How long would you like me to count?

    0:1:0 //what the user puts in//

    HERE WE GO!
    3:11:13
    3:11:14
    4:0:0
    4:0:1
    4:0:2
    4:0:3

    Have you had your breakfast?
    1: Yes
    0: No

    1

    Good!

    4:0:4
    4:0:5
    4:0:6
    4:0:7
    4:0:8
    4:0:9
    4:0:10
    4:0:11
    4:0:12

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    1

    What is the current time?
    7:10:0

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    2

    How long would you like me to count?

    0:1:5

    HERE WE GO!
    7:10:0
    7:10:1
    7:10:2
    7:10:3
    7:10:4
    7:10:5
    7:10:6
    7:10:7
    7:10:8
    7:10:9
    7:10:10
    7:10:11
    7:10:12
    7:10:13
    7:10:14
    7:11:0
    7:11:1
    7:11:2

    Have you had your lunch?
    1: Yes
    0: No

    0

    You have 1:0:13 till the end of lunch time.

    7:11:3
    7:11:4

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    1

    What is the current time?
    19:11:10

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    2

    How long would you like me to count?

    0:0:8

    HERE WE GO!
    19:11:10
    19:11:11
    19:11:12
    19:11:13
    19:11:14
    0:0:0
    0:0:1
    0:0:2

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    4

    Sorry, that is not a valid menu choice.

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    3

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("3: quit\n");
    > case 3: printf("Have you had your breakfast yet?\n");
    There's one mistake
    Why is this even a separate case - surely you just want to run into breakfast anyway.

    > if(choice==1);
    > if(choice==0);
    The ; at the end nullifies the test.
    The code which follows is an independent statement.

    > case 5: printf("Thank You!\n");
    See the brace which follows this line?
    What is it supposed to be matched with?

    A truly vital skill is to be able to indent code properly (or at least consistently), since this will instantly tell you where your braces are supposed to go. Your code for example seemed to be missing some in a few key places.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define hrs 20
    #define sec 15
    #define min 12
    
    int main(void)
    {
      int total_sec;
      int set_time;
      int start_timer;
      int quit;
      int h = 0, m = 0, s = 0;
      int yes;
      int no;
      int choice;
      int i, j, k;
      int ch, cm, cs;
      int cnt = 0;
    
      for (;;) {
        printf(" Choose one of the following menu options:\n");
        printf("1: set_time\n");
        printf("2: start_timer\n");
        printf("3: quit\n");
        scanf("%d", &choice);
    
        switch (choice) {
        case 1:
          printf("what is the current time?\n");
          scanf("%d:%d:%d", &h, &m, &s);
          break;
    
        case 2:
          printf("how long would you like me to count?\n");
          scanf("%d:%d:%d", &ch, &cm, &cs);
          printf("HERE WE GO!\n");
          printf("\n");
          total_sec = (ch * hrs) + (cm * sec) + (cs);
          /*printf("\n%d\t%d\t%d\t%d\n",ch, cm, cs, total_sec); */
          for (i = 0; i < total_sec; i++) {
            printf("%d:%d:%d\n", h, m, s);
            s++;
            if (s == sec) {
              s = 0;
              m++;
              if (m == min) {
                m = 0;
                h++;
              }
            }
          }                         /*!! added to match for total_sec */
        case 3:
          printf("Have you had your breakfast yet?\n");
          printf("1:  yes\n");
          printf("0:  no\n");
          scanf("%d", &choice);
    
          if (choice == 1) {        /*!! removed ; */
            printf("Good\n");
    
          } else
            printf("Done Counting!\n");
          break;
        case 4:
          printf("Have you had your lunch yet?\n");
          printf("1: yes\n");
          printf("0: no\n");
          scanf("%d", &choice);
          if (choice == 0) {        /*!! removed ; */
            printf("You have %d%d%d till the end of lunch time!\n");
          } else
            printf("Good!\n");
    
          break;
    
        case 5:
          printf("Thank You!\n");
          /*!! } commented out */
          break;
    
        default:                    /*!! added */
          printf("Sorry, that is not a valid menu choice.\n");
          break;                    /*!! added */
        }
      }                             /*!! added */
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    I have this same assignment but took a different approach which I posted in a new thread. But the main difference I see is you used a switch. Mine doesn't do your problem, but my counting is messed up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  2. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM