|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
New to programming(just a few weeks into it) and 1st forum ever joined so bare with me while I go thru the learning curve on how forums work.
Assignment is as follows: Write a program that will ask a user to enter the current Tempeature (-50 to 50,decimal), windspeed(3 to 150, whole number) and ask whether thier input was in Fahrenheit or Celcius(f or c). It should then echo the temperature and wind speed entered in BOTH Fahrenheit and Celcius (to 1 decimal place) and output the wind chill in BOTH Fahrenheit and Celcius. (to 1 decimal place). I wrote it using an example the instructor showed in class as follows: (only part of code) Question: How do I validate only a number is entered? Code:
do// loop to test valid windspeed entered.
{
cout << "Enter Wind speed (3 to 150):";
cin >> windspeed;
if ((windspeed < 3) || (windspeed > 150))
{
cout << "Please re enter a windspeed"
<< " between 3 and 150:";
cin >> windspeed;
}
else
wsFlag = true;
}while (wsFlag != true);// end windspeed validation loop.
I thought I saw a previous post relating to this but cannot find it again!! Thank you all Last edited by WingedPanther; 04-24-2008 at 07:56 PM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||||
|
Generally, accepting your input as a string and then determining if it represents a number is safer than requiring the input to be a number.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
Maybe.... there is one way that I know of that works pretty well... but if they make a mistake, Backspace won't help them:
Try the C function getch(): Code:
// Be sure to include conio.h
// This function returns a string
// of only numbers, validated
// as typed.
string EnterOnlyNumbers() {
string numString = "";
char ch = getch();
while (ch != '\r') { // \r is the enter key
if (ch >= '0' && ch <= '9') {
cout << ch;
numString += ch;
}
ch = getch();
}
return numString;
}
However, my compiler gives me warnings on the getch() function, probably because it is indeed depreciated. If you want the best code, getline() as a string then validate it. |
| Sponsored Links |
|
|
|
|||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Ajax Form Help (validation and tips) | antonypeiris | AJAX | 4 | 02-15-2008 11:01 PM |
| how to write the code for user to input command ? | worried_student | C and C++ | 3 | 12-26-2007 09:11 PM |
| How to use same input for multiple while loops/cal.? Please help | thieflock | Java Help | 3 | 11-12-2007 09:26 AM |
| [C++] Validating user input | Xochiquetzal | C and C++ | 2 | 07-08-2007 06:18 PM |
| Java:Tutorial - User Input | John | Java Tutorials | 0 | 12-09-2006 08:25 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |