首页 > 解决方案 > 初始化块部分不允许声明

问题描述

#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 模拟器作为我的编译器和工作环境。

标签: cvariablesinitializationdeclaration

解决方案


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

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


推荐阅读