DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: isdigit() and isalpha()

  1. #1
    uraneum Guest

    isdigit() and isalpha()


    Can anyone tell me whats wrong with this program, it will not iterate correctly!
    Thnaks Jack



    #include<iostream.h>
    #include<ctype.h>
    int main(){
    char number, ans;

    do{
    cout << "Enter number ";
    cin >> number;

    if(isdigit(number)){
    }
    else{
    cout << "Letter! Try again? y for yes, n for no ";
    cin >> ans;
    }while(ans == 'y' || ans == 'Y');
    cout << "You have entered number(s)";
    return 0;
    }


  2. #2
    John Guest

    Re: isdigit() and isalpha()


    try your while statement like this:

    while((ans == 'y') || (ans == 'Y'));


    "uraneum" <uraneum@email.msn.com> wrote:
    >
    >Can anyone tell me whats wrong with this program, it will not iterate correctly!
    >Thnaks Jack
    >
    >
    >
    >#include<iostream.h>
    >#include<ctype.h>
    >int main(){
    >char number, ans;
    >
    >do{
    > cout << "Enter number ";
    > cin >> number;
    >
    > if(isdigit(number)){
    > }
    > else{
    > cout << "Letter! Try again? y for yes, n for no ";
    > cin >> ans;
    >}while(ans == 'y' || ans == 'Y');
    > cout << "You have entered number(s)";
    >return 0;
    >}
    >



  3. #3
    John Guest

    Re: isdigit() and isalpha()


    You Also need another } before Your While Satement


    "John" <jreilly37@hotmail.com> wrote:
    >
    >try your while statement like this:
    >
    >while((ans == 'y') || (ans == 'Y'));
    >
    >
    >"uraneum" <uraneum@email.msn.com> wrote:
    >>
    >>Can anyone tell me whats wrong with this program, it will not iterate correctly!
    >>Thnaks Jack
    >>
    >>
    >>
    >>#include<iostream.h>
    >>#include<ctype.h>
    >>int main(){
    >>char number, ans;
    >>
    >>do{
    >> cout << "Enter number ";
    >> cin >> number;
    >>
    >> if(isdigit(number)){
    >> }
    >> else{
    >> cout << "Letter! Try again? y for yes, n for no ";
    >> cin >> ans;
    >>}while(ans == 'y' || ans == 'Y');
    >> cout << "You have entered number(s)";
    >>return 0;
    >>}
    >>

    >



  4. #4
    uraneum Guest

    Re: isdigit() and isalpha()


    "uraneum" <uraneum@email.msn.com> wrote:
    >
    >Can anyone tell me whats wrong with this program, it will not iterate correctly!
    >Thnaks Jack
    >
    >
    >
    >#include<iostream.h>
    >#include<ctype.h>
    >int main(){
    >char number, ans;
    >
    >do{
    > cout << "Enter number ";
    > cin >> number;
    >
    > if(isdigit(number)){
    > }
    > else{
    > cout << "Letter! Try again? y for yes, n for no ";
    > cin >> ans;
    >}while(ans == 'y' || ans == 'Y');
    > cout << "You have entered number(s)";
    >return 0;
    >}
    >


    Im really not talking about syntax here. I know I left out the { after else,
    and the while statement will run correctly as is. Anyone have any suggestions
    on the iteration of the do-while loop. If data in is a digit, the program
    executes the if statement, and bypasses the while statement. if the data
    entered is a chracter other than digit, the program executes the else statement
    right, and asks the user if they want to re-enter data. This uses the while
    statement and will iterate, right??


  5. #5
    Jim A. Guest

    Re: isdigit() and isalpha()


    Try this; I changed the if/else statement to just an if statement, which is
    all you were using anyways, and changed the boolean expresion to !isdigit(number).

    #include<iostream.h>
    #include<ctype.h>
    int main(){
    char number, ans;

    do{
    cout << "Enter number ";
    cin >> number;

    if(!isdigit(number))
    {
    cout << "Letter! Try again? y for yes, n for no ";
    cin >> ans;
    }
    }while((ans == 'y') || (ans == 'Y'));

    cout << "You have entered number(s)";

    return 0;
    }





    >>
    >>Can anyone tell me whats wrong with this program, it will not iterate correctly!
    >>Thnaks Jack
    >>
    >>
    >>
    >>#include<iostream.h>
    >>#include<ctype.h>
    >>int main(){
    >>char number, ans;
    >>
    >>do{
    >> cout << "Enter number ";
    >> cin >> number;
    >>
    >> if(isdigit(number)){
    >> }
    >> else{
    >> cout << "Letter! Try again? y for yes, n for no ";
    >> cin >> ans;
    >>}while(ans == 'y' || ans == 'Y');
    >> cout << "You have entered number(s)";
    >>return 0;
    >>}
    >>

    >



  6. #6
    uraneum Guest

    Re: isdigit() and isalpha()


    thanks Jim, I will try this new approach, I'll let you know if it works, thanks
    again!
    jack




    "Jim A." <jannunziato@aol.com> wrote:
    >
    >Try this; I changed the if/else statement to just an if statement, which

    is
    >all you were using anyways, and changed the boolean expresion to !isdigit(number).
    >
    >#include<iostream.h>
    >#include<ctype.h>
    >int main(){
    >char number, ans;
    >
    >do{
    > cout << "Enter number ";
    > cin >> number;
    >
    > if(!isdigit(number))
    > {
    > cout << "Letter! Try again? y for yes, n for no ";
    > cin >> ans;
    > }
    >}while((ans == 'y') || (ans == 'Y'));
    >
    > cout << "You have entered number(s)";
    >
    >return 0;
    >}
    >
    >
    >
    >
    >
    >>>
    >>>Can anyone tell me whats wrong with this program, it will not iterate

    correctly!
    >>>Thnaks Jack
    >>>
    >>>
    >>>
    >>>#include<iostream.h>
    >>>#include<ctype.h>
    >>>int main(){
    >>>char number, ans;
    >>>
    >>>do{
    >>> cout << "Enter number ";
    >>> cin >> number;
    >>>
    >>> if(isdigit(number)){
    >>> }
    >>> else{
    >>> cout << "Letter! Try again? y for yes, n for no ";
    >>> cin >> ans;
    >>>}while(ans == 'y' || ans == 'Y');
    >>> cout << "You have entered number(s)";
    >>>return 0;
    >>>}
    >>>

    >>

    >



  7. #7
    Nickolaus Guest

    Re: isdigit() and isalpha()


    "uraneum" <uraneum@email.msn.com> wrote:
    >
    >"uraneum" <uraneum@email.msn.com> wrote:
    >>
    >>Can anyone tell me whats wrong with this program, it will not iterate correctly!
    >>Thnaks Jack
    >>
    >>
    >>
    >>#include<iostream.h>
    >>#include<ctype.h>
    >>int main(){
    >>char number, ans;
    >>
    >>do{
    >> cout << "Enter number ";
    >> cin >> number;
    >>
    >> if(isdigit(number)){
    >> }
    >> else{
    >> cout << "Letter! Try again? y for yes, n for no ";
    >> cin >> ans;
    >>}while(ans == 'y' || ans == 'Y');
    >> cout << "You have entered number(s)";
    >>return 0;
    >>}
    >>

    >
    >Im really not talking about syntax here. I know I left out the { after else,
    >and the while statement will run correctly as is. Anyone have any suggestions
    >on the iteration of the do-while loop. If data in is a digit, the program
    >executes the if statement, and bypasses the while statement. if the data
    >entered is a chracter other than digit, the program executes the else statement
    >right, and asks the user if they want to re-enter data. This uses the while
    >statement and will iterate, right??
    >



    Lets not forget though, that you cannot debug syntactically
    incorrect code. So its important to notice these mistakes whether it seems
    important or not. Those small differences might make a huge difference when
    you forget to change them. Also, it seems to me that in your algorithim
    if a non-digit is entered and then the user inputs 'y' or 'Y' to try again,
    the ans is always that value and the loop might run infinitely. This is
    w/o compiling it, so I might be wrong. But I think you might need to make
    sure you re-initialize ans to prevent this.

    -Nickolaus

  8. #8
    uraneum Guest

    Re: isdigit() and isalpha()


    "Nickolaus" <watts.77@osu.edu> wrote:
    >
    >"uraneum" <uraneum@email.msn.com> wrote:
    >>
    >>"uraneum" <uraneum@email.msn.com> wrote:
    >>>
    >>>Can anyone tell me whats wrong with this program, it will not iterate

    correctly!
    >>>Thnaks Jack
    >>>
    >>>
    >>>
    >>>#include<iostream.h>
    >>>#include<ctype.h>
    >>>int main(){
    >>>char number, ans;
    >>>
    >>>do{
    >>> cout << "Enter number ";
    >>> cin >> number;
    >>>
    >>> if(isdigit(number)){
    >>> }
    >>> else{
    >>> cout << "Letter! Try again? y for yes, n for no ";
    >>> cin >> ans;
    >>>}while(ans == 'y' || ans == 'Y');
    >>> cout << "You have entered number(s)";
    >>>return 0;
    >>>}
    >>>

    >>
    >>Im really not talking about syntax here. I know I left out the { after

    else,
    >>and the while statement will run correctly as is. Anyone have any suggestions
    >>on the iteration of the do-while loop. If data in is a digit, the program
    >>executes the if statement, and bypasses the while statement. if the data
    >>entered is a chracter other than digit, the program executes the else statement
    >>right, and asks the user if they want to re-enter data. This uses the while
    >>statement and will iterate, right??
    >>

    >
    >
    >Lets not forget though, that you cannot debug syntactically
    >incorrect code. So its important to notice these mistakes whether it seems
    >important or not. Those small differences might make a huge difference

    when
    >you forget to change them. Also, it seems to me that in your algorithim
    >if a non-digit is entered and then the user inputs 'y' or 'Y' to try again,
    >the ans is always that value and the loop might run infinitely. This is
    >w/o compiling it, so I might be wrong. But I think you might need to make
    >sure you re-initialize ans to prevent this.
    >
    >-Nickolaus


    Thanks Nick, I do realize that syntatically* incorrect code will not compile
    (are we going backwards here)? I simply am asking about the iteration of
    the do-while loop. The variable ans is not being checked for isdigit. Only
    the variables in the
    isdigit() are checked against this. The while check of the do-while loop
    is validating a 'y' or any other character for that matter, and will iterate,
    or not iterate, based on the users input at that time. Anyone else have feedback?
    Has anyone successfully used this isdigit() validation of input.
    jack


  9. #9
    Nickolaus Guest

    Re: isdigit() and isalpha()



    >Thanks Nick, I do realize that syntatically* incorrect code will not compile
    >(are we going backwards here)? I simply am asking about the iteration of
    >the do-while loop. The variable ans is not being checked for isdigit. Only
    >the variables in the
    >isdigit() are checked against this. The while check of the do-while loop
    >is validating a 'y' or any other character for that matter, and will iterate,
    >or not iterate, based on the users input at that time. Anyone else have

    feedback?
    >Has anyone successfully used this isdigit() validation of input.
    >jack
    >



    Okay, but here is what I'm saying: you want to do {something} while something
    else is true right? That's
    what this loop is saying. So, if the user sets ans to 'y' or 'Y' then until
    ans is changed again, it will be
    equal to 'y' or 'Y', whatever it was set to. Therefore the 'something else'
    is always true and 'something
    always gets executed. Its a minor issue as with all the small details that
    have been brought up, I know.
    But the point is that through correcting these errors you might discover
    how to fix the bigger problem. And
    then again, maybe not, but its still important to correct these things--
    we don't know that You know they
    are there.

    -Nick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center