473,473 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

4 New Member
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.
Feb 12 '08 #1
8 3303
Meetee
931 Recognized Expert Moderator Contributor
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
Feb 12 '08 #2
sicarie
4,677 Recognized Expert Moderator Specialist
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.
Feb 12 '08 #3
Harishapur
4 New Member
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." _
Feb 13 '08 #4
oler1s
671 Recognized Expert Contributor
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?
Feb 13 '08 #5
Harishapur
4 New Member
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.
Feb 13 '08 #6
Laharl
849 Recognized Expert Contributor
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.
Feb 14 '08 #7
Harishapur
4 New Member
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.   char favorite;
  8.  
  9.   cout << "Enter your favorite: a,b,c,d,e,1,2,3,4,5.";
  10.   cin >> favorite;
  11.  
  12.   if (favorite == 1)
  13.   {
  14.     cout << "hooray";
  15.   }
  16.   return 0;
  17. }
Feb 15 '08 #8
Ganon11
3,652 Recognized Expert Specialist
1 is a number, but '1' is a character. Since you are reading into a character, you should compare with characters.
Feb 15 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
7
by: Richard Hayden | last post by:
Hi, I've just upgraded my gcc and I'm currently trying to compile some code for my own operating system kernel, but I am getting an error of "Undefined reference to `memcpy`" when I try to link...
6
by: Julie | last post by:
I have a situation where I have multiple objects that aren't related in any way (no base class), but all have a couple of common methods/properties. I'm looking for a clean way to call a...
11
by: Demorsy | last post by:
I want to convert a string (or object) to a primitive type. But I don't know the type to convert to at run time. For example l have variable (lets say its an int): int unknownType = 0; And a...
0
by: DavidPfahler | last post by:
I am using the xml documentation(/doc) for c++ and have noticed that the method signatures are not correctly capturing the parameters. I have defined an enumeration in my project as well as other...
9
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the...
3
by: Valli | last post by:
Hi, I want to add an image to a html type input button. How can I do this? I have used background image style . But its not working. My target is to show an image in the button can anyone...
1
by: GarySharma | last post by:
Program should accept user input and do appropriate actions. But program hangs after creating 60 threads, that is after 60 transaction even if user submits input it doesn't take. I read that...
2
by: unauthorized | last post by:
The short story: I need to be able to cast a function pointer for any function and class to an intermediate type, so I could call it from any point of my program using the "__asm call" instruction....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.