c - 初始化块部分不允许声明
问题描述
#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]);
似乎}
缺少关闭。在标题中编写实际定义不是一个好习惯。
推荐阅读
- laravel - 不能在 Laravel 6 中使用依赖注入
- python - Python如何在不同级别对Dataframe中的多索引列进行排序?
- node.js - React-native node.js 显示错误:多部分:使用 react-native-image-picker 上传图像时未找到边界
- integration - 将 ionic 5 与缩放集成
- python - 如何确定字符串中是否存在相邻的单词列表?
- c++ - 在 C++ 中使用大量输入数据时出现分段错误
- sql - 使用嵌套案例更新 SQL 中的查询
- python - 如何使用 Pandas 将多列合并为 1 个指标?
- kubernetes - Kubernetes - 容器上的 Calico CrashLoopBack
- android - 如何防止安卓应用中的自动点击作弊?