Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-24-2008, 11:18 AM
Lab Lab is offline
Newbie
 
Join Date: Apr 2008
Location: Clifton Park, NY
Age: 44
Posts: 3
Rep Power: 0
Lab is on a distinguished road
Exclamation Input Validation

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.
the problem is if a charachter is entered rather than a number the programm goes crazy to say the least

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 11:45 AM
R-G's Avatar   
R-G R-G is offline
Programmer
 
Join Date: Apr 2007
Location: Europe
Posts: 144
Rep Power: 0
R-G is an unknown quantity at this point
Post

I think, with an similar example of a source software code algorithm, written in the computer programming language C++ you will understand it better.

Code:
while (!Valid)
    {
        cout << "Enter a number string: ";
        cin >> Number;
        if (cin)
        {
            cin.ignore(numeric_limits<int>::max(), '\n');
            if (cin.gcount() == +1)
            {
                if ( Number >= 0)
                {
                    Valid = true;
                }
            }
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<int>::max(), '\n');
            cout << "Not a valid number string. Try again: \n";
        }
    }
__________________
Like an angel without a sense of mercy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 07:59 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Input Validation

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-03-2008, 09:37 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,897
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Input Validation

Why is that? Are there disadvantages of validating the input first?
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-03-2008, 10:05 AM
mholt mholt is offline
Newbie
 
Join Date: Jan 2008
Posts: 27
Rep Power: 4
mholt is on a distinguished road
Default Re: Input Validation

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;
}
If you modify it a little bit, you can use this code for password input, so the password will not be displayed on the screen. (Print out "*" instead of ch.)

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-03-2008, 10:25 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,897
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Input Validation

Thanks - if you just want a character, then I suppose it's easy enough just to get the whole line, then parse out the character required.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-03-2008, 05:46 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: Input Validation

Ignoring all characters - Dev Shed
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 11:51 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads