C++, check if user entered more than one letter into a char












-2















I want to get a char input and if it contain more than one letter it should print a custom message.
for example 'a' is good 'abc' is not good.
Here's my code



{
std::cout << "Enter information for your objects" << std::endl;
const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p'; char shapetype;
char x = 'y';
std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;
std::cin >> shapetype;
switch(shapetype) {
case 'a':
//do something
break;
case 'b':
//idk
break;
default:
//doesnt matter
break;
}
}


so the problem is when its reading the char if i type for example 'abc'
it gets into the 'a' case.



Thanks!










share|improve this question




















  • 7





    A char can contain only one letter - perhaps you are thinking of a string?

    – Neil Butterworth
    Nov 24 '18 at 22:29











  • Nope, i think its weird too, but i will edit my question to give more information.

    – yarinc
    Nov 24 '18 at 22:32











  • Edited the code, i hope its understandable now

    – yarinc
    Nov 24 '18 at 22:40











  • Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

    – alter igel
    Nov 24 '18 at 22:41






  • 2





    When you read "abc" into a single char, only the first char is read.

    – Neil Butterworth
    Nov 24 '18 at 22:42
















-2















I want to get a char input and if it contain more than one letter it should print a custom message.
for example 'a' is good 'abc' is not good.
Here's my code



{
std::cout << "Enter information for your objects" << std::endl;
const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p'; char shapetype;
char x = 'y';
std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;
std::cin >> shapetype;
switch(shapetype) {
case 'a':
//do something
break;
case 'b':
//idk
break;
default:
//doesnt matter
break;
}
}


so the problem is when its reading the char if i type for example 'abc'
it gets into the 'a' case.



Thanks!










share|improve this question




















  • 7





    A char can contain only one letter - perhaps you are thinking of a string?

    – Neil Butterworth
    Nov 24 '18 at 22:29











  • Nope, i think its weird too, but i will edit my question to give more information.

    – yarinc
    Nov 24 '18 at 22:32











  • Edited the code, i hope its understandable now

    – yarinc
    Nov 24 '18 at 22:40











  • Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

    – alter igel
    Nov 24 '18 at 22:41






  • 2





    When you read "abc" into a single char, only the first char is read.

    – Neil Butterworth
    Nov 24 '18 at 22:42














-2












-2








-2








I want to get a char input and if it contain more than one letter it should print a custom message.
for example 'a' is good 'abc' is not good.
Here's my code



{
std::cout << "Enter information for your objects" << std::endl;
const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p'; char shapetype;
char x = 'y';
std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;
std::cin >> shapetype;
switch(shapetype) {
case 'a':
//do something
break;
case 'b':
//idk
break;
default:
//doesnt matter
break;
}
}


so the problem is when its reading the char if i type for example 'abc'
it gets into the 'a' case.



Thanks!










share|improve this question
















I want to get a char input and if it contain more than one letter it should print a custom message.
for example 'a' is good 'abc' is not good.
Here's my code



{
std::cout << "Enter information for your objects" << std::endl;
const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p'; char shapetype;
char x = 'y';
std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;
std::cin >> shapetype;
switch(shapetype) {
case 'a':
//do something
break;
case 'b':
//idk
break;
default:
//doesnt matter
break;
}
}


so the problem is when its reading the char if i type for example 'abc'
it gets into the 'a' case.



Thanks!







c++ char






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 18 at 9:42









Joey Mallone

2,26361933




2,26361933










asked Nov 24 '18 at 22:28









yarincyarinc

216




216








  • 7





    A char can contain only one letter - perhaps you are thinking of a string?

    – Neil Butterworth
    Nov 24 '18 at 22:29











  • Nope, i think its weird too, but i will edit my question to give more information.

    – yarinc
    Nov 24 '18 at 22:32











  • Edited the code, i hope its understandable now

    – yarinc
    Nov 24 '18 at 22:40











  • Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

    – alter igel
    Nov 24 '18 at 22:41






  • 2





    When you read "abc" into a single char, only the first char is read.

    – Neil Butterworth
    Nov 24 '18 at 22:42














  • 7





    A char can contain only one letter - perhaps you are thinking of a string?

    – Neil Butterworth
    Nov 24 '18 at 22:29











  • Nope, i think its weird too, but i will edit my question to give more information.

    – yarinc
    Nov 24 '18 at 22:32











  • Edited the code, i hope its understandable now

    – yarinc
    Nov 24 '18 at 22:40











  • Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

    – alter igel
    Nov 24 '18 at 22:41






  • 2





    When you read "abc" into a single char, only the first char is read.

    – Neil Butterworth
    Nov 24 '18 at 22:42








7




7





A char can contain only one letter - perhaps you are thinking of a string?

– Neil Butterworth
Nov 24 '18 at 22:29





A char can contain only one letter - perhaps you are thinking of a string?

– Neil Butterworth
Nov 24 '18 at 22:29













Nope, i think its weird too, but i will edit my question to give more information.

– yarinc
Nov 24 '18 at 22:32





Nope, i think its weird too, but i will edit my question to give more information.

– yarinc
Nov 24 '18 at 22:32













Edited the code, i hope its understandable now

– yarinc
Nov 24 '18 at 22:40





Edited the code, i hope its understandable now

– yarinc
Nov 24 '18 at 22:40













Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

– alter igel
Nov 24 '18 at 22:41





Is your question, "How do I prevent the user from entering more than one letter?" If so, you'll gain a lot by editing your title

– alter igel
Nov 24 '18 at 22:41




2




2





When you read "abc" into a single char, only the first char is read.

– Neil Butterworth
Nov 24 '18 at 22:42





When you read "abc" into a single char, only the first char is read.

– Neil Butterworth
Nov 24 '18 at 22:42












2 Answers
2






active

oldest

votes


















2















char check;
std::cin >> check;



You are only reading one character, you can't really guess whether the user typed in more than one letter or not.



It might be a better idea to read a std::string by getting a whole line from std::cin, then checking if the string is really what you're expecting it to be.



Here is an example from std::getline:



#include <iostream>
#include <string>

int main()
{
std::string s;
std::getline(std::cin, s);
std::cout << s;
}


When you type in hello world and press enter, it will print it back the same text.



If you still want to use a switch to check for just a single character you could check if s.size() == 1 then do your switch(s[0]), else fail.



Alternatively, you could do a bunch of if-else over the string, e.g. if (s == "a") { /* ... */ }, etc.






share|improve this answer































    -1














    Firstly, you need to understand that a char can ONLY contain a single character. It doesn't matter if the user types an entire sentence, your char shapetype will only end up ever receiving the first character from the sentence they type.



    Your main problem I think here is that you're using std::cin and this doesn't validate or care about how much input someone types. All that is going to happen is that the first character that they type is going to end up in your char shapetype variable. That means, to your code, someone typing "a small hole" is exactly the same as if they typed just "a".



    So the questions you should really ask yourself are:




    • Do you care if the user types more than a single character? If the answer to this question is yes, then you need another approach.


    • If you don't care if the user types more than one character, then you can simply do as others have suggested and capture the input into a std::string using something like getline(), check .empty() and .size() for greater than 1 then output an appropriate error message. Other than this, you know you've got exactly a single character. You might want to do a case conversion so that your switch will work with both uppercase and lowercase input too.



    Now if you want to just accept a single character you can use something like getchar() that captures a single character only.



    #include <stdio.h>

    int main(int argc, char** argv) {
    std::cout << "Enter information for your objects" << std::endl;
    const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p';
    std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;

    int shapetype = getchar();

    switch(shapetype) {
    case 'a':
    //do something
    break;
    case 'b':
    //idk
    break;
    default:
    //doesnt matter
    break;
    }
    }


    You need to also take care that getchar() actually returns an int rather than a char. So you might need to perhaps cast it, if needed. However, the above should solve your problem.






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462909%2fc-check-if-user-entered-more-than-one-letter-into-a-char%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2















      char check;
      std::cin >> check;



      You are only reading one character, you can't really guess whether the user typed in more than one letter or not.



      It might be a better idea to read a std::string by getting a whole line from std::cin, then checking if the string is really what you're expecting it to be.



      Here is an example from std::getline:



      #include <iostream>
      #include <string>

      int main()
      {
      std::string s;
      std::getline(std::cin, s);
      std::cout << s;
      }


      When you type in hello world and press enter, it will print it back the same text.



      If you still want to use a switch to check for just a single character you could check if s.size() == 1 then do your switch(s[0]), else fail.



      Alternatively, you could do a bunch of if-else over the string, e.g. if (s == "a") { /* ... */ }, etc.






      share|improve this answer




























        2















        char check;
        std::cin >> check;



        You are only reading one character, you can't really guess whether the user typed in more than one letter or not.



        It might be a better idea to read a std::string by getting a whole line from std::cin, then checking if the string is really what you're expecting it to be.



        Here is an example from std::getline:



        #include <iostream>
        #include <string>

        int main()
        {
        std::string s;
        std::getline(std::cin, s);
        std::cout << s;
        }


        When you type in hello world and press enter, it will print it back the same text.



        If you still want to use a switch to check for just a single character you could check if s.size() == 1 then do your switch(s[0]), else fail.



        Alternatively, you could do a bunch of if-else over the string, e.g. if (s == "a") { /* ... */ }, etc.






        share|improve this answer


























          2












          2








          2








          char check;
          std::cin >> check;



          You are only reading one character, you can't really guess whether the user typed in more than one letter or not.



          It might be a better idea to read a std::string by getting a whole line from std::cin, then checking if the string is really what you're expecting it to be.



          Here is an example from std::getline:



          #include <iostream>
          #include <string>

          int main()
          {
          std::string s;
          std::getline(std::cin, s);
          std::cout << s;
          }


          When you type in hello world and press enter, it will print it back the same text.



          If you still want to use a switch to check for just a single character you could check if s.size() == 1 then do your switch(s[0]), else fail.



          Alternatively, you could do a bunch of if-else over the string, e.g. if (s == "a") { /* ... */ }, etc.






          share|improve this answer














          char check;
          std::cin >> check;



          You are only reading one character, you can't really guess whether the user typed in more than one letter or not.



          It might be a better idea to read a std::string by getting a whole line from std::cin, then checking if the string is really what you're expecting it to be.



          Here is an example from std::getline:



          #include <iostream>
          #include <string>

          int main()
          {
          std::string s;
          std::getline(std::cin, s);
          std::cout << s;
          }


          When you type in hello world and press enter, it will print it back the same text.



          If you still want to use a switch to check for just a single character you could check if s.size() == 1 then do your switch(s[0]), else fail.



          Alternatively, you could do a bunch of if-else over the string, e.g. if (s == "a") { /* ... */ }, etc.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 22:47









          AsuAsu

          1,4861122




          1,4861122

























              -1














              Firstly, you need to understand that a char can ONLY contain a single character. It doesn't matter if the user types an entire sentence, your char shapetype will only end up ever receiving the first character from the sentence they type.



              Your main problem I think here is that you're using std::cin and this doesn't validate or care about how much input someone types. All that is going to happen is that the first character that they type is going to end up in your char shapetype variable. That means, to your code, someone typing "a small hole" is exactly the same as if they typed just "a".



              So the questions you should really ask yourself are:




              • Do you care if the user types more than a single character? If the answer to this question is yes, then you need another approach.


              • If you don't care if the user types more than one character, then you can simply do as others have suggested and capture the input into a std::string using something like getline(), check .empty() and .size() for greater than 1 then output an appropriate error message. Other than this, you know you've got exactly a single character. You might want to do a case conversion so that your switch will work with both uppercase and lowercase input too.



              Now if you want to just accept a single character you can use something like getchar() that captures a single character only.



              #include <stdio.h>

              int main(int argc, char** argv) {
              std::cout << "Enter information for your objects" << std::endl;
              const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p';
              std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;

              int shapetype = getchar();

              switch(shapetype) {
              case 'a':
              //do something
              break;
              case 'b':
              //idk
              break;
              default:
              //doesnt matter
              break;
              }
              }


              You need to also take care that getchar() actually returns an int rather than a char. So you might need to perhaps cast it, if needed. However, the above should solve your problem.






              share|improve this answer






























                -1














                Firstly, you need to understand that a char can ONLY contain a single character. It doesn't matter if the user types an entire sentence, your char shapetype will only end up ever receiving the first character from the sentence they type.



                Your main problem I think here is that you're using std::cin and this doesn't validate or care about how much input someone types. All that is going to happen is that the first character that they type is going to end up in your char shapetype variable. That means, to your code, someone typing "a small hole" is exactly the same as if they typed just "a".



                So the questions you should really ask yourself are:




                • Do you care if the user types more than a single character? If the answer to this question is yes, then you need another approach.


                • If you don't care if the user types more than one character, then you can simply do as others have suggested and capture the input into a std::string using something like getline(), check .empty() and .size() for greater than 1 then output an appropriate error message. Other than this, you know you've got exactly a single character. You might want to do a case conversion so that your switch will work with both uppercase and lowercase input too.



                Now if you want to just accept a single character you can use something like getchar() that captures a single character only.



                #include <stdio.h>

                int main(int argc, char** argv) {
                std::cout << "Enter information for your objects" << std::endl;
                const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p';
                std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;

                int shapetype = getchar();

                switch(shapetype) {
                case 'a':
                //do something
                break;
                case 'b':
                //idk
                break;
                default:
                //doesnt matter
                break;
                }
                }


                You need to also take care that getchar() actually returns an int rather than a char. So you might need to perhaps cast it, if needed. However, the above should solve your problem.






                share|improve this answer




























                  -1












                  -1








                  -1







                  Firstly, you need to understand that a char can ONLY contain a single character. It doesn't matter if the user types an entire sentence, your char shapetype will only end up ever receiving the first character from the sentence they type.



                  Your main problem I think here is that you're using std::cin and this doesn't validate or care about how much input someone types. All that is going to happen is that the first character that they type is going to end up in your char shapetype variable. That means, to your code, someone typing "a small hole" is exactly the same as if they typed just "a".



                  So the questions you should really ask yourself are:




                  • Do you care if the user types more than a single character? If the answer to this question is yes, then you need another approach.


                  • If you don't care if the user types more than one character, then you can simply do as others have suggested and capture the input into a std::string using something like getline(), check .empty() and .size() for greater than 1 then output an appropriate error message. Other than this, you know you've got exactly a single character. You might want to do a case conversion so that your switch will work with both uppercase and lowercase input too.



                  Now if you want to just accept a single character you can use something like getchar() that captures a single character only.



                  #include <stdio.h>

                  int main(int argc, char** argv) {
                  std::cout << "Enter information for your objects" << std::endl;
                  const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p';
                  std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;

                  int shapetype = getchar();

                  switch(shapetype) {
                  case 'a':
                  //do something
                  break;
                  case 'b':
                  //idk
                  break;
                  default:
                  //doesnt matter
                  break;
                  }
                  }


                  You need to also take care that getchar() actually returns an int rather than a char. So you might need to perhaps cast it, if needed. However, the above should solve your problem.






                  share|improve this answer















                  Firstly, you need to understand that a char can ONLY contain a single character. It doesn't matter if the user types an entire sentence, your char shapetype will only end up ever receiving the first character from the sentence they type.



                  Your main problem I think here is that you're using std::cin and this doesn't validate or care about how much input someone types. All that is going to happen is that the first character that they type is going to end up in your char shapetype variable. That means, to your code, someone typing "a small hole" is exactly the same as if they typed just "a".



                  So the questions you should really ask yourself are:




                  • Do you care if the user types more than a single character? If the answer to this question is yes, then you need another approach.


                  • If you don't care if the user types more than one character, then you can simply do as others have suggested and capture the input into a std::string using something like getline(), check .empty() and .size() for greater than 1 then output an appropriate error message. Other than this, you know you've got exactly a single character. You might want to do a case conversion so that your switch will work with both uppercase and lowercase input too.



                  Now if you want to just accept a single character you can use something like getchar() that captures a single character only.



                  #include <stdio.h>

                  int main(int argc, char** argv) {
                  std::cout << "Enter information for your objects" << std::endl;
                  const char circle = 'c', quadrilateral = 'q', rectangle = 'r', parallelogram = 'p';
                  std::cout << "which shape would you like to work with?.. nc=circle, q = quadrilateral, r = rectangle, p = parallelogram" << std::endl;

                  int shapetype = getchar();

                  switch(shapetype) {
                  case 'a':
                  //do something
                  break;
                  case 'b':
                  //idk
                  break;
                  default:
                  //doesnt matter
                  break;
                  }
                  }


                  You need to also take care that getchar() actually returns an int rather than a char. So you might need to perhaps cast it, if needed. However, the above should solve your problem.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 26 '18 at 12:23

























                  answered Nov 24 '18 at 23:06









                  The WelderThe Welder

                  344213




                  344213






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462909%2fc-check-if-user-entered-more-than-one-letter-into-a-char%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen