2

I have a restriction on the maximum size of an array (N), and I want to ask the user to input an array of numbers that can be less than or equal to N. How do I find how many values the user has inputted?

This is what I have, and it's not working.

(basically, i'm telling the program to stop counting "n" once the user hits enter) (and ofcourse i initialized n=0)

for(i=0;i<(N-1);i++)
{

    scanf("%d",&a[i]);
    n++;

    if(a[i]=='/n'){break;}
}

any help is appreciated! thanks!

Dana
  • 23
  • 3
  • 1
    The value of `i` after the loop will hold `the number of values inputed - 1` so what you need is the value of `i+1`, once the loop is finished – nem035 Jan 14 '15 at 17:14
  • 1
    Also, saying *it's not working* doesn't mean much. What **exactly** isn't working? What is happening versus what you want to happen? – nem035 Jan 14 '15 at 17:15
  • first off, sorry i made a mistake, i wrote 0 instead of '/n'.. The code itself is working, but when i input, lets say, 5 numbers, the program is still waiting for me to enter more and doesn't end the program. I want it to stop the program no matter how many numbers i input (as long as its less than N) – Dana Jan 14 '15 at 17:18
  • Ok so how will the computer know **when** to stop? You have to tell it somehow. If you want the computer to stop `no matter how many numbers` you input, it still needs some sort of a stoping signal. And i can see that you are telling the computer to stop (break out of the loop) when you enter a newline character or (N-1) is reached. So I still don't see what exactly is your issue. Another thing, if you want your loop to run up to a value *less than or equal to N*, change your loop condition from `i < (N-1)` to `i < N` – nem035 Jan 14 '15 at 17:25
  • 1
    You're reading an integer and then checking to see whether the user entered "10" (ASCII value for newline) rather than whether they actually pressed enter... or would be if you'd used `'\n'` instead of `'/n'`. Probably not what you want. – Dmitri Jan 14 '15 at 17:35
  • @Dana can you use `fgetc()` and `ungetc()`? – Iharob Al Asimi Jan 14 '15 at 17:54
  • 1
    If I understand your question you want to fill your array with the integers which are entered in a line i.e until a newline char is enountered is it so? – Gopi Jan 14 '15 at 18:00
  • Note that `'/n'` is a multicharacter constant, not a newline, and it has an implementation-defined value. Generally, `'\n'` (newline) is just a way of writing 10; your code is testing whether the user entered 10, which is a curious 'end of list' value. You should be checking the return value from `scanf()`. If you care about line boundaries, you can't use `scanf()` to read numbers, because `scanf()` doesn't care about line boundaries. If you need to read and parse lines, use `fgets()` (or `getline()`) to read the line and [`sscanf()` in a loop](http://stackoverflow.com/q/3975236/) to parse it. – Jonathan Leffler Jan 14 '15 at 19:16

3 Answers3

1

This isn't working because scanf with the "%d" specifier, will skip the \n, you can consume all white space characters and search for '\n' with fgetc(), the last white space character which is not '\n' could be returned to the stream using ungetc() so this program probably will do what you need

#include <stdio.h>
#include <ctype.h>

int main()
{
    int a[100];
    int i;
    int result;

    result = 1;
    i      = 0;
    while ((i < 100) && (result == 1))
    {
        int chr;

        /* 
         * consume all whitespace characters left by previous scanf, 
         * stop if one of them is '\n' 
         */
        while (isspace((chr = fgetc(stdin))) && (chr != '\n'));
        /* found the '\n', set the flag to exit the loop */
        if (chr == '\n')
            result = -1;
        else
        {
            /* not interesting put back this character for scanf to read it */
            ungetc(chr, stdin);
            /* save the result of scanf, that way you can validate input */
            result = scanf("%d", &a[i]);
            if (result == 1)
                i++;
        }
    }
    printf("read %d numbers\n", i);

    /* print the carachters, this will print in reverse obviously */
    while (--i >= 0)
        printf("%d\n", a[i]);


    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0
#include <stdio.h>
#include <ctype.h>

#define N 100

int main(void){
    int v, a[N];
    int i, n=0, stop = 0;
    int ch, res;

    while(!stop && EOF != (res = scanf("%d", &v))){
        if(res == 1){
            a[n++] = v;
            if(n == N)
                break;
            while(isspace(ch = getchar())){
                if(ch == '\n'){
                    stop = 1;
                    break;
                }
            }
            ungetc(ch, stdin);
        } else {
            printf("invalid input!\n");
            while(getchar() != '\n');
        }
    }
    for(i = 0; i < n; ++i)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
-1

You could ask the user to enter the number of items that he's willing to enter. Let's say that the max number is N and that the number of items the user wants is n.

puts("ENTER NUMBER OF ITEMS");
scanf("%d", &n);

n = min(n, N);
n = max(n, 0);

for(int i = 0; i < n; i++)
{   scanf("%d", &a[i]);
}

If the user keeps typing numbers and then hits enter to stop, you can read the input line by line and check that the user has typed something or break out of the loop elsewise.

int i = 0;
for(; i < N; i++)
{   char s[100];
    gets(s);

    if(strcmp(s, "") == 0) break;
    a[i] = atoi(s);
}

The number of items read is equal to i. This is less safe and you've got to do some error checking if you are willing to use this solution.

mrk
  • 3,061
  • 1
  • 29
  • 34