C: structures, easy, full short program code shown -
here program. commented aims etc. issues two:
a) typical error of "data definition has no type or storage class when create prototypes of functions.
b) after entering input scanf question , pressing enter, still have type letter , again press enter continue through program, otherwise not progress: thanks
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> /* object: * program allows upon user input recreate structures * different names. except when entering 'n', * ask "do want create new book (structure)? * storing books, title, author, price, , pages */ // 1. create blue print of structure (forget typedef...) struct book { char title[100]; int pages; int price; char author[50]; } ; // 2. declare variables char wants; char name [30]; // 3. function prototypes question(); create_structure_name(); // +++++++++++++++++++++++++++++++++++++++++++++++++++ create_structure_name(){ printf("give name structure: "); fflush(stdin); scanf("%s\n", &name); printf("you have chosen name %s structure: \n", name); struct book name; printf("title: "); fflush(stdin); scanf("%s\n", &name.title); printf("the title %s: ", name.title); printf("paginas: "); fflush(stdin); scanf("%d\n", &name.pages); printf("it has number of pages %d\n: ", name.pages); printf("price: "); fflush(stdin); scanf("%d\n", &name.price); printf("the price %d: ", name.price); printf("author: "); fflush(stdin); scanf("%s\n", &name.author); printf("the author %s: ", name.author); } // define function ++++++++++++++++++++++++++++ question() { printf("do want create new book? :"); fflush(stdin); scanf("%c\n", &wants); while(wants!= 'n') { create_structure_name(); } } // ++++++++++++++++++++++++++++++++++++++++++++++++++++ main(void) { create_structure_name(); question(); system("pause"); }
firstly, formatting makes code not readable.
secondly main(
should int main(
, return result
thirdly, don't call fflush
on input stream. has undefined behaviour.
fourthly, use scanf
. never use scanf
. ever. read whole line console (using fgets
, not gets
(which sets world of pain)) , use sscanf
, please remember check result gets returned.
Comments
Post a Comment