سؤال

I've been playing around with the code below just trying to get it to compile with the Borland compiler (It's the compiler I have to use - I'd much prefer to use GCC on Linux but here we are) but having no luck - can anyone see any obvious flaws?

Code

#include<stdio.h>
#define SIZE 10
float money() {
  float wages[SIZE], rise, total;  // where i is initaliser
  float percent = 0.2;
  int i = 0, j;
  for(i = 0; i<SIZE; i++) { 
    wages[i] = j; // Initializing each element seperately 
  }
  printf("Please enter the wages for each staff member\n");
  scanf("%d*%c", &wages[i]);
  rise = wages[i] * percent;
  total = rise + wages[i];
  printf("The new wage is $%f and appears in the %d array slot\n", total, j);
  return(0);
}

int main() {
  money();
  return(0);
}

Compiler output

Warning W8004 payrise.c 8: 'i' is assigned a value that is never used in function money

Warning W8065 payrise.c 23: Call to function 'money' with no prototype in function main

هل كانت مفيدة؟

المحلول

These are both warnings; in this case the code referred to by the warnings is correct but it could have its style improved. You should be able to run the program even though it showed warnings; since there were no errors. (BTW you should have help files that came with the compiler; these will tell you what the warnings mean if you find the warning text unclear).

The first warning is because you have:

int i = 0;
for (i = 0;

i.e. the first setting of = 0 is redundant because you immediately set it to 0 again.

The second warning is because you did not provide a prototype for money. You should declare the function as:

float money(void)

This makes it be a prototype, which has the effect that the compiler will flag an error if you later try to pass an argument to the function.

However, your code has other bugs which the compiler did not diagnose (or if it did, you didn't let us know).

Firstly you use j without initializing it. You should give it a value before using it.

Secondly, scanf("%d*%c", &wages[i]); is wrong. The format specifier for float is "%f", not "%d*%c". Also , i is out of range at this point (did you mean to have this code inside the loop?)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top