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, 10: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 06: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, 10: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, 06:59 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,034
Last Blog:
NaNoWriMo Day 23 The...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
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
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-03-2008, 08:37 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Input Validation

Why is that? Are there disadvantages of validating the input first?
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-03-2008, 09:05 AM
mholt mholt is offline
Newbie
 
Join Date: Jan 2008
Posts: 27
Rep Power: 3
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, 09:25 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
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.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-03-2008, 04:46 PM
dcs dcs is offline
Learning Programmer
 
Join Date: Mar 2008
Posts: 60
Rep Power: 2
dcs will become famous soon enough
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax Form Help (validation and tips) antonypeiris AJAX 4 02-15-2008 10:01 PM
how to write the code for user to input command ? worried_student C and C++ 3 12-26-2007 08:11 PM
How to use same input for multiple while loops/cal.? Please help thieflock Java Help 3 11-12-2007 08:26 AM
[C++] Validating user input Xochiquetzal C and C++ 2 07-08-2007 05:18 PM
Java:Tutorial - User Input John Java Tutorials 0 12-09-2006 07:25 PM


All times are GMT -5. The time now is 10:52 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads