![]() |
|
#1
|
|||
|
|||
Program that displays the minutes and hoursI 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:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Sorry. Lets try again:
CPP / C++ / C Code:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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:
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:
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
|
||||
|
||||
|
Let's go back to your 1st code. I've added comments where I see problems
CPP / C++ / C Code:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
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 GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The