Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,671 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,179 people online right now. Registration is fast and FREE... Join Now!




testing for character in a loop

 
Reply to this topicStart new topic

testing for character in a loop, I am having trouble with a characters in a number test

micks4st
post 3 Sep, 2007 - 08:09 PM
Post #1


New D.I.C Head

*
Joined: 25 Aug, 2007
Posts: 4


My Contributions


This is homework assignment

abstract: a game for a user to guess the number that is hardcded.

issue: test that identifies user input of character and informs the user to use a number.
all the number if statements work but when a character is input it goes into a continuous
loop

I /* */ the if statment for the character any suggestion would be helpful.



CODE
#include <iostream>

using std::cin;
using std::cout;
using std::endl;


int main()
{
    int qnum = 0;                                                              //store the guessed number
    int mynum = 88;                                                          //the number to guess
    //char letter = 0;[/color]
    cout << "Mick's Guess my number game.\n";                  //Introduction to the game

    
        do                                                               //Starts the loop
        {
            
            cout << "Select a number between 1 and 99." << endl;        //give the user the parameters
            
            cout << "Guess what number I am thinking of?:  " << endl;   //tells the user to input their guess number
            cin >> qnum;                                                                    // input to qnum storage
            

            if((letter >= 'a') || (letter <= 'z'))
                cout << endl
                     << " You select a alfpha character, select a number.\n" <<endl;*/[/color]
            if((qnum <= 0 ) || (qnum >= 100))                                    //test the number to see if meets the parameters
            cout << endl
                   << "Try again, your number must between 1 and 99\n\n" << endl;  //output msg telling user to select a number between 1 - 99


            if((qnum >= 1 ) && (qnum <= 87))                                     //test if number is to low
                cout << endl
                       << "Your guess is to low try again\n\n" << endl;            // output msg for low guess

            if((qnum >= 89 ) && (qnum <= 99))                                    //test if number is to high
                cout << endl
                                   << "Your guess is to HIGH try again\n\n" << endl;           // output msg for High guess

            } while(((qnum >= 1) && (qnum <= 87)) || ((qnum >= 89 ) && (qnum <= 99)) ||
                ((qnum <= 0 ) || (qnum >= 100)));       //keeps the loop running until user guesses 88
                
                cout << endl
                                   << "Congradulations you won!!" << endl;                      //output for guessing the right number

    return 0;


}
User is offlineProfile CardPM

Go to the top of the page

enpey
post 3 Sep, 2007 - 10:51 PM
Post #2


D.I.C Head

**
Joined: 2 May, 2007
Posts: 59



Thanked 1 times
My Contributions


You need to post your code within [ code ] and [ /code ] (without spaces) like the background of the post window states.

A clarification of your problem may make it easier to help. If a character is input as a guess (qnum), or as mynum ?
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 01:54 AM
Post #3


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


The problem is that your input statement fails when you enter a character, and you're not emptying the stream or clearing the error flags.

Have a look at this modified version of your program, in particular the test for cin.fail()
CODE
#include <iostream>
#include <limits>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    int qnum;
    do
    {
        cout << "Enter a number ";
        cin >> qnum;

        if( cin.fail() )
        {
            cout<< "Error" << endl;

            //Clear error flags
            cin.clear();

            //Ignore all remaining characters in the stream
            // up-to and including the new-line character
            cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        }
        else
        {
            cout << "You entered " << qnum << endl;
        }
    }while ( qnum != 88 );
}


As for your if statements, you've got a huge amount of redundant testing in there... think how you could slim it down a bit.

This post has been edited by Bench: 4 Sep, 2007 - 01:58 AM
User is offlineProfile CardPM

Go to the top of the page

dan_ram
post 4 Sep, 2007 - 06:30 AM
Post #4


D.I.C Head

**
Joined: 15 Aug, 2007
Posts: 56


My Contributions


can a character be taken into an integer? it is given as
CODE
int qnum;
rite?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 4 Sep, 2007 - 06:39 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Correct...but the user is trying to restrict the input to numbers.
User is online!Profile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 12:41 PM
Post #6


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(dan_ram @ 4 Sep, 2007 - 03:30 PM) *

can a character be taken into an integer? it is given as
CODE
int qnum;
rite?

Well, yes and no. a character is an integral value, so in theory, there's nothing wrong with it.

in practice, cin gets picky. If you type an non-numeric character when cin is expecting a number, cin gets a bit confused, so the input fails

This post has been edited by Bench: 4 Sep, 2007 - 12:42 PM
User is offlineProfile CardPM

Go to the top of the page

micks4st
post 4 Sep, 2007 - 04:40 PM
Post #7


New D.I.C Head

*
Joined: 25 Aug, 2007
Posts: 4


My Contributions


QUOTE(enpey @ 3 Sep, 2007 - 11:51 PM) *

You need to post your code within [ code ] and [ /code ] (without spaces) like the background of the post window states.

A clarification of your problem may make it easier to help. If a character is input as a guess (qnum), or as mynum ?



enpey,

Thank you for the comments. I will use your suggestions next time I post my code.

icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

micks4st
post 4 Sep, 2007 - 04:51 PM
Post #8


New D.I.C Head

*
Joined: 25 Aug, 2007
Posts: 4


My Contributions


QUOTE(Bench @ 4 Sep, 2007 - 02:54 AM) *

The problem is that your input statement fails when you enter a character, and you're not emptying the stream or clearing the error flags.

Have a look at this modified version of your program, in particular the test for cin.fail()
CODE
#include <iostream>
#include <limits>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    int qnum;
    do
    {
        cout << "Enter a number ";
        cin >> qnum;

        if( cin.fail() )
        {
            cout<< "Error" << endl;

            //Clear error flags
            cin.clear();

            //Ignore all remaining characters in the stream
            // up-to and including the new-line character
            cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        }
        else
        {
            cout << "You entered " << qnum << endl;
        }
    }while ( qnum != 88 );
}


As for your if statements, you've got a huge amount of redundant testing in there... think how you could slim it down a bit.



QUOTE


Thank you for the comments and the help. I am interested in improving my programming skill but the text book we are using is hard to follow and not user freindly. Do you or any suggestions for books that would help me. Or just keep programming and trial / error training.

Micks4st

icon_up.gif icon_up.gif cool.gif
User is offlineProfile CardPM

Go to the top of the page

Unknown Hero
post 4 Sep, 2007 - 06:44 PM
Post #9


New D.I.C Head

Group Icon
Joined: 4 Sep, 2007
Posts: 35



Thanked 7 times

Dream Kudos: 50
My Contributions


Try to declare qnum as char and then convert it to int.

Play with ASCII code a bit. 'A'...'Z' have ASCII code 65-90, and 'a'...'z' have ASCII code 97-122.

Numbers '0'...'9' have ASCII code 48-57.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 5 Sep, 2007 - 04:01 AM
Post #10


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(Unknown Hero @ 5 Sep, 2007 - 03:44 AM) *

Try to declare qnum as char and then convert it to int.

Play with ASCII code a bit. 'A'...'Z' have ASCII code 65-90, and 'a'...'z' have ASCII code 97-122.

Numbers '0'...'9' have ASCII code 48-57.

That wouldn't work - if the user typed 99 when cin was looking for a char, cin would retrieve '9'.

A better solution would be to input a std::string using getline, and convert with a stringstream (This is generally better than clearing error flags on cin, and having to wipe out unwanted characters)

CODE
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    using namespace std;
    string input;
    cout << "Enter a number 1-99 inclusive: ";
    getline( cin, input );

    stringstream ss( input );
    int n;
    if( ss >> n )
    {
        cout << "You entered: " << n << endl;
    }
    else
    {
        cout << "Invalid input" << endl;
    }
}


This post has been edited by Bench: 5 Sep, 2007 - 04:01 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:01AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month