所以,我不确定如何开始,所以我将从一些代码开始:
int main()
{
system("cls");
printf( "1. D4\n" );
printf( "2. D6\n" );
printf( "3. D8\n" );
printf( "4. Exit\n" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1:
roll_d4();
break;首先,我有这个,当我选择一个选项时,它会让我转到我想要的功能。因此,当我到达D4函数时,我有以下代码:
void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
case 5:
exit(0);
d4_number();
}现在,每次我尝试运行它,它都不像d4_number();,它说它是一个隐式声明。我只是想让你选择一个选项,并让它转到代码的下一部分,在本例中,这个函数实际上是一个掷骰子的函数,而不是像这样的菜单。但是这样做是行不通的,我不知所措。
编辑:代码添加。下面是完整的程序:
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
char response;
int sum = 0;
time_t t;
int result;
int die_d4_1 = 0;
int die_d4_2 = 0;
int die_d4_3 = 0;
int die_d4_4 = 0;
int die_d4_5 = 0;
int input;
void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
case 5:
exit(0);
d4_number(0);
}
}
void roll_d6()
{
printf( "How many D6 do you want to roll?" );
}
void roll_d8()
{
printf( "How many D8 do you want to roll?" );
}
void d4_number(){
printf("How many d4 do you want to roll?");
}
int main()
{
system("cls");
printf( "1. D4\n" );
printf( "2. D6\n" );
printf( "3. D8\n" );
printf( "4. Exit\n" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1: /* Note the colon, not a semicolon */
roll_d4();
break;
case 2:
roll_d6();
break;
case 3:
roll_d8();
break;
case 4:
printf( "Thanks for playing!\n" );
break;
default:
printf( "Bad input, quitting!\n" );
break;
}
getchar();
}它显然是非常不完整的,但我只是想在添加更多之前解决这个问题。
发布于 2017-02-16 08:49:16
好吧,我觉得自己像个笨蛋。我所要做的就是移动声明:
void d4_number(){
printf("How many d4 do you want to roll?");
}上图
void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
case 5:
d4_number(0);
}所以它是在调用之前声明的…我对此感到非常愚蠢,但感谢每个人的快速响应!
发布于 2017-02-16 08:32:42
在调用之前,您需要给d4_number()一个原型,以便编译器知道它接受什么参数以及返回什么。否则,它仍然链接(由于历史原因),并假定它返回一个int。
发布于 2017-02-16 08:39:45
如果你仔细看看这段代码
switch (input){
case 5:
exit(0);
d4_number(0);
}您可以注意到,在d4_number函数之前,有一个exit函数被调用。在这种情况下,d4_number函数永远不会有机会执行,因为整个程序会停止并退出,返回代码为0。
另一个问题是d4_number函数定义不接受任何参数,但是在语句d4_number(0)中传递了一个额外的参数。
https://stackoverflow.com/questions/42262677
复制相似问题