0

これがプログラムです。目的などについて完全にコメントされています。問題は2つあります。

a) 関数のプロトタイプを作成するときに、「データ定義に型またはストレージ クラスがありません。

b) scanf の質問に入力して Enter キーを押した後、文字を入力してもう一度 Enter キーを押してプログラムを続行する必要があります。それ以外の場合は進行しません: ありがとう

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


/* Object:
 * A program that allows upon user input recreate structures
 * with different names. Except when entering 'n', it will always
 * ask "Do you want to create a new book (structure)? 
 * It is about storing books, title, author, price, and pages */



// 1. I create the blue print of the structure (forget about typedef...)

        struct book {
        char title[100];
        int pages;
        int price;
        char author[50];
        } ;

// 2. I declare some variables

char wants;
char name [30];


// 3. Function prototypes

question();
create_structure_name();


// +++++++++++++++++++++++++++++++++++++++++++++++++++                

  create_structure_name(){ 
  printf("Give a name to your structure: ");
  fflush(stdin);
  scanf("%s\n", &name);  
  printf("you have chosen this name %s for the structure: \n", name);
  struct book name;       

  printf("Title: ");
  fflush(stdin);
  scanf("%s\n", &name.title);
  printf("The title is %s: ", name.title);       


  printf("Paginas: ");
  fflush(stdin);
  scanf("%d\n", &name.pages);
  printf("It has this number of pages %d\n: ", name.pages);

  printf("Price: ");
  fflush(stdin);
  scanf("%d\n", &name.price);
  printf("The price is %d: ", name.price);

  printf("Author: ");
  fflush(stdin);
  scanf("%s\n", &name.author);
  printf("The author is %s: ", name.author);       
}      

// I define the function ++++++++++++++++++++++++++++

question()
{
printf("Do you want to create a new book? :");
fflush(stdin);
scanf("%c\n", &wants);
     while(wants!= 'n')
     {
         create_structure_name();         
     }             
 }           


// ++++++++++++++++++++++++++++++++++++++++++++++++++++        


        main(void)
        {
        create_structure_name();
        question();
        system("PAUSE");

                  }
4

3 に答える 3

3

あなたのフォーマットは奇妙で、多くの問題があります。

関数宣言の書き方、特に戻り値の型の意味を学ぶ必要があります。

それは例えばである必要がありますvoid create_structure_name(void);

もう 1 つ: グローバル変数は、関数内char name[30];でシャドウされます。struct book name;create_structure_name

于 2013-08-28T08:51:15.153 に答える
3
question();

まともなプロトタイプではありません。プロトタイプには、次のような戻り値の型とパラメーターが必要です。

void question (void);

関数定義についても同様です。

私が子供の頃 (K&R の時代) には、これらの方法でうまくいったかもしれませんが、今ではもっと良い方法があります :-)

于 2013-08-28T08:52:42.863 に答える