If the test portion of the while(test in here) doesn't resolve to 1 (true), then the inner part of the while() loop will not be entered by the program.
So, is t[jcs] > 100, right at the top of the first loop?
And somewhere in the while() loop, you want to increment a variable (i is the standard syntax, but any integer variable name will work:
i=0;
t[i] = 99;
while(t[i]<100) {
printf("i = %d", i);
++i;
t[i] = getchar();
}
Without the ++i, the program would always put one value, right on top of the other one, in t[i]. Be careful not to confuse the value of t[i] with the value of i.
Welcome to the forum, TomaCukor! ;)
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
You realize that getchar is reading one character and returning the code value of that character, right? If you type "100", the values you get are likely to be 49, 48, and 48. Is this intended? Because it raises red flags when I read it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Why not have the user input an integer, instead of a char, (as mentioned above), and use a do while loop, instead of a while loop? That will put the test for what the user entered, AFTER they have entered the loop, instead of right at the top of it.
(This can be done with a while loop, but the logic has to be changed. It's more intuitive to use a do while loop, for clarity.)
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
There's no way to use a function that only takes one key input, and use it for to input an integer. No, since an integer might contain several digits.
You're asking if there is a trash can, that can haul the garbage truck. Clearly not.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
>is there any getchar equivalent for ints ?
If you mean something super simple like int x = get_int(stdin); then no. A usable function along those lines can easily be written though:
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
int get_int(FILE *in, unsigned *n_digits)
{
const unsigned base = 10;
const unsigned safe = UINT_MAX / base;
const unsigned lsd = UINT_MAX % base;
unsigned overflow = 0;
unsigned sign = 0;
unsigned temp = 0;
unsigned n = 0;
int value = 0;
int ch;
if ((ch = getc(in)) == '-' || ch == '+') {
sign = ch == '-';
ch = getc(in);
}
for (; isdigit(ch); ch = getc(in), n++) {
unsigned digit = ch - '0';
overflow = temp > safe || (temp == safe && digit > lsd);
if (overflow)
break;
temp = temp * base + digit;
}
ungetc(ch, in);
if (n > 0) {
if (overflow || (sign && temp > -INT_MIN) || (!sign && temp > INT_MAX))
errno = ERANGE;
value = sign ? -(int)temp : (int)temp;
}
if (n_digits != NULL)
*n_digits = n;
return value;
}
int main(void)
{
unsigned n = 0;
int x = get_int(stdin, &n);
int ch;
if (errno == ERANGE)
fprintf(stderr, "Error: Number exceeds [%d, %d]\n", INT_MIN, INT_MAX);
if (n == 0)
fprintf(stderr, "Error: No integer detected\n");
else
printf("Detected %d with %u digits\n", x, n);
fputs("Characters remaining: '", stdout);
while ((ch = getchar()) != '\n')
putchar(ch);
puts("'");
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
so now im trying to read integers from input untill input is eof or blank line(line was left empty and enter was hit) ... ive found some codes(getline ) but it only applies for char type ! and i dont want to go through the whole conversion thing(but if theres a simple one ill consider it) !
Why do you want to do it the hard way and not the easy way?
Read the line.
Test if it's blank.
If not, test if it's digits (this is simple).
If so, convert to binary (also simple, one statement).
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Get the line then start testing. Don't get it in the IF statement.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Start with (in your case) the hardest test first.
if line is blank
exit loop
else
if line is #
convert it
else
bad line
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Other than using the non-Standard scanf_s() I don't really see a problem. Try displaying t[jcs] after the input to make sure it was input correctly.
More info is probably needed.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944