Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
for(i=0;l<n;i++)>
{
   scanf("%d",&arr[i]);
}

where n is already known.
What if I don't know 'n' value.
Posted
Updated 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)

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).
 
Share this answer
 
v2
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.
 
Share this answer
 
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!
 
Share this answer
 
v2
XML
#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();
}
 
Share this answer
 
Comments
Member 14527742 11-Jul-19 9:14am    
Awesome thinking sir

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900