-1

To answer this question:

Read, from the keyboard, one character at a time until the user enters a letter ('A'..'Z' or 'a'..'z').

Example: LETTER ? 4 // invalid character; so, ask for another letter LETTER ? . // repeat ... LETTER ? / // ... LETTER ? # LETTER ? k // finally, the user typed a letter !!!

I wrote the following code:

 #include <iostream>
 #include <stdio.h>
 #include <ctime>
 #include <string.h>
 #include <cstring>
 #include <ctype.h>

 using namespace std;
 int main(int letter){

   cout << "LETTER ? ";
   cin >> letter;
   if (!isalpha(letter))
   {main(letter);}
   else
   {};

       return(0);

       };

If it is a number it is working. If it is a symbol or a letter it's saying LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? LETTER ? (...)

Could you please help me?

  • Why are you calling `main` recursively? it's a bit unsual and if I'm right it's nonstandard in C++. Use a loop instead of. EDIT: And your parameters to main function isn't right. – The Mask Apr 01 '14 at 19:21
  • 3
    Yeah, this code is invalid. See: [Is it legal to recurse into main() in C++?](http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c) – Blastfurnace Apr 01 '14 at 19:22

3 Answers3

2

Your code has undefined behaviour. The C++ Standard does not allow to call function main recursively.

Also this declaration of main

int main(int letter){

is not C++ Standard compliant. It could be an implementation defined declaration of main if the documentation of the compiler would describe it.

As for the repeating messages then when you entered a non-digit (variable letter has type int) stream std::cin got erraneous internal state and if you will not clear this state with call

std::cin.clear();

std:;cin will not allow to enter something else.

If there is no requirement that the function would be recursive then you could write simply

#include <iostream>
#include <cctype>

int main()
{
   char letter;

   do
   {
      std::cout << "LETTER ? ";
      std::cin >> letter;
   } while ( std::cin && std::isalpha( letter ) );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Your current input parameter is int. Use char or string instead.

int main(char letter)
{
    cout << "LETTER ? ";
    cin >> letter;
    cin.clear();            //Good to have this (see comments below)
    cin.ignore(200, '\n');  //This 2 lines will allow you to continue inputting when unexpected input is given

    ......
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
0

If your only task is reading from the keyboard until a,z,A,Z is entered without printing out the letters, this should work:

  #include <iostream>

    using namespace std;

    int main()
    {
    char letter = 'b';
    while (letter != 'a' && letter != 'A' && letter != 'Z' && letter != 'z')
    cin >> letter;
    return 0;
    }
  • @Blastfurnace I also wonder who upvoted this answer.:) Even if do not take into account the invalid condition of if statement the program does do the same as the original program tries to do.:) In Russhia there is said usually "do not have 100 dollars but have 100 friends".:) – Vlad from Moscow Apr 01 '14 at 19:45
  • Sorry I fixed it. @VladfromMoscow – user3486543 Apr 01 '14 at 19:51
  • @user3486543 Nevertheless it is not the same as the author of the thread want to have. – Vlad from Moscow Apr 01 '14 at 19:54
  • The OP clearly wants to check if it's in the range from `'a'` to `'z'`, not if it's exactly `a` or `z`. `std::isalpha` is the correct way to do this and he is already using it. Your solution is only making it worse. – Excelcius Apr 01 '14 at 20:12
  • @Excelcius Sorry, I'm new here and this question was sort of confusing. Do you recommend that I delete my post? – user3486543 Apr 01 '14 at 20:43
  • @user3486543 It's ok, the question is somewhat confusing that's true. I leave it up to you to either edit your answer or delete it. You can also leave it the way it is although some might downvote it. – Excelcius Apr 01 '14 at 20:45