Greetings to you friends)) Advice please - how to get integer value from the console?
I did so, but nothing good can come out -

short int n = 0;
n = (int)getchar();

thank you in advance for your reply ))

It is impossible. Some words are used to get a letter (number).
cin and get are examples.

commented: +++++++ +1

Interactively, use fgets() to get a line, then strtol() to convert it to a long.

If you're reading from structured input, use scanf().

@alexchen: C is not the same as C++. Please take that cin nonsense somewhere else.

commented: +++++ +1

If you're reading from structured input, use scanf().

yes but this function is not found in # include <stdio.h>, as i now it means that you can do without it ....is it
impossible to directly convert char to int ?

yes but this function is not found in # include <stdio.h>

Erm, yes, it is, at least in C.

is it impossible to directly convert char to int ?

No, it's perfectly possible. You did it yourself, in your first post. But the way you phrased the question made it sound as if what you really wanted was to read an integer value from the contents of a char *. That's what the strtoX functions are for.

Perhaps you could clarify what you mean by "get integer value from the console" and "convert char to int", because I think both those questions have been adequately answered already.

@Trentacle
He casting to int directly dude how he will get correct int value??


@Vedro

You can do like this, (with some Portability issues ;) )

n = getchar()-'0';

Something good will come out ;) :D

try scanf()

int    n = 0;
scanf("%d",&n);

xiilin seems to be the only intelligent person posting in this thread! Use scanf() as suggested.

Experts are toooo busy to read full thread ..

Oh i didnt knew thr is scanf function :D

He said he couldnt use it and wanted some other way ..

commented: you are right)))))))))) +1

yes)) it will serve -

n = getchar()-'0';

!
thank you, Shankye ))

Thank you :)

If problem solved mark this thread solved ..

Happy programming :)

Reading from right to left, cast each char to int then subtract the ASCII value of zero, multiplying by 10 more than the last. Can also be used with float.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int result = 0;
    int multiplier = 1;
    int end = 0;
    char buffer[512];;
    gets(buffer);
    while (buffer[end+1] != '\0')end++;
    for (; end >= 0; end--) {
        result += (buffer[end] - 48) * multiplier;
        multiplier *= 10;
    }
    printf("%d\n", result);
}

@Sean
Its C thread ..

yes, but don't send "-" to him because of -

Its C thread ..

)))) this Thread is Solved ))

Oh i didnt knew thr is scanf function :D

He said he couldnt use it and wanted some other way ..

No, he said:

but this function [ scanf() ] is not found in # include <stdio.h>

which is completely wrong. It is in stdio.h therefore available to be used.

commented: + +1

which is completely wrong

yes, it's my mistake ))but it is rather complicated function of (I think that sometimes can be more easy way)

@Sean
So sweet of you .. But this community is not being sweet to others..

I had voted down for posting FULL SOURCE CODE ..

please explain me - why the compiler ignores the condition -

while (j<(n*n))
  { 
    u = getchar() - 48; 
   [B] if (([U]u != '\n'[/U])&&(j<(n*n))&&(u != 0))[/B]
	{
	  *(*(m+i)+k)=u;
	  i = i+1;
	  j = j+1;
	  if (i==n) {i=0; k++;}
	}
  }

- If I press -

1 ENTER 2 ENTER

I'll have in console (from array) -

1
-38
2
-38

why it's so?

>why it's so?
Enter is a character too, the newline ('\n'). On your system it has a value of 10, so 10 - 48 = -38. Note that your condition fails to catch the newline because you've already changed the value by that time.

really?! but what about the order of the operators?

if ((u != '\n')&&(j<(n*n))&&(u != 0)) // [B] - first[/B]
	{
	  *(*(m+i)+k)=u; // - second
.............
for(k=0;k<=n-1;k++) {for(i=0;i<=n-1;i++)
 {printf("%d",*(*(m+i)+k));
  putchar('\n');} }

>but what about the order of the operators?
What about them?

assignment is found after the conditions

if ((u != '\n')&&(j<(n*n))&&(u != 0)) //  - first
    {
      *(*(m+i)+k)=u; // - second
............. // after all  - 
for(k=0;k<=n-1;k++) {for(i=0;i<=n-1;i++)
 {printf("%d",*(*(m+i)+k));
  putchar('\n');} }

Um, yes? I really have no idea what you're asking. Please be a little less vague.

You said -

our condition fails to catch the newline because you've already changed the value by that time.

but

already changed

only variable u -

while (j<(n*n))
  { 
    [B]u = getchar() - 48; [/B]
    if ((u != '\n')&&(j<(n*n))&&(u != 0))
	{

but the value of the array is changed only after verification -

while (j<(n*n))
  { 
    u = getchar() - 48; 
    if ((u != '\n')&&(j<(n*n))&&(u != 0))
	{
	 [B] *(*(m+i)+k)=u;[/B]

maybe I don't understand something ...

>u = getchar() - 48;
If getchar returns '\n', you still subtract 48

>if ((u != '\n')&&(j<(n*n))&&(u != 0))
The only way u will ever match '\n' is if you type ':' (with a value of 58 in ASCII/Unicode). So even if you type a newline, this condition will never catch it properly. What you need to do is validate that the character is a digit before subtracting 48:

u = getchar();

if (u >= '0' && u <= '9')
    u -= '0';

/* Now the test for '\n' will work */
if (u != '\n' && j < n * n && u != 0)

However, you also need to consider other non-digits, but that's extraneous to the current problem of recognizing a newline character.

>u = getchar() - 48;

Narue, I totally forgot ...... and do not see that I write -48
sorry for my stupidity ....
thank you very much, Narue! ))))
finally I was able to get the integers from the console))))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.