473,395 Members | 1,938 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,395 software developers and data experts.

How to check conent in char.

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....

Expand|Select|Wrap|Line Numbers
  1. char x;
  2. int y;
  3. cin>>x;
  4. if(x="a"){y=1;}
  5.  
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.
Sep 26 '07 #1
6 1596
Savage
1,764 Expert 1GB
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....

Expand|Select|Wrap|Line Numbers
  1. char x;
  2. int y;
  3. cin>>x;
  4. if(x="a"){y=1;}
  5.  
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
Sep 26 '07 #2
Hahah, ::bows head in shame::

Thank you for the quick response.
Sep 26 '07 #3
Banfa
9,065 Expert Mod 8TB
Only thing you need to do is to replace " with '...
and replace = with ==
Sep 26 '07 #4
Savage
1,764 Expert 1GB
and replace = with ==

Didn't saw that..

Savage
Sep 26 '07 #5
JosAH
11,448 Expert 8TB
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....

Expand|Select|Wrap|Line Numbers
  1. char x;
  2. int y;
  3. cin>>x;
  4. if(x="a"){y=1;}
  5.  
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:

Expand|Select|Wrap|Line Numbers
  1. char x;
  2. int y;
  3. cin>>x;
  4. y= (x == 'a');
  5.  
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
Sep 26 '07 #6
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:

Expand|Select|Wrap|Line Numbers
  1. char x;
  2. int y;
  3. cin>>x;
  4. y= (x == 'a');
  5.  
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.

Expand|Select|Wrap|Line Numbers
  1. int loopPicker;
  2. int x;
  3. char y;
  4. int z;
  5.  
  6. cout<<"type 1 for number, 2 for letter";
  7. cin>>loopPicker;
  8.  
  9. while(loopPicker=1)
  10. {
  11.     cout<<"enter a number:";
  12.     cin>>x;
  13.  
  14. cout<<"type 1 for number, 2 for letter";
  15. cin>>loopPicker;
  16. }
  17.  
  18. while(loopPicker=2)
  19. {
  20.     cout<<"enter a letter:";
  21.     cin>>y;
  22.  
  23. if(y=='a'){z=10)
  24. if(y=='b'){z=11)
  25. if(y=='c'){z=12)
  26.           .
  27.           .
  28.           . 
  29.     //and so on.
  30.  
  31. cout<<"type 1 for number, 2 for letter";
  32. cin>>loopPicker;
  33. }
  34.  
but 2 probs..
1)i need an
Expand|Select|Wrap|Line Numbers
  1. 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
Sep 26 '07 #7

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

Similar topics

2
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...
1
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...
2
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...
6
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)
3
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
3
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?
9
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...
7
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: //...
55
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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...
1
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...
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
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
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
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,...

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.