943,834 Members | Top Members by Rank

View Poll Results: Is my query a dumb one?
Yes! 4 50.00%
Ofcourse! 1 12.50%
Not at all! 0 0%
You're a nice guy! 3 37.50%
Voters: 8. You may not vote on this poll

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6368
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 5th, 2007
0

C Program where gets(), getchar(), are not working.

Expand Post »
Hello Geeks!

I have tried real hard trying to figure out the solution for why the getchar() and fputs() arent working in my program. I have already wasted so much time thinking and debugging about it and here I am after registering wid DANIWEB for the first time.

I will be attaching my program PROGRAM.C below, please help me out why in the function getline() the getchar() isnt working?? It is not accepting values from the user.

I will also be attaching another program SAMPLE.C which involves the same function called getline() which works in a perfect manner.

Please have the file LOV.TXT in the program directory and execute.

All help and suggestions appreciated.

Thank you.
Mohan!
Attached Files
File Type: c sample.C (586 Bytes, 48 views)
File Type: txt lov.txt (201 Bytes, 22 views)
File Type: c program.c (19.5 KB, 30 views)
Last edited by brightmohan; May 5th, 2007 at 1:10 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

post your code here otherwise ppl will not download it.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

there is a lot easier way to get a line from the keyboard than what you coded in the program.c file. fgets() will do all that nasty work for you, like this -- notice there are no loops and only 1 line of code other than the buffer declaration.

  1. char line[100];
  2. fgets(line,sizeof(line),stdin);

BTW: your question is ok, its the poll that is a dumb one.
Last edited by Ancient Dragon; May 5th, 2007 at 1:00 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,951 posts
since Aug 2005
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

Click to Expand / Collapse  Quote originally posted by iamthwee ...
post your code here otherwise ppl will not download it.
Okie Dokie! Let me do that!

Thank You very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

If you're having issues with downloading the code you can see it here.

