GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2004, 18:37
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Program that displays the minutes and hours


I usually dont like to ask for help, but I am stumped on this. I am also new to C programming. here is my code:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
    int mins, hrs, left, minutesHour;
	printf("Enter minutes (ctrl and z to stop): ");
	scanf("%d",&mins);
	
	

	while (scanf("%d", &mins) !=EOF)
	{
		minutesHour=60;
		hrs=mins/minutesHour;
		left=mins-(hrs*minutesHour);
	   
		printf("\nEnter minutes (ctrl and z to stop): ");
		scanf("%d",&mins);
		
	}
		printf("\n%d Hours and %d  minutes",hrs,left);

	getch ();

	return 0;

}

I need to have the Control and Z or F6 to stop the program. The program is like this because my teacher gave us partial code and we had to figure out the rest. Any suggestions would really be appreciated. Thanks....Tom
Last edited by dsmith : 06-Mar-2004 at 18:50. Reason: Please use [c] & [/c] to highlight c code
  #2  
Old 06-Mar-2004, 19:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Tommy. Welcome to GIDforums. I've got to be honest with you, I am lost in what you are trying to accomplish with your code. Maybe you could post what is supposed to happen and we could compare it to that. Your program is using 3 scanf statements, 2 in the same loop. Is this really what you want to do? Also, you are setting minutesHours to 60 every time through your loop. This isn't an error, but it is unnecesary. You should initialize this variable prior to entering the loop.

Ctrl-Z is easy as it is already set to stop a running program, but F6? What O/S are you using? Does f6 automatically end a program on your system or are you trying to capture this key press?

Sorry, I can't help you more until I understand the intent.
  #3  
Old 06-Mar-2004, 20:21
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Sorry. Lets try again:
CPP / C++ / C Code:
#include <stdio.h>
#include<conio.h>

int main()
{
int mins, hrs, left;
printf("enter minutes (0 to stop): ");
scanf("%d", &mins);
while (  )
{
      hrs=
     left=
     printf
     printf("Enter minutes (0 to stop):  ");
     scanf("%d", &mins);

}
getch();

return 0;

}
Now, I need this to run and when you punch in 0, the program stops. Then I need to write another program that stops when you type in F6 or ctrl and z. I need to submit both programs. I have the first one, but it's not working the way it's supposed too. I also used this for one of my scanf's:

while (scanf ("%f", &mins) !=EOF)----This is for the F6 or ctrl and z.

Thanks for trying to help. I tried every thing imaginable.
Last edited by dsmith : 06-Mar-2004 at 20:26. Reason: Enclose code in [c] & [/c] for highlighting.
  #4  
Old 06-Mar-2004, 20:23
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
F6 is the same thing as ctrl and Z..I am using Windows XP.. At least I thought ctrl and Z are the same thing as pressing F6. It does the same thing on my computer. Thanks again for helping and thanks for the welcome. I am going to work, but I am going to check my mail later. This needs to be done and I will try to work on it again at work. Have a great night!!
  #5  
Old 06-Mar-2004, 20:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay. I think I understand a little. It is still kind of a wierd format. ctrl-z will stop your program even if you don't hard code it. If you are in XP and F6 does the same, then there should be no need to trap for that either. Your initial format is kind of redundant, but using that format, you could just use:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

#define MINS_HOUR 60

int main()
{
	int mins, 
		hrs, 
		left;
  
  printf("Enter minutes (ctrl and z to stop): ");
  scanf("%d",&mins);
  
  

	while (mins){
		left = mins % MINS_HOUR;  //Do you know about the modulus command?
    	hrs=mins/MINS_HOUR;
     	printf("\n%d Hours and %d  minutes",hrs,left);
		
		printf("\nEnter minutes (ctrl and z to stop): ");
		scanf("%d",&mins);
    }
    

  getch ();   //I still don't understand this.

  return 0;

}

The reason I say the format is strange is because this could be done more efficiently with a do..while statement:
CPP / C++ / C Code:
#include <stdio.h>

#define MINS_HOUR 60

int main()
{
	int mins, 
		hrs, 
		left;

	do{
		printf("\nEnter minutes (ctrl and z to stop): ");
		scanf("%d",&mins);
		if(mins){
			left = mins % MINS_HOUR;
    		hrs=mins/MINS_HOUR;
     		printf("\n%d Hours and %d  minutes",hrs,left);
		}
    }while(mins);

  return 0;

}

Anyway, you should probably turn it in the way your teacher expects it to be. Both of these will end by entering 0 or by hitting ctrl-z. I don't know about F6, I am in Linux.
  #6  
Old 06-Mar-2004, 20:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Let's go back to your 1st code. I've added comments where I see problems
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
    int mins, hrs, left, minutesHour;
    printf("Enter minutes (ctrl and z to stop): ");
    scanf("%d",&mins);  
    // You have just asked for a value to be entered 

    // You are now going to ask for another value. Why?  
    // Get rid of the above scanf().  This one will suffice
    while (scanf("%d", &mins) !=EOF)
    {
        minutesHour=60;    // As dsmith said, move this above the while()
        hrs=mins/minutesHour;
        left=mins-(hrs*minutesHour);

        printf("\nEnter minutes (ctrl and z to stop): ");
        // The following scanf is also unnecessary, because the scanf in
        // the while loop will simply overwrite the value entered here.
        // Kill this one.
        scanf("%d",&mins);
		
    }
    printf("\n%d Hours and %d  minutes",hrs,left);

    getch ();
    return 0;
}

Now your program should work fine -- at least the inpu. You just had too many scanf()'s.

By the way, the sytax for specifying ctrl and z is CTRL-Z
  #7  
Old 07-Mar-2004, 06:57
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Talking

Hey guys,

Thanks soi much for the replys. Very quick and efficient. I will try this today at work. I work 2pm-11pm after working a 10pm-6:30am shift...How long have you been doing this? I hope to be reall good at this one day. There is a lot too learn. Any suggestions on how to learn, like an tips, tricks, study guides, etc... I am willing to learn, but it's finding other stuff out there. I have searched the internet and there are a ton of C programming out there, but I figured someone here could help me out and tell me there favorite sites. I have another book on C programming called "Teach Yourself C" and it's pretty good so far. Having a double major is tough and I am taking 16 credits and working full-time, hence the reason for asking help. The reaosn why I need this code like the way I posted it is because my teacher gave us this Lab assignment and we have to fix the code to make it do what it's supposed to do. So if I say: 90 minutes, it will say 1 hour and 30 minutes. I also saw the double scanf and I am still debating on whether to keep one there or to take 1 out. A good programmer creates code that's fast and efficient that also doesnt take up a lot of resources.

I will get back to you and let you know if I need any more help with this or any other problems I may have, which I am sure I will sooner or later.

Tom
  #8  
Old 07-Mar-2004, 07:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by tommy69
Any suggestions on how to learn, like an tips, tricks, study guides, etc...

I figure it is like anything else. I always tell myself when I go skiing or dirt-bike riding that if you aren't crashing you aren't learning. I've learned C mostly from "crashing" alot. I am not encouraging bad programming, but I would encourage you to push yourself and experiment with what you can do.
  #9  
Old 07-Mar-2004, 17:16
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
dsmith, you asked about getch() in the code above. getch() works sort of like system pause when you put it at the end of your program. It waits for input from the user, then the code continues. People often use this in console applications to prevent the program from exiting before the user can view the results.
  #10  
Old 08-Mar-2004, 17:57
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
dsmith, you asked about getch() in the code above. getch() works sort of like system pause when you put it at the end of your program. It waits for input from the user, then the code continues. People often use this in console applications to prevent the program from exiting before the user can view the results.
What getch() actually does is allow a keystroke to be entered then immediately continuing the program. It's a non-buffered version of getchar()

I still don't recommend using it except in rare cases (especially for pausing a program) because of it's nonportability. It's defined in M$, Borland compilers but few others (Dev too I think) -- mainly in Windows.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.