hello all
i have a problem with a "while condition" useing c.
my assignment was to create a dynamical array, using malloc.
i did that and all worked as it should.
the only thing i dont like,is when it asks you if you want to continue, putting in integer.
you have to write an (int) 1 and i wanted a (char) 'y' insteed but then it dont work anymore :(
the while condition is like this
int e=0;
if(e==1)
{.....;}
while(e==1)
{....;}
i tried alternate the while condition like this
char c;
if(c=='y')
{....;}
while(c=='y')
{....;}
of course i also alternated scanf inputs corectly to scanf("%c",&c)
if you need to look at the code here it is but i guess its more of a common problem (line 43 would be the while loop condition c the char i want there and e the one that is there atm)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int *pa;
//pointer a (becomes the array which is given out
int *ps;
//pointer b (is the storing array
int n=0,o=0;
//n:how big the array is at the beginning, o:growing operator
int i=0,i2=0,i3=0,i5=0;
//counting stuff
char c=0;
//char i would like to use in while and if condition
int e=0;
//char i use in the while and if condition
printf("how big shall the first array be?\n");
scanf("%d",&n);
pa = (int*) malloc(n*sizeof(int));
//make the array a
ps = (int*) malloc(n*sizeof(int));
//make the array b
o=n+1;
for(i=0;i<n;i++) //fill the array
{
printf("Please enter the first number\n");
scanf("%d", pa+i);
}
printf("do you want to put another number into the array? press y=1 or n=0 \n");
scanf("%d",&e);
if(e==1) //if condition when you want to add a number
{
for(i2=0;i2<n;i2++)
{
ps[i2] =pa[i2];
pa[i2]=0;
}
}
while(e==1) //while as long as you want to add them
{
pa = (int*) malloc(o*sizeof(int));
for(i2=0;i2<o;i2++)
{
pa[i2]=ps[i2];
ps[i2]=0;
}
printf("please insert your int\n");
scanf("%d",&pa[o-1]);
ps = (int*) malloc(o*sizeof(int));
for(i2=0;i2<o;i2++)
{
ps[i2] =pa[i2];
pa[i2]=0;
}
printf("do you want to put another number into the array? press y or n\n");
scanf("%d",&e);
o=o+1;
}
for(i3=0;i3<(o-1);i3++) //prints your vector out
{
printf("%d",ps[i3]);
}
system("pause");
//only operateble in windows so i can use dev c++
return 0;
}
edit made the // a little bit more readable