gpt4 book ai didi

c - 初始化 block 部分不允许声明

转载 作者:行者123 更新时间:2023-11-30 14:33:42 25 4
gpt4 key购买 nike

#include <string.h>
#include <conio.h>
#include <math.h>
#include "helper2.h"

char validLoop = ' ';
int choice;
int validInput = 0;

main(){

repeat:
clrscr();

printf("=======================\nMenu\n=======================\n[1] Binary to Decimal\n[2] Sorting Algorithm(Ascending Descending)\n[3] Palindrome Checker\n=======================\n");

do{
printf("Enter Your Choice: ");
scanf("%d", &choice);

if (choice == 1 || choice == 2 || choice == 3){
validInput = 1;
}

else{
printf("Invalid Input! Please Input a value given within the choices.\n");
}

} while(validInput == 0);


if (choice == 1){
BinToDec();
}

if (choice == 2){
sorting();
}

if (choice == 3){
checker();
}

printf("\nPress Y||y to repeat or any key to exit.");
validLoop = getche();

if (validLoop == 'y' || validLoop == 'Y'){
goto repeat;
}

else{
return 0;
}

}

我在 validLoop、choice 和 validInput 未初始化的地方收到错误,并显示“此处不允许声明”。它还说缺少一个声明;第 11 行。我是不是做错了什么?

编辑:抱歉,我忘记添加标题:

char checker(){
int i;
int check = 0;
char checker();
char word[50];
printf("Insert a string: ");
scanf("%s", &word);
for (i=0;i<strlen(word)/2;i++){

if (word[i] == word[strlen(word)-i-1]){
check++;
}
}


if (check == strlen(word)/2){
printf("%s is a palindrome", word);
}

else{
printf("%s is not a palindrome", word);
}

return 0;

}

int BinToDec(){
char binary[8];
int i;
int sum=0;
int value;
int length;


printf("Input binary number you want to convert to decimal: ");
scanf("%s", binary);

length = strlen(binary);


for (i=strlen(binary)-1;i>=0;i--){
value = length - (i + 1);
if ((int)binary[i]-48 == 1){
sum = sum + pow(2.0, (float)value);
}
}

printf("%d", sum);
getch();

return 0;

}




int sorting(){
int i;
int sort[10];
int min;
int temp;
int currentElement;
int compareElement;

printf("Input integers to sort: ");
for (i=0;i<10;i++){
scanf("%d", &sort[i]);

for (currentElement=0;currentElement<(sizeof(sort)/sizeof(sort[0]))-1;currentElement++){
min = currentElement;

for (compareElement=currentElement+1;compareElement<(sizeof(sort)/sizeof(sort[0]))-1;compareElement++){
if (sort[compareElement] < sort[currentElement]){
min = compareElement;
}
}

temp = sort[currentElement];
sort[currentElement] = sort[min];
sort[min] = temp;
}

printf("Ascending Order: ");

for (currentElement=0;currentElement<=(sizeof(sort)/sizeof(sort[0]))-1;currentElement++){
printf("%d, ", sort[currentElement]);
}

printf("\n");

printf("Descending Order: ");

for (currentElement=(sizeof(sort)/sizeof(sort[0]))-1;currentElement>=0;currentElement--){
printf("%d, ", sort[currentElement]);
}

printf("\n");

return 0;

}

基本上整个这应该是一个从头文件中调用我的函数的程序。一切都很顺利,甚至在我尝试添加验证之前它就可以正常工作。我不知道这是否真的有帮助,但我会感谢每一个反馈。我使用 Turbo C 模拟器作为我的编译器和工作环境。

最佳答案

printf("Input integers to sort: ");
for (i=0;i<10;i++){
scanf("%d", &sort[i]);

似乎缺少关闭}。在 header 中编写实际定义并不是一个好习惯。

关于c - 初始化 block 部分不允许声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59194272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com