473,721 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prompts the user to enter another value, until value

64 New Member
Hello I have a small question. I'm trying to loop a question and scan the number that the user input. The user input must be valid such as that it has to between 1000-3000 range. I tried a small code,but just came to my mind that it would ask twice. I want it to stop if the input is valid and if not ask the question again. Anyone can provide me some tips or help. Thanks.

Example

Expand|Select|Wrap|Line Numbers
  1.     while (!(year>3000) && !(year>1000))
  2.         {
  3.         printf("Please enter a year between 1000 to 3000\n");
  4.         scanf("%d\n",&year);
  5.         }
  6.  
Result

Please enter a year between 1000 to 3000
3000
2000
Feb 28 '07 #1
4 8805
Ganon11
3,652 Recognized Expert Specialist
Hello I have a small question. I'm trying to loop a question and scan the number that the user input. The user input must be valid such as that it has to between 1000-3000 range. I tried a small code,but just came to my mind that it would ask twice. I want it to stop if the input is valid and if not ask the question again. Anyone can provide me some tips or help. Thanks.

Example

Expand|Select|Wrap|Line Numbers
  1.     while (!(year>3000) && !(year>1000))
  2.         {
  3.         printf("Please enter a year between 1000 to 3000\n");
  4.         scanf("%d\n",&year);
  5.         }
  6.  
Result

Please enter a year between 1000 to 3000
3000
2000
Think about your loop condition. You want to keep getting numbers while year is greater than 3000 or less than 1000. So the loop should execute until the number is between these two - in other words, while year is greater than 1000 AND less than 3000.

Currently, I think you have the loop executing while the user enters a valid number and stopping at an invalid value.
Feb 28 '07 #2
td0g03
64 New Member
Think about your loop condition. You want to keep getting numbers while year is greater than 3000 or less than 1000. So the loop should execute until the number is between these two - in other words, while year is greater than 1000 AND less than 3000.

Currently, I think you have the loop executing while the user enters a valid number and stopping at an invalid value.
Thanks for the information. I changed the code and understand why, but it still asks to type two numbers if it is is valid or not valid.
Feb 28 '07 #3
td0g03
64 New Member
Well here is a little update. I been messing around the code.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  
  4. int year;
  5. int whatYear;
  6.  
  7. int main(void)
  8. {
  9.     printf("Please enter a year between 1000 to 3000 first\n");
  10.     scanf("%d\n",&year);
  11.     if(!(year >=1000) || !(year <=3000))
  12.     {
  13.          while(year <1000 || year >3000)
  14.          {
  15.                printf("Please enter a year between 1000 to 3000 second\n");
  16.                scanf("%d\n",&year); 
  17.          }
  18.     }
  19.     else
  20.     year = whatYear;
  21.     printf("herere %d",whatYear);
  22. }
  23.  
  24.  
The result is half right, but it still asks for two input if it valid or not.

Please enter a year between 1000 to 3000 first
1500
1500
herere 0

Second test

Please enter a year between 1000 to 3000 first
1
1
Please enter a year between 1000 to 3000 second
1
Please enter a year between 1000 to 3000 second
1
Please enter a year between 1000 to 3000 second
1
Please enter a year between 1000 to 3000 second
1
Please enter a year between 1000 to 3000 second
1500
Please enter a year between 1000 to 3000 second
1500
herere 0
Feb 28 '07 #4
td0g03
64 New Member
Just another update! I got the program working yay! Thanks for the help
Feb 28 '07 #5

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

Similar topics

3
1770
by: Jason Heyes | last post by:
This is a revised version of a post entitled "Class to support keywords". Please reply to this post instead of the old one. The following program repeatedly prompts the user for C++ keywords until 'explicit' is entered. If the user fails to enter a valid keyword, the program terminates. #include <iostream> #include "KeyWord.h"
10
3356
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does anybody know how I could do this?
5
2059
by: V. Jenks | last post by:
Long story as short as possible: I have a page (aspx) that contains a user control (ascx). In the form the user can enter a username and password. If they do, the password box auto-posts-back and checks to see if the user already exists in the database. If yes, the rest of the form fields on the page are populated with that existing user's data.
1
6397
by: Bill Nguyen | last post by:
Report source is an SQLSERVER 2K store procedure. VB.NET application. Report created by CR 8.5. At runtime, I still had to click "CANCEL" to bypass the parameter prompts before the report display correctly. Also, print option grayed out although it was available at design time. Thanks a million! Bill
5
1151
by: alf | last post by:
Hi, I have a command line program which also does some interaction with the user using stdin and stdout. My requirement is to print prompt so the user can answer in the same line. Unfortunately: print 'enter command:',
5
2577
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD Inventory application. It works, does what I want it to and all that, but now I need to put an array in there to store more than one cd at a time. Seems simple enough until I actually start coding. I want to save as much of the code as I can since I worked...
3
3651
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return statement).If either of the values is negative,the progra prints"Negative input" and terminates.Otherwie, it prints the values that the user enters. After that, the program compares the first integer and the second ineger and prints either "The first integer...
2
2719
by: beemomo | last post by:
Hi everyone. I used the BeforeUpdate event procedure to display a confirmation prompt and handle a user's response to either cancel or continue with the save as described in MSDN: Private Sub Form_BeforeUpdate(Cancel As Integer) Dim strMsg As String Dim iResponse As Integer strMsg = "Do you wish to save the changes?" & Chr(10) strMsg = strMsg & "Click Yes to Save or No to Discard changes." iResponse = MsgBox(strMsg,...
1
3650
by: Constantine AI | last post by:
Hi i am trying to get User input if data does not exist within a DLOOKUP table. I have gotten it to work for one record but not multiple, i have tried to incorporate my code into a loop procedure but i have failed. Could anyone give me any advice? Dim db As Database Dim rst As Recordset Dim rst2 As Recordset Dim strSQL As String Dim Widthval As Integer Dim Depthval As Integer Dim Heightval As Integer Dim...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.