If character then redo the function

前端 未结 2 959
醉话见心
醉话见心 2021-01-28 13:30

In C code. This is the part I want to work on below. I want to be able to do... if a character value then say \"not a number\", however, because it is an array and it increments

2条回答
  •  有刺的猬
    2021-01-28 14:37

    If you want to repeat something, you need a loop. If you don't know how many times you'll want to loop in advance, it's probably going to be a while loop. The following structure will achieve your goal cleanly:

    while (1) {
       printf("Enter salary for Employee #%d: ", i + 1);
       scanf("%f", &sal[i]);        
       if (...valid...)
          break;
    
       printf("Not a Number. Try again.\n");
    }
    

    The value returned by scanf will help you determine if the input was valid. I will leave consulting the documentation and finishing that part of your homework to you.

提交回复
热议问题