C: structures, easy, full short program code shown

Here is the program. It is fully commented what it aims to do etc. The issues are two:

a) I get the typical error of "data definition has no type or storage class when I create the prototypes of the functions.

b) After entering the input to the scanf question and pressing enter, I still have to type any letter and again press enter to continue through the program, otherwise it just does not progress: Thanks

#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");

                  }

Upvotes: 0

Views: 744

Answers (3)

Tom Tanner
Tom Tanner

Reputation: 9354

Firstly, your formatting makes your code not exactly readable.

Secondly main( should be int main( and return a result

Thirdly, don't call fflush on an input stream. It has undefined behaviour.

Fourthly, you use scanf. NEVER use scanf. EVER. Read a whole line from the console (using fgets, not gets (which sets you up for another world of pain)) and then use sscanf and please remember to check the result that gets returned.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882146

question();

is not a decent prototype. Prototypes should have return types and parameters, such as:

void question (void);

Ditto for your function definitions as well.

Those things may have worked when I was a boy (during the K&R years) but there are much better ways of doing things now :-)

Upvotes: 3

unwind
unwind

Reputation: 400009

Your formatting is odd, and there are many issues.

You need to learn how to write function declarations, especially what a return type means.

It should be e.g. void create_structure_name(void);.

Another: the global variable char name[30]; is shadowed by struct book name; inside the create_structure_name function.

Upvotes: 3

Related Questions