초보자 C는 여기에 있습니다. 프로그램을 실행할 때마다 변수에 대해 다른 값을 갖기 위해 노력하고 있습니다. 이 코드가 있습니다C - 난수 생성
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ADD 1
#define SUB 2
#define MUL 3
#define DIV 4
int main(void)
{
double out;
int choice;
float x, y;
printf("Welcome to our first calculator program!\n");
printf("===========================\n");
printf("Please insert 2 numbers:\n");
scanf("%f%f", &x, &y);
choice = 1 + srand(time(NULL)) % 4;
if(choice == ADD)
out = x + y;
else if(choice == SUB)
out = x - y;
else if(choice == MUL)
out = x * y;
else
{
if(y != 0)
out = x/y;
else
{
out = 0;
fprintf(stderr, "error");
return 1;
}
}
printf("The outcome is: %0.2lf\n", out);
return 0;
}
를하지만 항상 선택의 날 (4)를 제공합니다. 나는 이유를 모르겠다. ...
나를 도와 줄 수 있니? 감사.
* 확실히 * 그 코드 컴파일 있습니까? 'srand' (또는 "seed random")는 * void * 리턴 타입을 가져야하고,'rand'는 실제 * 난수를 얻기 위해 사용되어야합니다. – user2864740
@ user2864740'gcc 버전 4.7.2 (Debian 4.7.2-5)는'오류가 발생합니다 : void 값이 무시되어서는 안됩니다. ' –
@ user2864740 Visual Studio 2010에서도 다음과 같은 오류가 발생합니다. '오류 C2296 :'% ': 불량, 왼쪽 피연산자에'void '유형이 있습니다. –