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, 8:47 AM   #21
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Perhaps you should actually compile and run it and see what happens if you enter many things, some of which are not numbers, and some of which are less than zero or greater than 20. This thread doesn't need more horseshit, it reeks as it stands. A disappearing window or reaching "pause" doesn't necessarily represent a good solution, incidentally
__________________
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:50 AM   #22
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
My example is not the solution to the problem, but can be easily adapted...
-=PARADOX=- is offline   Reply With Quote
Old Nov 25th, 2005, 8:54 AM   #23
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by -=PARADOX=-
My example is not the solution to the problem, but can be easily adapted...
Yeah, and when you adapt it will be the same as Dawei's code .
Polyphemus_ is offline   Reply With Quote
Old Nov 25th, 2005, 9:18 AM   #24
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
My example is not the solution to the problem, but can be easily adapted...
Then why introduce your "solution" like this:
Quote:
What the hell...

Just do this:
__________________
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, 11:15 AM   #25
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The C++ FAQ suggests you do it this way:
while ((std::cout << "Enter a number: ") && !(std::cin >> integer))
{
    std::cout << "OK, that's not a number, genius." << std::endl;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
DaWei's code will work equally well, so it's just a matter of preference.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 25th, 2005, 11:29 AM   #26
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Cache
One problem I have noticed though, atoi() seems to convert all non integer values (a, b, c, ect) to 0.
Is the above quote voodoo and bullshit?
Cache is offline   Reply With Quote
Old Nov 25th, 2005, 11:32 AM   #27
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
This works when i change "int getStrength" to "int main", but when i try to add this in my code it compiles, but still doesnt fix the input problem.. im pretty sure im doing something wrong, would be nice if someone could apply this code below, to the example of my program i am trying to fix a few posts up.

#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;
}
__________________

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, 11:42 AM   #28
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Well, what's the problem?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 25th, 2005, 12:00 PM   #29
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
problem

just ignore the backround sounds ..

once a letter or any number following "-", this problem starts happening.

for example, if i type abc, it will start the problem, or if i type 252- or 1- or 124-, ect, it will start the problem.
__________________

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, 12:18 PM   #30
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Where does it say: "you must spend at least one point"?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion 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 9:40 AM.

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