473,416 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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.
Feb 12 '08 #1
8 3293
Meetee
931 Expert Mod 512MB
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 Expert Mod 4TB
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
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 Expert 512MB
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
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 Expert 512MB
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
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 Expert 2GB
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.