Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. conversion error

conversion error

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonygeeker
    wrote on last edited by
    #1

    I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?

    J L 2 Replies Last reply
    0
    • A Anonygeeker

      I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      It is your while loop that checks t1 which gets the number of hours assigned in the loop. With input 3663, t1 is set to 63 within the first loop iteration which is then processed and shown as "1:1". Add the missing scanf() call at the end of the loop to read the next user input and remove the unnecessary lines:

      scanf("%d", &t1);
      while (t1 > 0){
      printf("%d\n", t1);
      t = t1 % 60;
      t1 = t1 / 60;
      printf("%d : %d\n", t1, t);
      scanf("%d", &t1);
      }

      1 Reply Last reply
      0
      • A Anonygeeker

        I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Indent your code properly, and use meaningful names for your variables and the problem becomes clearer:

        // no need for a while loop as you only have a single value to convert
        int minutes = t1 % 60; // remainder = 3
        int hours = t1 / 60; // 3663 / 60 = 61

        You could add a third calculation to convert the hours to days and hours.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups