How to accept unknown type input (ie either 'A' or '1')

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harishapur
    New Member
    • Feb 2008
    • 4

    How to accept unknown type input (ie either 'A' or '1')

    Hello I'm new to the forum and to C++. I have one of what will probably be many questions. How do I code and unkown input. For example if I want to give the user
    a choice of A or 1. Someone suggested using a string, but I don't know how. Thanks for any help.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by Harishapur
    Hello I'm new to the forum and to C++. I have one of what will probably be many questions. How do I code and unkown input. For example if I want to give the user
    a choice of A or 1. Someone suggested using a string, but I don't know how. Thanks for any help.
    Your question is not clear to me. Please explain your exact problem in some detail!

    Regards

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      If it is just A or just 1 you won't have too much trouble, and a string will work well, though if you are going to be trying to parse input such as 'aslkd7i3jkm' or even sentences with numbers in there, you will have a bit more trouble as you won't know how long the word is, how long the number is, things like that, and you'll have to parse the string out.

      So the first step would be to define clearly what inputs you will allow.

      Comment

      • Harishapur
        New Member
        • Feb 2008
        • 4

        #4
        Originally posted by zodilla58
        Your question is not clear to me. Please explain your exact problem in some detail!

        Regards
        Say i have a set of parameters:
        1,2,3,4,5,a,b,c ,d,e
        i know one of these will be entered, but i dont know which one. I cant use int or char, so how do i code it.

        code sample:
        "Please enter your favorite of the following: 1,2,3,4,5,a,b,c ,d,e." _

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          I cant use int or char, so how do i code it.
          Believe it or not, when you see 1 on the screen, you are actually seeing a character. The internal numeric representation of the character 1 depends on whatever system is in use, like ASCII or Unicode.

          I.E., you can ask your user to enter a choice, store it in a char. And then later test to see if that char corresponds to something in our numeric system, and then convert appropriately. Do you understand the above, or do you need to me explain a portion of it further?

          Comment

          • Harishapur
            New Member
            • Feb 2008
            • 4

            #6
            Originally posted by oler1s
            Believe it or not, when you see 1 on the screen, you are actually seeing a character. The internal numeric representation of the character 1 depends on whatever system is in use, like ASCII or Unicode.

            I.E., you can ask your user to enter a choice, store it in a char. And then later test to see if that char corresponds to something in our numeric system, and then convert appropriately. Do you understand the above, or do you need to me explain a portion of it further?
            A portion further,
            How do you access the alternate system in order to store a character as a number.

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              It's automatic, characters are stored as integers according to the ASCII standard in C++. Here is a table of the standard that shows the various conversion.

              Comment

              • Harishapur
                New Member
                • Feb 2008
                • 4

                #8
                Originally posted by Laharl
                It's automatic, characters are stored as integers according to the ASCII standard in C++. Here is a table of the standard that shows the various conversion.
                can u show me some sample code i cant get it to work

                [CODE=cpp]#include <iostream>

                using namespace std;

                int main()
                {
                char favorite;

                cout << "Enter your favorite: a,b,c,d,e,1,2,3 ,4,5.";
                cin >> favorite;

                if (favorite == 1)
                {
                cout << "hooray";
                }
                return 0;
                }[/CODE]
                Last edited by Ganon11; Feb 15 '08, 02:54 PM. Reason: Please use the [CODE] tags provided.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  1 is a number, but '1' is a character. Since you are reading into a character, you should compare with characters.

                  Comment

                  Working...