Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2005, 12:55 AM   #11
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
i added your code to that, but it didnt work.. was hoping someone could show me how i would change the segment i posted above to work with what you posted.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Nov 25th, 2005, 1:22 AM   #12
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
I'm just a newb so don't mind me if I'm wrong, but shouldn't the above look more like:

int i;
	
	while (1)
	{
	  cout<<"number?\n"<<endl;
	  cin>>i;
	  if (i<0 || i >18)
	  {
			  break;
	  }	  
	}
Cache is offline   Reply With Quote
Old Nov 25th, 2005, 1:22 AM   #13
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Cache
I'm just a newb so don't mind me if I'm wrong, but shouldn't the above look more like:

int i;
	
	while (1)
	{
	  cout<<"number?\n"<<endl;
	  cin>>i;
	  if (i<0 || i >18)
	  {
			  break;
	  }	  
	}
EDIT: Yup I'm wrong..sorry about that..

And now I've just quoted myself trying to edit my original post too..lol.:o
Cache is offline   Reply With Quote
Old Nov 25th, 2005, 1:51 AM   #14
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
OK, in a bid to make up for my last two ridiculous posts I've come up with the folowing:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	
	char input[10];
	int i;
	
	while(1)
	{
		 cout<<"number?\n"<<endl;
		 cin.getline(input,10);
		 i = atoi(input);
		 if ((i<=0) || (i>=20))
		 {
			cout << "\nERROR: Invalid selection\n";
			cout << "Press any key to retry. . .\n\n";
			cin.get();
			continue;
		 } else
		   {
			   break;
		   }
	}   
	
	cout<<"\nyou entered "<<i<<"\n"<<endl;
	
	system("pause");
	
	return 0;
}

Note you will have to include 'cstdlib' for atoi() to work.
Cache is offline   Reply With Quote
Old Nov 25th, 2005, 2:26 AM   #15
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
i thought my solution was stupid. i'm sure there's a better way to check for type compatibility.

cache's most recent solution seems to work the best.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Nov 25th, 2005, 2:47 AM   #16
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
One problem I have noticed though, atoi() seems to convert all non integer values (a, b, c, ect) to 0. So you wouldn't be able to use anything that =0.
Cache is offline   Reply With Quote
Old Nov 25th, 2005, 8:10 AM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There is a lot of voodoo and bullshit being recommended in this thread, which is a Bad Thang. The perils of input have been addressed on the forum, but let's cover them again, rather than recommend a nasty search.

Incidentally, Andro is on the right track, with the right ideas. Kilo, you should not recommend using non-portable code when such isn't necessary.

You rarely can control what the user provides. You only have the chance to validate it and clean it up. As an aid in that process, most input functions provide return values. One checks these to see if the user has managed to break the input mechanism. It's easy to do. If you're asking for input and not checking status you are either new (the most common case) or a fool (not uncommon enough). Cin is a collection of methods. The extraction operator (>>) doesn't return status directly (it returns the stream), so one must use additional methods. Among these are .good (), .bad () and .fail ().

Some input functions, among them cin, offer to do more for you than merely gather keystrokes. They offer conversion. They know how to convert, every bit as well as atoi knows how to convert, and most likely, better. Some things aren't convertible; in a conversion situation they will either break something or produce garbage. One must check for these things.

The example procedure illustrates some of this. If one is really picky, one distinguishes between bad (severe), fail, eof, and such.

#include <iostream>

using namespace std;

int getStrength ()
{
    unsigned int strength;
    unsigned int tries = 0;

    cout << endl << "Enter strength: ";
    while (true)
    {
        if (tries > 3)
        {
            cout << "You just can't get it, can you?" << endl;
            return -1;
        }
        // The following clears the error bits for the stream,
        // in case some usage broke it.
        cin.clear ();
        // Adjust the stream related pointers.  For istreams,
        // this effectively clears content.  It's possible for
        // it to fail.  Some istreams might not have buffers;
        if (cin.sync () == -1) return -1;
        cin >> strength;
        if (!cin.good ()) cout << "Invalid input; try again: ";
        else if (strength > 20) cout << "Invalid number, try again: ";
        else return strength;
        tries++;
    }
    return -1;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 25th, 2005, 8:14 AM   #18
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
What the hell...

Just do this:

int main()
{

        int x;
        while ( cin >> x )
                cout << x << endl;
        cout << "You not entered a number..."<< endl;

        system("PAUSE");
        return 0;
}
-=PARADOX=- is offline   Reply With Quote
Old Nov 25th, 2005, 8:43 AM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Because that doesn't get the job done....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 25th, 2005, 8:44 AM   #20
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
Why is that?
-=PARADOX=- is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC