Thread: Menu.

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep Power
    0

    Unhappy Menu.


    Hello,
    I want to create a menu that can read user input eg. "Peter Corbell" an the program must be able to check that the input is all letter with space between eg.Peter and Corbell and if input is a number, it print "error" message and prompt user to enter again. How am I going to program it as I am a novice to the and could not find or do not know how to find solution even after a few weeks of trying with reference to book.
    Can anyone please help me. Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    1)Read in the user data with cin.getline()

    2)isalpha() will check to see if each char is a letter.

    3)If you look at an ASCII code table, you will see that a space is stored as the integer 32, so you can check to see if there is a space somewhere between the first char and the last char of the input.

    4)You should consider spending a few months doing easier programs.

    5)Read Ivor Horton's Beginning C++(skip the overly complex bitwise operator section in Chapter 3)
    Last edited by 7stud; April 26th, 2003 at 05:54 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep Power
    0

    Just to say thank you.


    7stud,
    Thank to your advice and recommendation of the book. I will try to find the book. I am not sure on how to use the function in 1 and 2.
    I refer to my book and isalpha() is not easy for me. Please explain it to me in much simple way which relate to my problem if you are willing. For the name print out problem I had posted, my solution is to change the %s to %[^\n] in scanf and it works.
    Thank you for the cin.getline you suggest but I do not know how to use it. Thank you for helping again.
    Last edited by EDIT; April 27th, 2003 at 12:16 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    1) See here:

    http://www.cplusplus.com/doc/tutorial/tut3-2.html

    2)
    #include<cctype>//for isalpha()

    char text[50];
    cin.getline(text, 50);
    if(!isalpha(text[i]))
    ......cout<<"Input Error!: "<<text[i]<<endl;

    3)
    int count=0;
    if(text[i] == ' ') count++;
    if(count==0)
    .....cout<<"Input Error"<<endl;
    Last edited by 7stud; April 28th, 2003 at 03:22 AM.

IMN logo majestic logo threadwatch logo seochat tools logo