473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

program help

I have to use a do-while loop to force the user to enter a value within the listed range. Also, if the user does enter a value outside the range, use an "if" conditional to display a message indicating "Out of range".

The prompt, scanf, and the "if" conditional must all reside within the do-while.

Here is what I have so far:

[code]/* Seconds.c by Ryan 09/13/2006 */

/* This program will be able to calculate the number of seconds that
have elapsed on a given day or for a given event. */

#include <stdio.h>

int main()
{
/* Declaration */
long hours, minutes, seconds, total_seconds;

/* Input Section */
printf("Second Calculator \n\n\n\n");


do
{
printf("Hours (0-23) ");
scanf("%ld", &hours);
if (hours > (23)) {
printf(" Out of range");
} /* end if */
}
while (hours < 0 || hours > 23);



printf("\nMinutes (0-59) ");
scanf("%ld", &minutes);


printf("\nSeconds (0-59) ");
scanf("%ld", &seconds);

/* Processing Section */
total_seconds = hours * 3600 + minutes * 60 + seconds;

/* Output Section */
printf("\n\n\n\nTotal Seconds = %ld\n", total_seconds);

return 0;
}

The problem I'm having is once I type in a value higher than 23 for hours it ends up saying Out of rangeHours (0-23) instead of just Out of range.

Any help would be great. Thank you.
Oct 9 '06 #1
5 1632
manontheedge
175 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. do
  3. {
  4.    scanf ( "%ld", &hours );    
  5.  
  6.    if ( hours > ( 23 ) ) 
  7.    {
  8.         printf( " Out of range" );
  9.    }
  10.  
  11.    else
  12.    {
  13.         printf ( "Hours (0-23) " );
  14.    }
  15. }
  16.  
  17. while (hours < 0 || hours > 23);
  18.  

...try this. This way, it will only print the "Hours (0-23)" IF it's within range.
Oct 9 '06 #2
I tried that and it made me type in Hours (0-23) 50 and then it came up with a whole bunch of numbers and crashed, lol.
Oct 9 '06 #3
manontheedge
175 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. printf ( "Hours (0-23) " );
  3. scanf ( "%ld", &hours ); 
  4.  
  5.  
  6. if ( hours > 23 || hours < 0 )
  7. {
  8.      while ( hours > 23 || hours < 0 )     
  9.      {
  10.           printf( " Out of range\n" );      
  11.           printf ( "Hours (0-23) " );      
  12.           scanf ( "%ld", &hours );      
  13.      }
  14. }
  15.  
  16.  
...this works, i promise. It keeps looping IF you want to get a valid number of hours. If you just want to skip it if an invalid entry is entered...

Expand|Select|Wrap|Line Numbers
  1.  
  2. printf ( "Hours (0-23) " );
  3. scanf ( "%ld", &hours ); 
  4.  
  5.  
  6. if ( hours > 23 || hours < 0 )
  7. {
  8.    printf( " Out of range\n" );      
  9. }
  10.  
  11.  
sorry about that last post
Oct 9 '06 #4
manontheedge
175 100+
i just realised that you said you have to use a do-while...

is the program suppose to end if ANY value is outside of the range?
if so, then, put EVERYTHING inside the do while, and in the condition, you'll have 6 statements

if the program is suppose to continue, you'll need to have 3 do-while loops
Oct 9 '06 #5
Yes, it is supposed to have 3 do-while loops. I finally figured it out. Thank you for the help man.
Oct 9 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
2
by: stanlo | last post by:
Hallo to everyone, i am just begining to learn c++ even though i did pascal when i studied mathematics in the univesity.i just took on my self a project which writing a c++ program which does...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
1
by: Willing 2 Learn | last post by:
Below is a program I did to recognize a Finite State Automata for ASCII (J+H)*. I got that one working but im having trouble getting the NFA program to work. I really desperately need help! My...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
0
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.