Very simply, I want to compare the value of a user entered data to a given value, and have that set a value of another variable. In theory, something like this.... -
char x;
-
int y;
-
cin>>x;
-
if(x="a"){y=1;}
-
But that doesn't work. I'm pretty sure it's because C++ can't tell what you have in a char variable, it can just spit it back at you. What can i do to make my concept work.
6 1596
Very simply, I want to compare the value of a user entered data to a given value, and have that set a value of another variable. In theory, something like this.... -
char x;
-
int y;
-
cin>>x;
-
if(x="a"){y=1;}
-
But that doesn't work. I'm pretty sure it's because C++ can't tell what you have in a char variable, it can just spit it back at you. What can i do to make my concept work.
What can you do?
Only thing you need to do is to replace " with '...
Savage
Hahah, ::bows head in shame::
Thank you for the quick response.
Banfa 9,065
Expert Mod 8TB
Only thing you need to do is to replace " with '...
and replace = with ==
and replace = with ==
Didn't saw that..
Savage
Very simply, I want to compare the value of a user entered data to a given value, and have that set a value of another variable. In theory, something like this.... -
char x;
-
int y;
-
cin>>x;
-
if(x="a"){y=1;}
-
But that doesn't work. I'm pretty sure it's because C++ can't tell what you have in a char variable, it can just spit it back at you. What can i do to make my concept work.
Chars are not strings and leaving the value of a local variable undefined in one
branch of the control flow is a dangerous thing to do; do this instead: -
char x;
-
int y;
-
cin>>x;
-
y= (x == 'a');
-
Note the single quotes and also note that (local variable?) y equals either 1 or 0
according to the value of x.
kind regards,
Jos
Chars are not strings and leaving the value of a local variable undefined in one
branch of the control flow is a dangerous thing to do; do this instead: -
char x;
-
int y;
-
cin>>x;
-
y= (x == 'a');
-
Note the single quotes and also note that (local variable?) y equals either 1 or 0
according to the value of x.
kind regards,
Jos
I don't know if your method will work for what I am trying to do, but I may be wrong- so please clarify.
For my example, i am trying to make a loop based on user input, but the user may enter a letter or a number, so i was doing something like this. -
int loopPicker;
-
int x;
-
char y;
-
int z;
-
-
cout<<"type 1 for number, 2 for letter";
-
cin>>loopPicker;
-
-
while(loopPicker=1)
-
{
-
cout<<"enter a number:";
-
cin>>x;
-
-
cout<<"type 1 for number, 2 for letter";
-
cin>>loopPicker;
-
}
-
-
while(loopPicker=2)
-
{
-
cout<<"enter a letter:";
-
cin>>y;
-
-
if(y=='a'){z=10)
-
if(y=='b'){z=11)
-
if(y=='c'){z=12)
-
.
-
.
-
.
-
//and so on.
-
-
cout<<"type 1 for number, 2 for letter";
-
cin>>loopPicker;
-
}
-
but 2 probs..
1)i need an -
if(y!=='a'||y!=='b'.......){cout<<"bad input";}
not working.
2)loopPicker wont swtch to second loop, stays on loop 1 regardless of if you type "2" for a loop choice.
please help, you guys rock
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jonathan |
last post by:
I am looking for a simple way to check if a database table exists. I
keep getting advice to use "Try.. Catch" and other error handling
methods, but I obviously don't want to have to display an...
|
by: Patrick Gunia |
last post by:
Hi,
i´m trying to build a xml - parser, which should simply list all used
tokens an dattributes including their values. So far, so good, this
works, but now i try to check for illegal phrases in...
|
by: CR |
last post by:
having a problem with the final strcpy of filename from *argv into a
structure containing an array with 20 elements called fname
It seems that strcpy & strncpy aren't stopping after null is found...
|
by: Peter Dunker |
last post by:
Hello,
I think I have a newbee question.
If I create a char pointer at the beginning of function and use it only
in a special case:
char *name;
if (a = 1)
|
by: Al |
last post by:
Hello,
Sorry for the silly question: How do I insert Check Mark into RichTextBox? I
can’t find the ascii code for it.
Thanks
Alex
|
by: kathy |
last post by:
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...
what is the easy way for only interger?
|
by: chutsu |
last post by:
hi
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array then how can I check it with a if statement.
if...
|
by: xiaolim |
last post by:
hi, sorry to disturb again, currently i have an assignment on stacks and as show in the the title, i need to use stacks to determine palindrome.
i've done part of my code, here it is:
//...
|
by: lovecreatesbea... |
last post by:
Do you check all error conditions for all library calls in you code?
Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |