473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ 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 3307
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
2454
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 wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //****************************...
7
12050
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 it using the GNU linker. I do not reference the symbol memcpy explicitly anywhere in the offending function. I have narrowed the offending code down...
6
2862
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 particular method/property for the object, w/o having to resort to a bunch of if-is-cast statements. Here is what I'd like to be able to do: object...
11
14263
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 string: string str = "123"; If I knew the type is int I could have used: unknownType = Convert.ToInt32(str); or int.Parse(str) etc.
0
821
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 classes. Anytime I use one of these non-native types in a signature it lists <unknown type> in the xml file. This scenario does not happen in all...
9
3993
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 object - a variable (B) of the type "Type" which contains the type of the object in (A) (A should be of the type B) - a method which should return the...
3
1740
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 help me? -- Regards
1
1637
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 TerminateThread has some memory problems but couldn't find alternate solutions to fix this. I highly appreciate some help. I am using VC++ and following...
2
5315
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. The long story: I have been trying to develop a data driven method to call C++ functions, that allows calling them with minimal overhead. The...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
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.