The PROGRAM.C code:
  1.  
  2.  
  3. #define TRUE 1
  4. #define MAX_REC_LEN 1024
  5. #define EOF_MARKER 26
  6. #define LF 10
  7. #define CR 13
  8.  
  9.  
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12. #include<string.h>
  13. #include<errno.h>
  14. //#include<curses.h>
  15.  
  16. int getline(char line[], int max)
  17. {
  18. int nch = 0;
  19. int c;
  20. max = max - 1; /* leave room for '\0' */
  21.  
  22. while((c = getchar()) != '\0')
  23. {
  24. if(c == '\n')
  25. break;
  26.  
  27. if(nch < max)
  28. {
  29. line[nch] = c;
  30. nch = nch + 1;
  31. }
  32. }
  33.  
  34. if(c == EOF && nch == 0)
  35. return EOF;
  36.  
  37. line[nch] = '\0';
  38. return nch;
  39. }
  40.  
  41.  
  42.  
  43. void HR(int iLen)
  44. {
  45. int i;
  46.  
  47. for (i = 0; i < iLen; i++)
  48. printf("-");
  49.  
  50. printf("\n");
  51. return;
  52.  
  53. } /* end HR() */
  54.  
  55.  
  56.  
  57. /******************************************************************************/
  58. int search_delete(FILE *fp,char *szReadLine, long lLineCount, long lLineLen, long lThisFilePos, long *lLastFilePos, int isFilePosErr, char *lookup_field, int del_choice)
  59. /******************************************************************************/
  60. /* Use: Details for current line */
  61. /* */
  62. /* Arguments: char *szReadLine = Read buffer containg text line */
  63. /* long lLineCount = Current line number */
  64. /* long lLineLen = Current line length */
  65. /* long lThisFilePos = Offset of start of current line */
  66. /* long *lLastFilePos = Offset of end of current line */
  67. /* int isFilePosErr = True if start of current line is not */
  68. /* 1 greater than end of last line */
  69. /* */
  70. /* Return: int 1 or 0 if the record is deleted or not */
  71. /******************************************************************************/
  72. {
  73. int flag=0;
  74.  
  75. char *cPtr; /* Pointer to current character */
  76.  
  77. int i=-1,j=-1,k=-1;
  78.  
  79. char *title[20];
  80. char *fname[20];
  81. char *lname[20];
  82. char *price[10];
  83. char *repcost[10];
  84.  
  85. int count1=0, count2=0 ,count3=0, count4=0,count5=0;
  86. int p=0;
  87. int m=-1;
  88. int del_count=0;
  89. int l=-1;
  90.  
  91. int count;
  92. char c;
  93.  
  94. int p_loop=0;
  95.  
  96. //FILE* fp1;
  97. //fp1=fopen("lov1.txt","w+");
  98.  
  99. //printf("Book ID: %ld",lLineNumber);
  100.  
  101. char *temp3;
  102. char *tit;
  103.  
  104. cPtr = szReadLine; /* Point to start of string */
  105.  
  106.  
  107.  
  108.  
  109. /* Print the characters, including null terminator */
  110. for (cPtr = szReadLine; cPtr <= szReadLine + lLineLen; cPtr++)
  111. {
  112. if(del_count==0)
  113. {
  114. if(*cPtr=='|')
  115. {
  116. //printf("A | encountered");
  117. del_count++;
  118. title[++i]='\0';
  119. printf("\n\nThe value in Title is: %s",title);
  120. *cPtr++;
  121. //break;
  122. }
  123. else
  124. {
  125. title[++i]=*cPtr;
  126. count1=i+1;
  127. }
  128. }//end of del_count0
  129.  
  130. if(del_count==1)
  131. {
  132. if(*cPtr=='|')
  133. {
  134. //printf("A | encountered");
  135. del_count++;
  136. fname[++j]='\0';
  137. *cPtr++;
  138. //break;
  139. }
  140. else
  141. {
  142. fname[++j]=*cPtr;
  143. count2=j+1;
  144. // printf("Character Count: %d",count2);
  145. }
  146. }//end of del_count0
  147.  
  148. if(del_count==2)
  149. {
  150. if(*cPtr=='|')
  151. {
  152. //printf("A | encountered");
  153. del_count++;
  154.  
  155. lname[++k]='\0';
  156. printf("\n\nThe value in Last Name is: %s",lname);
  157. *cPtr++;
  158. //break;
  159. }
  160. else
  161. {
  162. lname[++k]=*cPtr;
  163. count3=k+1;
  164. }
  165. }//end of del_count0
  166.  
  167. if(del_count==3)
  168. {
  169. if(*cPtr=='|')
  170. {
  171. //printf("A | encountered");
  172. del_count++;
  173. price[++l]='\0';
  174. printf("\n\nThe value in Price is: %s",price);
  175. *cPtr++;
  176. //break;
  177. }
  178. else
  179. {
  180. price[++l]=*cPtr;
  181. count4=l+1;
  182. }
  183. }//end of del_count
  184.  
  185. if(del_count==4)
  186. {
  187. if(*cPtr=='\0')
  188. {
  189. //printf("A | encountered");
  190. del_count++;
  191. repcost[++m]='\0';
  192. printf("\n\nThe value in Replacement Cost is: %s",repcost);
  193. *cPtr++;
  194. //break;
  195. }
  196. else
  197. {
  198. repcost[++m]=*cPtr;
  199. count5=m; //no +1 cos' its the terminator char
  200. }
  201. }//end of del_count0
  202.  
  203. } /* end for (cPtr) */
  204.  
  205. printf("CCC: %d %d %d %d %d ", count1,count2,count3,count4,count5);
  206.  
  207. for (p=0; p<count1; p++)
  208. {
  209. title[p] = tolower(title[p]);
  210.  
  211. printf("\n--%c--",title[p]);
  212. //lookup_field[p]=tolower(lookup_field[p]);
  213. if(title[p]==lookup_field[p])
  214. {
  215. printf("YES");
  216. }
  217.  
  218. }
  219.  
  220. for (p=0; p<count2; p++)
  221. {
  222. fname[p] = tolower(fname[p]);
  223.  
  224. // printf("\n--%c--",fname[p]);
  225. //lookup_field[p]=tolower(lookup_field[p]);
  226. }
  227.  
  228.  
  229. for (p=0; p<count3; p++)
  230. {
  231. lname[p] = tolower(lname[p]);
  232.  
  233. // printf("\n--%c--",fname[p]);
  234. //lookup_field[p]=tolower(lookup_field[p]);
  235. }
  236.  
  237.  
  238. for (p=0; p<count4; p++)
  239. {
  240. price[p] = tolower(price[p]);
  241.  
  242. // printf("\n--%c--",fname[p]);
  243. //lookup_field[p]=tolower(lookup_field[p]);
  244. }
  245.  
  246.  
  247. for (p=0; p<count5; p++)
  248. {
  249. repcost[p] = tolower(repcost[p]);
  250.  
  251. // printf("\n--%c--",fname[p]);
  252. //lookup_field[p]=tolower(lookup_field[p]);
  253.  
  254.  
  255. }
  256.  
  257. //printf("%s\n %s\n",fname,lookup_field);
  258.  
  259. switch(del_choice)
  260. {
  261. case 1:
  262. //count= strncmp(lookup_field,title,sizeof(title));
  263.  
  264. if(count==0)
  265. {
  266. printf("YES YES");
  267. }
  268.  
  269.  
  270. if(*title==*lookup_field)
  271. {
  272.  
  273. return(1);
  274. }
  275. else
  276. {
  277. return(0);
  278. }
  279. break;
  280. case 2:
  281. if(*fname==*lookup_field)
  282. {
  283. printf("\n\n YEAH BABY THEY ARE EQUAL");
  284. return(1);
  285. }
  286. else
  287. {
  288. return(0);
  289. }
  290. break;
  291. case 3:
  292. if(*lname==*lookup_field)
  293. {
  294. printf("\n\n YEAH BABY THEY ARE EQUAL");
  295. return(1);
  296. }
  297. else
  298. {
  299. return(0);
  300. }
  301. break;
  302.  
  303. } //end of switch case
  304.  
  305.  
  306. printf("\n");
  307.  
  308. } /* end of function search_delete()*/
  309.  
  310.  
  311.  
  312.  
  313. /******************************************************************************/
  314. void PrintLine(char *szReadLine, long lLineCount, long lLineLen,
  315. long lThisFilePos, long *lLastFilePos, int isFilePosErr)
  316. /******************************************************************************/
  317. /* Use: Print detail for current line */
  318. /* */
  319. /* Arguments: char *szReadLine = Read buffer containg text line */
  320. /* long lLineCount = Current line number */
  321. /* long lLineLen = Current line length */
  322. /* long lThisFilePos = Offset of start of current line */
  323. /* long *lLastFilePos = Offset of end of current line */
  324. /* int isFilePosErr = True if start of current line is not */
  325. /* 1 greater than end of last line */
  326. /* */
  327. /* Return: void */
  328. /******************************************************************************/
  329. {
  330. char *cPtr; /* Pointer to current character */
  331.  
  332. int del_count=0;
  333. HR(80); /* Print a separator line */
  334. printf("Book ID: %ld, Length=%#x (dec %ld)\n",lLineCount, (int)lLineLen, lLineLen); /* See PrintHeader() for an */
  335. /* explanation of why the cast */
  336. /* is needed. */
  337. // printf(" Offset:");
  338.  
  339. cPtr = szReadLine; /* Point to start of string */
  340.  
  341. //if (isFilePosErr) /* Indicates offset error */
  342. // printf("*%2x", lThisFilePos++); /* Print '*' plus starting offset */
  343. // else /* Offset okay */
  344. // printf("%3x", lThisFilePos++); /* Just print starting offset */
  345.  
  346. //for (++cPtr; cPtr < szReadLine + lLineLen; cPtr++) /* Remaining offsets */
  347. //printf("%3x", lThisFilePos++);
  348.  
  349. //if (lLastFilePos != NULL) /* Set end position if arg passed */
  350. //*lLastFilePos = lThisFilePos - 1;
  351.  
  352. //printf("\n Hex: ");
  353.  
  354. /* Print the hex values, including null terminator */
  355. //for (cPtr = szReadLine; cPtr <= szReadLine + lLineLen; cPtr++)
  356. //printf("%3x", *cPtr);
  357.  
  358.  
  359. //printf("\n Char: ");
  360.  
  361. if(del_count==0)
  362. {
  363. printf("\n Title: ");
  364. }
  365. /* Print the characters, including null terminator */
  366. for (cPtr = szReadLine; cPtr <= szReadLine + lLineLen; cPtr++)
  367. {
  368.  
  369. switch (*cPtr)
  370. {
  371. case 0: /* Null terminator */
  372. //printf(" \\0");
  373. break;
  374.  
  375. case CR: /* Carriage return */
  376. //printf(" cr");
  377. break;
  378.  
  379. case LF: /* Line feed */
  380. //printf(" lf");
  381. break;
  382.  
  383. case EOF_MARKER: /* DOS end-of-file marker */
  384. //printf(" em");
  385. break;
  386.  
  387. case '|':
  388. //printf("|");
  389. del_count++;
  390. if(del_count==1)
  391. {
  392. printf("\n Author: ");
  393.  
  394. }
  395. if(del_count==2)
  396. {
  397. printf(", ");
  398. }
  399. if(del_count==3)
  400. {
  401. printf("\n Year: ");
  402. }
  403.  
  404. if(del_count==4)
  405. {
  406. printf("\n Replacement Cost: ");
  407. }
  408.  
  409. break;
  410.  
  411. default: /* A 'real' character */
  412. //printf("%3c", *cPtr);
  413. printf("%c",*cPtr);
  414. break;
  415.  
  416. } /* end switch (*cPtr) */
  417.  
  418. } /* end for (cPtr) */
  419.  
  420. printf("\n");
  421. return;
  422.  
  423. } /* end PrintLine()*/
  424.  
  425.  
  426. load_catalogue(FILE *input)
  427. {
  428.  
  429. int isNewline; /* Boolean indicating we've read a CR or LF */
  430. long lFileLen; /* Length of file */
  431. long lIndex; /* Index into cThisLine array */
  432. long lLineCount; /* Current line number */
  433. long lLineLen; /* Current line length */
  434. long lStartPos; /* Offset of start of current line */
  435. long lTotalChars; /* Total characters read */
  436. char cThisLine[MAX_REC_LEN]; /* Contents of current line */
  437. char *cFile; /* Dynamically allocated buffer (entire file) */
  438. char *cThisPtr; /* Pointer to current position in cFile */
  439.  
  440. fseek(input, 0L, SEEK_END); /* Position to end of file */
  441. lFileLen = ftell(input); /* Get file length */
  442. rewind(input); /* Back to start of file */
  443.  
  444. cFile = calloc(lFileLen + 1, sizeof(char));
  445.  
  446. if(cFile == NULL )
  447. {
  448. printf("\nInsufficient memory to read file.\n");
  449. return 0;
  450. }
  451.  
  452. fread(cFile, lFileLen, 1, input); /* Read the entire file into cFile */
  453.  
  454. lLineCount = 0L;
  455. lTotalChars = 0L;
  456.  
  457. cThisPtr = cFile; /* Point to beginning of array */
  458.  
  459. while (*cThisPtr) /* Read until reaching null char */
  460. {
  461. lIndex = 0L; /* Reset counters and flags */
  462. isNewline = 0;
  463. lStartPos = lTotalChars;
  464.  
  465. while (*cThisPtr) /* Read until reaching null char */
  466. {
  467. if (!isNewline) /* Haven't read a CR or LF yet */
  468. {
  469. if (*cThisPtr == CR || *cThisPtr == LF) /* This char IS a CR or LF */
  470. isNewline = 1; /* Set flag */
  471. }
  472.  
  473. else if (*cThisPtr != CR && *cThisPtr != LF) /* Already found CR or LF */
  474. break; /* Done with line */
  475.  
  476. cThisLine[lIndex++] = *cThisPtr++; /* Add char to output and increment */
  477. ++lTotalChars;
  478.  
  479. } /* end while (*cThisPtr) */
  480.  
  481. cThisLine[lIndex] = '\0'; /* Terminate the string */
  482. ++lLineCount; /* Increment the line counter */
  483. lLineLen = strlen(cThisLine); /* Get length of line */
  484.  
  485. /* Print the detail for this line */
  486. PrintLine(cThisLine, lLineCount, lLineLen, lStartPos, NULL, 0);
  487.  
  488. } /* end while (cThisPtr <= cEndPtr) */
  489.  
  490. HR(80); /* Print a separator line */
  491.  
  492. printf("Length of file array=%#x (dec %d)\n", strlen(cFile), strlen(cFile));
  493.  
  494. return 1;
  495.  
  496. }//End of function load_catalogue()
  497.  
  498. /*Load and delete function for various choices*/
  499. load_delete(FILE *input,char *field,int choice)
  500. {
  501.  
  502. int isNewline; /* Boolean indicating we've read a CR or LF */
  503. long lFileLen; /* Length of file */
  504. long lIndex; /* Index into cThisLine array */
  505. long lLineCount; /* Current line number */
  506. long lLineLen; /* Current line length */
  507. long lStartPos; /* Offset of start of current line */
  508. long lTotalChars; /* Total characters read */
  509. char cThisLine[MAX_REC_LEN]; /* Contents of current line */
  510. char *cFile; /* Dynamically allocated buffer (entire file) */
  511. char *cThisPtr; /* Pointer to current position in cFile */
  512. int found=0;
  513. char *buffer;
  514. FILE *file1;
  515.  
  516. fseek(input, 0L, SEEK_END); /* Position to end of file */
  517.  
  518. lFileLen = ftell(input); /* Get file length */
  519. rewind(input); /* Back to start of file */
  520.  
  521.  
  522. cFile = calloc(lFileLen + 1, sizeof(char));
  523.  
  524. buffer= calloc(lFileLen + 1, sizeof(char));
  525.  
  526. if(cFile == NULL )
  527. {
  528. printf("\nInsufficient memory to read file.\n");
  529. return 0;
  530. }
  531.  
  532. fread(cFile, lFileLen, 1, input); /* Read the entire file into cFile */
  533. fclose(input);
  534.  
  535. file1=fopen("lov1.txt","w+");
  536.  
  537. lLineCount = 0L;
  538. lTotalChars = 0L;
  539.  
  540. cThisPtr = cFile; /* Point to beginning of array */
  541.  
  542. while (*cThisPtr) /* Read until reaching null char */
  543. {
  544. lIndex = 0L; /* Reset counters and flags */
  545. isNewline = 0;
  546. lStartPos = lTotalChars;
  547.  
  548. while (*cThisPtr) /* Read until reaching null char */
  549. {
  550. if (!isNewline) /* Haven't read a CR or LF yet */
  551. {
  552. if (*cThisPtr == CR || *cThisPtr == LF) /* This char IS a CR or LF */
  553. isNewline = 1; /* Set flag */
  554. }
  555.  
  556. else if (*cThisPtr != CR && *cThisPtr != LF) /* Already found CR or LF */
  557. break; /* Done with line */
  558.  
  559. cThisLine[lIndex++] = *cThisPtr++; /* Add char to output and increment */
  560. ++lTotalChars;
  561.  
  562. } /* end while (*cThisPtr) */
  563.  
  564. cThisLine[lIndex] = '\0'; /* Terminate the string */
  565. ++lLineCount; /* Increment the line counter */
  566. lLineLen = strlen(cThisLine); /* Get length of line */
  567.  
  568. /* Print the detail for this line */
  569. // PrintLine(cThisLine, lLineCount, lLineLen, lStartPos, NULL, 0);
  570.  
  571. found=search_delete(input, cThisLine, lLineCount, lLineLen, lStartPos, NULL, 0,field,choice);
  572.  
  573. if(found==1)
  574. {
  575. //dont write it to the buffer
  576. printf("One Matching Record Found: Deleted");
  577. }
  578. else {
  579. //Write that line to the buffer.
  580. fprintf(file1,"%s",cThisLine);
  581.  
  582. }
  583.  
  584. } /* end while (cThisPtr <= cEndPtr) */
  585.  
  586. HR(80); /* Print a separator line */
  587.  
  588. printf("Length of file array=%#x (dec %d)\n", strlen(cFile), strlen(cFile));
  589.  
  590. return 1;
  591.  
  592. } //end of function load_delete()
  593.  
  594. save_catalogue(char *filename, char *title, char *fname, char *lname, int year, float repl_cost )
  595. {
  596.  
  597.  
  598. }//end of function save_catalogue()
  599.  
  600. sort_display(char *filename, int flag)
  601. {
  602.  
  603. } //end of the sort_display() function
  604.  
  605. void main()
  606.  
  607. {
  608.  
  609. int choice,del_choice;
  610. char *cat_file="Catalogue.txt";
  611. FILE *fp,*fp1;
  612. int i;
  613.  
  614. char *del_cat,au_fname[20], au_lname[20];
  615. char new_cat_file[30],new_title[30], new_fau[30], new_lau[30];
  616.  
  617. int new_year, sort_choice;
  618.  
  619. float new_rep_cost;
  620.  
  621. int sort_flag;
  622.  
  623. while(TRUE)
  624. {
  625.  
  626. printf("***Library Catalogue***\n\n");
  627. printf("Select your option please:\n\n");
  628. printf("1> Load Catalogue\n");
  629. printf("2> Delete book from the Catalogue\n");
  630. printf("3> Save Catalogue[Inserting a new item]\n");
  631. printf("4> Sort & Display Catalogue\n");
  632. printf("5> Quit\n");
  633.  
  634. scanf("%d",&choice);
  635.  
  636. switch(choice)
  637. {
  638. case 1: printf("Load Catalogue\n");
  639. printf("Enter the name of the catalogue file to load:\n");
  640. scanf("%s",cat_file);
  641. fp=fopen(cat_file, "r+");
  642. if(fp==NULL)
  643. {
  644. printf("Error Opening The Catalogue File\n\n");
  645. break;
  646. }
  647. else
  648. {
  649. load_catalogue(fp);
  650. }
  651. fclose(fp); //close the file stream
  652.  
  653. break;
  654.  
  655. case 2: printf("Delete book from the Catalogue\n");
  656. printf("Please enter the name of the Catalogue File: ");
  657. scanf("%s",new_cat_file);
  658. fp1=fopen(new_cat_file, "r+");
  659. if(fp1==NULL)
  660. {
  661. printf("Error Opening The Catalogue File\n\n");
  662. break;
  663. }
  664. printf("Press 1 to delete a book by Title\n");
  665. printf("Press 2 to delete a book by Author's First Name\n");
  666. printf("Press 3 to delete a book by Author's Last Name\n");
  667. printf("Your Choice please: ");
  668. scanf("%d",&del_choice);
  669.  
  670. switch(del_choice)
  671. {
  672. case 1:
  673. printf("Please enter the book title to delete: ");
  674. //scanf("%s",del_cat);
  675. getline(del_cat,30);
  676. printf("%s",del_cat);
  677.  
  678.  
  679. for(i=0;i<=30;i++)
  680. {
  681. del_cat[i] = tolower(del_cat[i]);
  682. printf("%c",del_cat[i]);
  683. }
  684.  
  685. load_delete(fp1,del_cat,del_choice);
  686.  
  687. break;
  688.  
  689. case 2:
  690. printf("Enter the Author's First Name to delete books: ");
  691. scanf("%s",au_fname);
  692. for(i=0;i<=20;i++)
  693. {
  694. au_fname[i] = tolower(au_fname[i]);
  695. }
  696. load_delete(fp1,au_fname,del_choice);
  697. break;
  698.  
  699. case 3:
  700. printf("\nEnter the Author's Last Name: ");
  701. scanf("%s",au_lname);
  702. for(i=0;i<=20;i++)
  703. {
  704. au_lname[i] = tolower(au_lname[i]);
  705. }
  706. load_delete(fp1,au_lname,del_choice);
  707. break;
  708.  
  709. default:
  710. printf("Sorry Wrong Choice!");
  711. break;
  712. }
  713.  
  714. break;
  715.  
  716. case 3: printf("Save Catalogue[Inserting a new item]\n");
  717.  
  718. printf("Please enter the name of the Catalogue File: ");
  719. scanf("%s",new_cat_file);
  720. printf("\nEnter the title of the book: ");
  721. scanf("%s",new_title);
  722. printf("\nEnter the Author's First Name: ");
  723. scanf("%s",new_fau);
  724. printf("\nEnter the Author's Last Name: ");
  725. scanf("%s",new_lau);
  726. printf("\nEnter the Year of Publication: ");
  727. scanf("%d",&new_year);
  728. printf("\nEnter the Replacement Cost: ");
  729. scanf("%f",&new_rep_cost);
  730. save_catalogue(cat_file,new_title,new_fau,new_lau,new_year,new_rep_cost);
  731. break;
  732.  
  733. case 4: printf("Sort & Display Catalogue\n");
  734. printf("Please slect the sorting order: \n");
  735. printf("1> By Title\n2> By LastName\n3> By Replacement Cost\n4> By Publication Year\n ");
  736. scanf("%d",&sort_choice);
  737.  
  738. switch(sort_choice)
  739. {
  740. case 1:
  741. sort_flag=1;
  742. sort_display(cat_file,sort_flag);
  743. break;
  744.  
  745. case 2:
  746. sort_flag=2;
  747. sort_display(cat_file,sort_flag);
  748. break;
  749.  
  750. case 3: sort_flag=3;
  751. sort_display(cat_file,sort_flag);
  752. break;
  753.  
  754. case 4: sort_flag=4;
  755. sort_display(cat_file,sort_flag);
  756. break;
  757.  
  758. default: printf("Sorry! Invalid Choice.");
  759. break;
  760.  
  761. } //End of inneer swith case
  762.  
  763.  
  764. case 5: printf("Thank you for using the system :)");
  765. exit(0);
  766. break;
  767.  
  768. default: printf("Sorry! Invalid choice.");
  769. break;
  770.  
  771. }//End of the main switch case
  772.  
  773. } // End of the main While loop which runs infinite
  774.  
  775. } //End of the main() function
