I have this code in here:

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

void array_input(int *a)
{
	int i=0,n,p;
	printf("How many items you wanna input? : ");
	scanf("%d", &p);
	for(n = 0; n <= 99; n++)
	if(*(a+n) != 0)
		i++;
	
	for(i; i<(i+p); i++)
	{
		printf("Enter %d number:",i);
		scanf("%d", &(*(a+i)));
	}
}

int main(void)
{
	int n,t=1;
	int arrays[99] = {30};

	while(t)
	{
		n = getch();
		switch(n)
		{
			case '1':
				printf("Imput numbers into array!\n");
				array_input(arrays);
				break;
			case '2':
				printf("Excute the array into file!\n");
				break;
			case '3':
				printf("Exit");
				//t=0;
				break;
			default:
				printf("Wrong choice");
				break;
		}
	}
}

Actually when I run it everything is going by plan until i get here:

for(i; i<(i+p); i++)
	{
		printf("Enter %d number:",i);
		scanf("%d", &(*(a+i)));
	}

This for is running beyond all limits. If i have 1 number in array which is 30 and i wanna input 2 new numbers it the (i+p) should be 1+2 = 3 okay, then the restriction become 3 its go one and not stopping?

Can some one helps me on this one ?

You have i < (i + p)...unless p is negative(or becomes negative) you'll never exit you for loop.

Edited 6 Years Ago by gerard4143: n/a

Oopppssss ..

See in ur

for(i; i<(i+p); i++)
      {
          printf("Enter %d number:",i);
          scanf("%d", &(*(a+i)));
      }

i <(i+p)
when i increaments it increaments on either side..
Condition cant fail unless p=-i ..

Soluction is simple use another variable to store count of elements

int temp = i;
      for(i; i<(temp+p); i++)
      {
      printf("Enter %d number:",i);
      scanf("%d", &(*(a+i)));
      }

and btw try to make program more user friendlier ..

and btw try to make program more user friendlier ..

What do you mean by that? Whats wrong ?

i mean give options to user dude lk

printf("Enter your choice from following list");
printf("1 -> Add elements \n 2 -> smthing smthing etc");

Ah this was for me, because this is my training for me, nothing more!

This article has been dead for over six months. Start a new discussion instead.