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

Edited 6 Years Ago by ce0: n/a

but then it dont work anymore

WTF do you mean by "it don't work anymore". Are we to read your mind for sufficient details? I suspect you're encountering a very simple and common problem with scanf not playing nice, but I can't say for sure because in that relatively large post, you completely failed to say how "it don't work anymore". :icon_rolleyes:

ahh yes sorry i guess thats important too and i really tried to give all the infos

the programm compiles and when i run it the output/input looks like this

[
how big shall the first array be?
2
please enter the first number
1
please enter the first number //i'll change that in time
2
do you want to put another number in the array? press y=1 n=0
/*acctually since i changed the programm to char it should read y or n
so i preass y and enter but insteed of asking what i would put in next it writes as follow*/
41297924129792drücken sie eine beliebige taste...
]

what do you mean scanf not playing nicely isnt it scanf("%c",&c);?

thanks for your answer

Towards the end of your while loop(lines 83-84) you ask the user to chose between y/n but the variable that you have used for taking the input is an integer.
So what happens is when the user enters y, scanf sees that it has received an char but the variable it is going to use to place the input value is an integer so it does not do anything. The value of the variable e is the same value as before ie 1.
Then we come to line 70. we have a char in the input stream, but again scanf is expecting an int so we get a garbage value here. This procedure repeats itself infinitely.

In debug mode you can see what value is copied into char c, maybe you must clean the buffer before you take the character with scanf.

Some advice from me:
1-Use meaningful variable names, such as state, condition, myArray. It really helps you and us figure out what the problem is.
2-Learn and use coding standarts.
3-Learn debugging, it dramatically reduces the problems you encounter.
4-When asking a question you must use a good algorithm to tell the problem. Human can understand you better than computers but you still need to tell the problem in a meaningful way. :)

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