-4

My Programm looks like this.

int main(){

   int maxnote = 0;
   int eingabewert;
   int n = 0;
   int userMarks[200];
   ind promark;

   printf("Welcome, plese enter your points, -1 to finish.\n");

   while (eingabewert != -1){
   scanf("%d", &eingabewert);

   if(eingabewert < -1){
     printf("A student can't have 0 > points.\n");
     exit(0);
    }
    userMarks[counter] = eingabewert;
    counter += 1;
   }
   printf("Please insert, the least pints needed for 6:");
   //Second Scanf doesn't work, it stays in a Loop or something like that
   scanf(" %d", &maxnote);
   for(int i = 0; userMarks[i] != -1; i++){
     userMarks[i] = berechneNote(userMarks[i], maxnote);
   }
   countMarks(userMarks);
   notenstats(userMarks);
   promark = ((suffmark/counter) * 100);
   printStatistic(maxnote, promark);
}

The first Scanf() does it job perfectly and takes the given numbers.

However the second one isn't doing that.

It stays in a Loop and I can't go forward with my code.

What should I do to fix this?

Omega09
  • 3
  • 1

2 Answers2

3

Because you are using eingabewert uninitialized in

while (eingabewert != -1){

Initialize it with

int eingabewert = 0;

And always check the result of scanf

while ((eingabewert != -1) && (scanf("%d", &eingabewert) == 1))

You are also using userMarks uninitialized in

for(int i = 0; userMarks[i] != -1; i++){

In this case (an array) initialize it using

int userMarks[200] = {0};
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Oops, you need to initialize also the array, edited. Always initialize variables and arrays before using them, only static and global variables are auto-initialized to zero, non initialized local variables contains garbage, thus, reading/writing to those variables is undefined behaviour. – David Ranieri Mar 31 '18 at 17:26
  • @Jean-FrançoisFabre Yeah :) – David Ranieri Mar 31 '18 at 17:27
  • 1
    Happened to me a lot... let's hope it's the last bug. It seems to be. – Jean-François Fabre Mar 31 '18 at 17:27
  • I initialized it, but it still doesn't work. The second scanf() doens't stop it stays active. – Omega09 Mar 31 '18 at 17:31
  • @Omega09 Then, you should show what `berechneNote(userMarks[i], maxnote);` is doing. – David Ranieri Mar 31 '18 at 17:35
  • int berechneNote(int points, int maxnote) { int note; double temponote; temponote = (1 +((5*points)/maxnote)); note = rundeabauf(temponote); return note;} – Omega09 Mar 31 '18 at 17:55
  • 1
    Using `eingabewert` in `scanf("%d", &eingabewert);` is not a problem. Using `eingabewert` in `while (eingabewert != -1){` is a problem. Failure to check the return value of ``scanf("%d", &eingabewert);` is also weak. – chux - Reinstate Monica Apr 01 '18 at 02:20
  • @Omega09 Is an infinite loop because you never set `userMarks[i + 1]` to `-1`, notice that the `i++` in the `for` loop is incrementing the value of `i` before checking `userMarks[i] != -1` – David Ranieri Apr 01 '18 at 06:36
  • The code goes through the first scanf() and the for loop but the second scanf() doesn't work.. – Omega09 Apr 01 '18 at 13:10
0

There is a space in the format specifier in the second scanf. And you should learn how to use a debugger, like gdb. It's a lot faster than posting such a long question on SO.

mattmilten
  • 6,242
  • 3
  • 35
  • 65
  • 2
    What's wrong with this space? Take a look to http://www.cplusplus.com/reference/cstdio/scanf/ _The function will read and ignore any whitespace characters encountered before the next non-whitespace character._ – David Ranieri Mar 31 '18 at 17:06