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.
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
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.
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." _
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?
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.
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.
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 - #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;
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
//****************************
//****************************...
|
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...
|
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...
|
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.
|
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...
| |
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |