0

I'm currently learning C and wanted to write a program that takes a number ganzeZahl to determine array length.

Then you have to input the numbers being stored in that array of size n and after that it's supposed to do a selection sort (which I cut out here, because my program doesn't even reach to that part).

I can't get past the while loop whenever I try to run it. It compiles fine. It never reaches the printf("!!!--------!!!"); //this part isn't reached for some reason? test5.

#include<stdio.h>

int main() {

  int ganzeZahl;
  scanf("%d", &ganzeZahl);
  //printf("ganze Zahl ist: %d\n", ganzeZahl); //test

  int array[ganzeZahl];
  int i = 0;

  for(int z = 0; z < ganzeZahl; z++) {
    printf("%d\n", array[z]);
  }

  while(i<ganzeZahl) {
    //printf("!!!hier!!!\n"); //test2

    scanf("%d", &array[i]);
    //printf("zahl gescannt!\n"); //test 3
    i++;
    //printf("i erhöht!\n"); //test 4
  }

  printf("!!!--------!!!"); //this part isn't reached for some reason? test5
  //selection sort here
//[...]

  return 0;
}
IrAM
  • 1,720
  • 5
  • 18
laureen
  • 11
  • 2
  • 1
    Can't reproduce - your program works fine when I test it with a `ganzeZahl` input of 3, then inputting 11 22 33. What did you input? And how? – Adrian Mole Jan 03 '21 at 11:55
  • 1
    Just a thought, though - maybe add a newline to your 'missing' `printf` call: `printf("!!!--------!!!\n");` - just to make sure the output buffer is being flushed. – Adrian Mole Jan 03 '21 at 11:56
  • 1
    Move the loop printing the results from *before* data entry to *after*. – Weather Vane Jan 03 '21 at 11:57
  • 1
    You shouldn't print the content of `array` before having filled it with `scanf` (see [here](https://stackoverflow.com/questions/32769922/what-happens-in-c-when-you-print-a-declared-but-unassigned-variable)), but the code seems to work in general (see a quick [ideone](https://ideone.com/6JJhWw)) – xanatos Jan 03 '21 at 12:00

2 Answers2

2

Your program does execute correctly, and eventually reaches the last printf call.

When, it enters the whileloop, it keeps on calling scanf, what causes it to stop and wait until you enter a value every iteration. If you provide ganzeZahl inputs (enter a number and press 'enter'), it will complete the loop and proceed. I guess that if you add a printf before scanf within the loop, it should be more intuitive.

fjm
  • 46
  • 5
  • Oh jeez, than it must be something in the later part that interrupts. Thanks so much! I guess I should have tested that first. Anyways, thanks for taking the time – laureen Jan 03 '21 at 12:17
1
for(int z = 0; z < ganzeZahl; z++){
printf("%d\n", array[z]);

The array is not initialized and so you can't print the array yet.

Actually you messed up with the order of code.The while loop should come first and then the for loop. I have corrected your code below. Happy coding!

#include<stdio.h>

int main() {

  int ganzeZahl;
  scanf("%d", &ganzeZahl);
  //printf("ganze Zahl ist: %d\n", ganzeZahl); //test

  int array[ganzeZahl];
  int i = 0;

 while(i<ganzeZahl) {
    //printf("!!!hier!!!\n"); //test2

    scanf("%d", &array[i]);
    //printf("zahl gescannt!\n"); //test 3
    i++;
    //printf("i erhöht!\n"); //test 4
  }
  
  for(int z = 0; z < ganzeZahl; z++) {
    printf("%d\n", array[z]);
  }


  printf("!!!--------!!!"); //this part isn't reached for some reason? test5
  //selection sort here
//[...]

  return 0;
}
jiVatx19
  • 21
  • 7