c - Can't take input in a string -
in program, i'm not able input in string c[] (in add_diary() ) using fgets() or gets(), program skips input process, please tell me doing wrong. can't started. in diary important thing write , thing 'you can't write, cuz fgets() isn't working'.
#include <stdio.h> #include <stdlib.h> void access(); void diary_main(char user[],char pass[]); void diary_login(); void add_diary(unsigned dd, unsigned mm, unsigned yy); void add_entry(unsigned choice); void del_entry(unsigned dd, unsigned mm, unsigned yy); void update_entry(unsigned dd, unsigned mm, unsigned yy); void logout(); void main() { access(); } void access() { char password[10],username[20], ch; int i; printf("enter user name: "); gets(username); printf("enter password (8 characters):"); for(i=0;i<8;i++) { ch = getch(); password[i] = ch; ch = '*' ; printf("%c", ch); } password[i] = '\0'; /*printf("\nyour password :"); for(i=0;i<8;i++) { printf("%c", password[i]);*/ diary_main(username, password); } void diary_main(char user[],char pass[]) { /*file *fptr; //char file_name[10]; //sprintf(file_name, "user%s.txt", user); fptr = fopen("login.txt", "w"); fprintf(fptr,"\n%s",user); fclose(fptr);*/ diary_login(); } void diary_login() { unsigned choice; printf("\nenter choice\n"); printf("1. add diary entry\n"); printf("2. delete diary entry\n"); printf("3. update diary entry\n"); printf("4. logout\n"); printf("5. exit\n"); scanf("%d", &choice); if (choice == 1) { add_entry(choice); } else if (choice == 2) { add_entry(choice); } else if (choice == 3) { add_entry(choice); } else if (choice == 4) { logout(); } else if (choice == 5) { exit(1); } else { //printf("%d", choice); printf("enter correct choice"); diary_login(); } } void add_entry(unsigned choice) { unsigned dd, mm, yy; printf("enter date (dd/mm/yy):"); scanf("%u/%u/%u", &dd, &mm, &yy); //printf("%u/%u/%u", dd, mm, yy); if ((dd>31 || mm>12) || (dd>29 && mm == 2) || (yy!=17)) { printf("\nenter correct date\n"); add_entry(choice); } else { //printf("else"); switch(choice) { case 1: add_diary(dd, mm, yy); break; case 2: del_entry(dd, mm, yy); break; case 3: update_entry(dd, mm, yy); break; } } } void add_diary(unsigned dd, unsigned mm, unsigned yy) { //printf("add_diary"); long file_extra; char file_name[50], c[5000]; file *fptr; file_extra = (((dd*100)+mm)*100)+yy; sprintf(file_name, "diary%ld.txt", file_extra); fptr = fopen(file_name, "w"); /*if(fptr == null){ printf("error"); exit(1); }*/ printf("\nstart writing diary..\n"); scanf("%s", c); //fgets(c, sizeof(c), stdin); //gets(c); fprintf(fptr,"%s",c); fclose(fptr); } void del_entry(unsigned dd, unsigned mm, unsigned yy) { long file_extra; char file_name[50]; file_extra = (((dd*100)+mm)*100)+yy; sprintf(file_name, "diary%ld.txt", file_extra); int status; status = remove(file_name); if( status == 0 ) { printf("entry deleted successfully.\n"); } else { printf("unable delete file\n"); } } void update_entry(unsigned dd, unsigned mm, unsigned yy) { long file_extra; char file_name[50], c[5000]; file_extra = (((dd*100)+mm)*100)+yy; sprintf(file_name, "diary%ld.txt", file_extra); file *fptr; fptr = fopen(file_name, "w"); printf("\nstart editing diary..\n"); scanf("%s", c); //fgets(c, sizeof(c), stdin); //gets(c); fprintf(fptr,"%s",c); fclose(fptr); } void logout(){ }
from understanding how scanf works:
let's have them input "hello world", brought in "hello world\n". can work around selecting n-1 in array ('\n') , replace terminating ('\o').
Comments
Post a Comment