-10

さて、いくつかのコーディングを終了しようとしています.....そして、プログラムの何が問題なのか正確にはわかりません変数のメモリ位置を教えてくれると思うので、エラーは出力領域にあるようですが、0に初期化しましたそして今何が悪いのかわからない...混乱した顔。ただし、ここにプログラムがあります(エラー領域のみ、完全なプログラムではありません)。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Mica
{
    char FleNme [256];
}Mscl;

typedef struct Blade
{
    char BladeName [80];
}blade;

typedef struct eader
{
    char Header[256];
}header;

int main()
{
    Mscl Access;
    blade info;
    int hcount =0;
    int x=0;
    char decision;
    float BladePrice = 0;
    int spin = 0;
    int speed= 0;
    int weight= 0;
    header inf[hcount];
    FILE *Nmes;

    system("cls");

    printf("Please enter the name of the file you wish to Add:\n ");
    scanf("%s",&Access.FleNme);

    if((Nmes=fopen(Access.FleNme,"r"))==NULL)
    {
        printf("File can be created\n");
        Nmes=fopen(Access.FleNme,"w");
    }
    else
    {
        printf("File exist Already redirecting to Menu y for yes n for no: ");
        scanf("%s",&decision);

        if(decision=='y')
        {
            main();
        }
        else
        {
            exit(0);
        }
    }

    printf("Format Example: Bladename  BladePrice  Spin  Control  Weight\n");
    printf("Please Input how many headers you will be using: ");
    scanf("%d",&hcount);

    for (x=0;x<hcount;x++)
    {
        printf("Please enter The Header Name: ");
        scanf("%s",&inf[x].Header);
    }

    system("cls");
    printf("Please enter Name of Item: ");
    scanf("%s",&info.BladeName);
    printf("\nPlease enter Price of Item: ");
    scanf("%f",&BladePrice);
    printf("\nPlease enter Speed of Item: ");
    scanf("%d",&speed);
    printf("\nPlease enter Spin of Item: ");
    scanf("%d",&spin);
    printf("\nPlease enter weight of Item: ");
    scanf("%d",&weight);

    for(x=0;x<hcount;x++)
    {
        printf("%s\t",inf[x].Header);
    }

    printf("\n%s\t%.2f\t%d\t%d\t%d",&info.BladeName,&BladePrice,&speed,&spin,&weight);
}
4

1 に答える 1

2

1つのエラーは

char decision;
...
scanf("%s",&decision);

scanfchar配列を単一のcharに書き込むように求めています。これにより未定義の結果が得られますdecisionが、上書きされた後にスタック変数が発生する可能性があります。

これを修正するには%c、フォーマット指定子として使用します

scanf("%c",&decision);

decisionまたはchar配列として宣言します

char decision[32];
scanf("%.31s",decision);

引き続き問題が発生する場合は、プログラム内のどこで発生したかをより具体的に示してください。時間をかけてコードをフォーマットし、インデントを修正することも大きな助けになります。

于 2013-01-31T14:15:59.863 に答える