How to print a character code from char*? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm getting a character from a user input using getchar(), but instead of using cout to print the character I want to print the character code, like the one for Return, ESC, etc, so I can use in my code later to check using a if.

To do this you can cast the char to an int,
int charval = (int) mychar;

printf("%d\n", c); will show you the character code.

so I can use in my code later to check using a if.
You don't need to expicitly convert it to an int for that.
if(getchar() == char_code)
doSomething();

Cast the char as an int and print the int value.

Related

Compare char array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Hi I'm doing an assignment and in this part of it I need to check if a word I have entered into a char array is equal to a word stored in a structure array. This is what I have but it doesn't work:
if (CDdata[i].artist == search)
Can someone please help me compare the item in the structure array to the char array?
Thanks.
Assuming that CDdata[i].artist and search are char* or const char*, all you're currently doing is comparing the pointers not the values.
You need to use something like if (strcmp(CDdata[i].artist, search)) which will return 0 for equality.
strcmp is a standard function in the C standard library.
You can use the strcmp function in the c standard library.
if (strcmp(CDdata[i].artist, search) == 0)

Palindrome without using extra space [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I know various ways to check if an integer is a palindrome or not by using string conversion, stack and number breaking, but here question is
"How can we check wether an integer is a palindrome or not, without using any extra space?"
You can revert a number with some code like:
int revert(int num) {
int reverted = 0;
while (num) {
reverted = reverted*10 + num%10;
num /= 10;
}
return reverted;
}
And now you only check if
num == revert(num)
That is all. Sorry for giving the exact solution instead of just a tip, but I don't think I could have given any tip without the solution itself.

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

validating the value in a integer...? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
how do v find a value in an integer is garbage or not???
Initialize it at point of declaration, and it can never be garbage.
Pass it to strfry and compare the returned value with the original.
I guess that you mean if a string contains a valid integer. Check out scanf or the stream equivalent of c++, or Boost lexical cast

how can i store more than just one letter in a variable? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
with char i get this error:
.\main.cpp(6) : error C2015: too many characters in constant
A char only holds one character:
char bar = 'a';
If you want more, use a string constant to initialize a character array:
char foo[] = "This is my thing";
Given the file extension cpp, I am going to go out on a limb and assume you are using C++. If so, use the string class to store a string.
See Compiler Error C2015 for an explanation of the error. MSDN is a great source of knowledge and usually describes the error messages from Visual Studio (as I assume you are using) in more detail.
Use a string i.e. array of characters For example char s[] = "Hello";