LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2009, 04:34 AM   #1
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Rep: Reputation: 0
why is this code not running?


#include<stdio.h>
#include<conio.h>
void main()
{ char a='\0';
FILE *fp;
char ch;
int choice;
struct cricketer
{
char name[100];
int runs;
};
struct cricketer i;
fp=fopen("dummy.txt","a");
printf("enter the choice");
scanf("%d",&choice);
switch(choice)
{


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...
 
Old 02-12-2009, 04:42 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Well what's not running about it? You're not trying to record runs for England are you? that'll be why I'm sure, esp when you get to Ian Bell.
 
Old 02-12-2009, 04:52 AM   #3
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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?
 
Old 02-12-2009, 06:09 AM   #4
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
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.
 
Old 02-12-2009, 06:17 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by rizwanrafique View Post
You should use gets to get strings.
According to the man page (under section "BUGS"): Never use gets().

Use fgets() instead.
 
Old 02-12-2009, 06:42 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Hko View Post
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).
 
Old 02-12-2009, 07:32 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by ErV View Post
You could explain "why", you know.
The man page I mentioned already explaines it.
 
Old 02-17-2009, 04:24 AM   #8
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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???
 
Old 02-17-2009, 04:42 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Wink

Quote:
Originally Posted by savitrasapre View Post
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.

This code using gets():
Code:
#define STRSIZE 40
char mystring[STRSIZE];
gets(mystring);  /* Bad code! Don't use.. */
... is equivalent and should be replaced using fgets() this way:
Code:
#define STRSIZE 40
char mystring[STRSIZE];
fgets(stdin, STRSIZE, mystring);
Seems "ErV" was right when saying that I could have explained why..
 
Old 02-17-2009, 05:01 AM   #10
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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
 
Old 02-17-2009, 12:49 PM   #11
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by savitrasapre View Post
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: ");
}
 
Old 02-18-2009, 02:51 AM   #12
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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:

exit();
}
}
getch();
fclose(fp);
return 0;
}
 
Old 02-18-2009, 05:01 AM   #13
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running code on boot as a limited user... itnaa Linux - Software 6 08-25-2007 04:52 PM
Running untrusted code cep21 Linux - Security 9 03-29-2006 12:33 PM
Loading object code into RAM and running it sto237 Linux - Software 1 01-27-2006 06:46 PM
running executable in C++ code psIpher Programming 2 11-25-2005 01:44 PM
Running C code in Redhat 7.2 Sifu-Rusty Linux - Newbie 2 03-14-2003 05:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration