Click here to Skip to main content
12,255,421 members (31,133 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C
I have to take a input either through C or C++ program in which I don't know the number of inputs given by user. All the numbers are number, but I only know that to read 'n' number of characters we run a loop from i to n and read them in an array of 'n' length.
for(i=0;l<n;i++)>
{
   scanf("%d",&arr[i]);
}
where n is already known.
What if I don't know 'n' value.
Posted 29-Sep-12 20:23pm
Edited 29-Sep-12 20:27pm
v2
Comments
enhzflep 30-Sep-12 2:36am
   
You could also look into c++ - I.e std::vector (can use like an array of unknown no of elements)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Like Sandeep Mewara said in 99% of the cases n is unknown when you write your program. You usually get n from somewhere. In your case you should run an infinite loop (for example with for (;;)) and you should break out from it (with the break keyword) if the user enters a special value (like -1 or zero). You should also check if the return value of scanf() is one or something else, check its documentation about its return value. Without error checking you might end up inputting garbage when scanf() fails. In your infinite loop when a number is entered you either process it immediately as it comes in, or if your algorithm needs the total number of input items (for example because you want to sort the numbers) then you have to put each item into a dynamically resizing array (like std::vector) and after breaking out from the infinite loop you have n which is the size of your array. The name std::vector might be confusing but it is an array you can resize (the normal C array provided by the language are fixed in size and their length can not be zero).
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What if I don't know 'n' value
Well, it's quite common to not know the number of inputs earlier.

Mostly, the first question would be, 'how many inputs you want to enter?' Value of this will be taken as 'n' value. Once this is defined, rest of the code is as above.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Using C++ you can use "new" to allocate new positions in the destination array and "delete" to deallocate them, use a while(input!=x) loop where x would be a specific input code to break the loop.

Scan for text not numbers, before processing the input check it using strcmp and see if it is 'EXIT' or any other keyword that you would choose (used as the condition in the loop), if it is not, try to convert the input to number (atoi will help)...

Good luck!
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

#include<stdio.h>
#include<conio.h>
 

void main(void)
{
    int f=0,arr[50];
    clrscr();
    printf("\n Enter pair of numbers (Enter a non integral value to terminate input):\n");
    while(scanf("%d",&arr[f]))
    {
        f++;
    }
    printf("\n The number of inputs are: %d",f);
    getch();
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100