Last edited by ~s.o.s~; May 5th, 2007 at 1:49 pm. Reason: Fixed code tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

And sample.c and lov.txt?

Please post them as well if you haven't already.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

there is a lot easier way to get a line from the keyboard than what you coded in the program.c file. fgets() will do all that nasty work for you, like this -- notice there are no loops and only 1 line of code other than the buffer declaration.

  1. char line[100];
  2. fgets(line,sizeof(line),stdin);

BTW: your question is ok, its the poll that is a dumb one.
Gosh! Nope! It still doesnt work.....The interpreter jumps at fgets() I dunno why. It doesnt stop to get characters from my Keyboard. Is it because I'm doing it on Laptop??? lol ?

Gosh!! The time is just flying!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

Here is the SAMPLE.C where gets() works absolutely fine. I dont understand why it isnt working in PROGRAM.C


  1.  
  2. #include<errno.h>
  3. #include<stdio.h>
  4.  
  5. int getline(char line[], int max)
  6. {
  7. int nch = 0;
  8. int c;
  9. max = max - 1; /* leave room for '\0' */
  10.  
  11. while((c = getchar()) != '\0')
  12. {
  13. if(c == '\n')
  14. break;
  15.  
  16. if(nch < max)
  17. {
  18. line[nch] = c;
  19. nch = nch + 1;
  20. }
  21. }
  22.  
  23. if(c == EOF && nch == 0)
  24. return EOF;
  25.  
  26. line[nch] = '\0';
  27. return nch;
  28. }
  29.  
  30.  
  31. #include <stdio.h>
  32.  
  33. extern int getline(char [], int);
  34.  
  35. main()
  36. {
  37. char line[256];
  38.  
  39. printf("Your names");
  40. getline(line,256);
  41. //while(getline(line, 256) != '\0')
  42. //
  43. printf("you typed \"%s\"\n", line);
  44.  
  45. return 0;
  46. }

I tired the solution posted by geek ANCIENT DRAGON. It is not working too....:
  1. char line[100];
  2. fgets(line,sizeof(line),stdin);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

The lov.txt file is:

How to swim|Tina|Turner|1998|1980.90
How to learn C++|Tim|DelMar|1997|2909.89
How to Crawl|Benicio|Tot|1998|1980.90
How to learn Java|All|Gore|1997|2909.89
How to Relax|Earnst|Summers|1889|2039.90


Its the file that you can pass to the PROGRAM.C while executing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightmohan is offline Offline
12 posts
since May 2007
May 5th, 2007
0

Re: C Program where gets(), getchar(), are not working.

Here is the SAMPLE.C where gets() works absolutely fine. I dont understand why it isnt working in PROGRAM.C


  1.  
  2. #include<errno.h>
  3. #include<stdio.h>
  4.  
  5. int getline(char line[], int max)
  6. {
  7. int nch = 0;
  8. int c;
  9. max = max - 1; /* leave room for '\0' */
  10.  
  11. while((c = getchar()) != '\0')
  12. {
  13. if(c == '\n')
  14. break;
  15.  
  16. if(nch < max)
  17. {
  18. line[nch] = c;
  19. nch = nch + 1;
  20. }
  21. }
  22.  
  23. if(c == EOF && nch == 0)
  24. return EOF;
  25.  
  26. line[nch] = '\0';
  27. return nch;
  28. }
  29.  
  30.  
  31. #include <stdio.h>
  32.  
  33. extern int getline(char [], int);
  34.  
  35. main()
  36. {
  37. char line[256];
  38.  
  39. printf("Your names");
  40. getline(line,256);
  41. //while(getline(line, 256) != '\0')
  42. //
  43. printf("you typed \"%s\"\n", line);
  44.  
  45. return 0;
  46. }

I tired the solution posted by geek ANCIENT DRAGON. It is not working too....:
  1. char line[100];
  2. fgets(line,sizeof(line),stdin);
Take a look at this function:

  1. #include <stdio.h>
  2.  
  3. int *getline( char *string, size_t size )
  4. {
  5. size_t i = 0;
  6. for(;;)
  7. {
  8. int ch = fgetc(stdin);
  9. if(ch == '\n' || ch == EOF)
  10. {
  11. break;
  12. }
  13. if(i < size - 1)
  14. {
  15. string[i++] = ch;
  16. }
  17. } /* forever loop end */
  18.  
  19. string[i] = '\0';
  20. return string;
  21. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: WinApi GUI Menu Problem
Next Thread in C Forum Timeline: search binary tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC