ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
case 1: printf("Want to enter a record?(Y/N)\n");
fflush(stdin);
// scanf("%c",&a);
a=getche();
while(a=='y'||a=='Y')
{
printf("Enter the name of cricketer=");
// gets(i.name);
scanf("%s",&i.name);
printf("Enter the no. of runs he scored=");
scanf("%d",&i.runs);
fprintf(fp,"%s %d",i.name,i.runs);
fputs("\n",fp);
printf("Enter another record(y/n)?");
a=getche();
case 2: printf("hahaha");
default: exit();
}
}
getch();
fclose(fp);
}
plus if i use gets(i.name) the controller doesnt stop at the name part and directly goes to the runs part if i enter any name in capital letters,plz help,and also help me to form a cricketer managing interface as this code is also the same...
It is running but when u enter any capital letters such as Sachin Tend. then the cursor doesnt stop at runs and directly goes to do u want to continue..? why is that happening?
I think it's not because you enter capital letters. By default the input terminator in scanf is any white space character. So entering Sachin Tend makes it provide Sachin to scanf and Tend to next scanf call. You should use gets to get strings.
According to the man page (under section "BUGS"): Never use gets().
Use fgets() instead.
You could explain "why", you know.
gets is not recommended because it doesn't allow to specify target buffer size (which might cause buffer overrun + segmentation fault in some cases).
guys but dont u think fgets is used to read a string from the given file and i want to read a string from buffer memory???
No, I don't think so. Your program is reading from the standard input ("stdin"). That is not the same as a memory buffer.
Of course it seems logical to use gets() for that, since that function only can read from standard input.
But fgets() can also read from stdin. But you will need to specify it explicitly.
But the point that matters is that you can (or need to) pass the size of the buffer to fgets(), so it will not write to memory beyond the allocated buffer. That would be one of those infamous "buffer overflow" vulnerabilities, which can easily cause hard-to-explain ugly crashes and security-holes.
now i tried
#define size 100
and defined the array as name[size] and fgets(stdin,size,i.name);
now it isnt even taking the 2nd character it is just taking S and the loop breaks
now i tried
#define size 100
and defined the array as name[size] and fgets(stdin,size,i.name);
now it isnt even taking the 2nd character it is just taking S and the loop breaks
You got your code to compile??
The prototype for fgets() is:
Code:
char * fgets(char *s, int size, FILE *stream);
I'm sure Hko had the best of intentions when he tried to help you, but he erred with the example code he supplied.
In conclusion, refer to the man-page for fgets() on further details.
Here's how I would setup the queries in my code:
Code:
#include <stdio.h>
#include <stdbool.h>
void displayMenu();
int main(int argc, char** argv)
{
//...
bool done = false;
do
{
char choice[3] = {0}; // one space for choice, one for newline, one for null
displayMenu();
fgets(choice, sizeof(choice), stdin);
switch (choice[0])
{
case '1':
//...
break;
case '2':
//...
break;
case 'q':
done = true;
break;
default:
printf("Unknown choice made; try again.\n\n");
break;
}
} while (!done);
//...
}
void displayMenu()
{
printf("\nMenu:\n");
printf("\t1. Enter new Player information.\n");
printf("\t2. TBD.\n");
printf("\tq. Quit.\n");
printf("Enter a choice: ");
}
im srry but its still not working if i use fgets(i.name,100,stdin);
it is still not working this is the code as in the whole code
#include<stdio.h>
#include<conio.h>
#define size 100
int main()
{
char a,ch;
FILE *fp;
int choice;
struct cricketer
{
char name[size];
int runs;
}i;
clrscr();//clear screen
//create a dummy file for output
fp=fopen("dummy.txt","a");
printf("Enter your choice:\n ");
printf("1.Player Menu\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Want to enter a record?(Y/N):");
a=getche();
while(a=='y'||a=='Y')
{
printf("\nEnter the name of cricketer:");
fgets(i.name,100,stdin);
// scanf("%s",&i.name);
printf("Enter the no. of runs he scored=");
scanf("%d",&i.runs);
fprintf(fp,"%s %d",i.name,i.runs);
fputs("\n",fp);
printf("Enter another record(y/n)?");
a=getche();
fflush(stdin);
default:
The getche() function is not from the standard C library (it probably comes from the conio library). Since I do not have this function on my system, I cannot test it, nor your code.
I suggest you consider using the standard C library to gather your user input. At your disposal are the following functions:
fgets()
scanf()
getchar()
Try experimenting with this code:
Code:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
int main()
{
char choice;
while (true)
{
printf("Enter a choice [y/n/q]: ");
choice = getchar(); // get the first character of user input
getchar(); // get/discard the newline
choice = tolower(choice);
if (choice == 'q')
break;
if (choice == 'y')
printf("choice 'y' was entered!\n");
else if (choice == 'n')
printf("choice 'n' was entered!\n");
else
printf("bad input; try again.\n");
}
}
This code is still prone to error if the user enters more than one character as input. When you have become more proficient at C programming, then perhaps you can delve into more advanced programming features.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.