beginner need help

This is a discussion on beginner need help within the C Programming forums, part of the General Programming Boards category; Code: void enterstring() { while(getchar()!='\n'); printf("a string:"); fgets(str,20,stdin); while(*str=='\n'||*str=='

Thread: beginner need help

') { printf("\t\tneed to enter a string:"); fgets(str,20,stdin); } } this code ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    beginner need help

    Code:
    void enterstring()
    {
                    while(getchar()!='\n');
    	printf("a string:");
    	fgets(str,20,stdin);
                    while(*str=='\n'||*str=='\0')
    	{
    		printf("\t\tneed to enter a string:");
    		fgets(str,20,stdin);
    	}
    }
    this code works fine if user enters nothing.
    but if user enters a string,
    2 'enter's are required to go to next line of code.
    the string and 1 '\n' is also assigned to 'str'.
    any help on how to reduce to 1 'enter' required and only stores the string?
    been trying out gets(str), scanf("%s",str)
    all have same or worse effect.
    any help pls?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    getchar() waits for input, so you have to press enter twice. i would suggest not using it.

    (if your next question is, "how do i check to see if the buffer is clear?" don't bother... there's no standard way to do it)

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    you could use getch() in place of getchar(), which won't make you press enter.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >you could use getch() in place of getchar(), which won't make you press enter.
    But mind it, getchar() is standard, while getch() isn't, your compiler may/maynot support it.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    think my compiler doesnt support getch()
    tried it but still cant work

    [code]void enterstring()
    {
    fflush(stdin);/*does this work better than getchar()?*/
    printf("\n\n\n\n\n\t\t\tenter a string:");
    fgets(str,20,stdin);
    while(*str=='\n'||*str=='\0')
    {
    printf("\t\tenter a ****ing string:");
    fgets(str,20,stdin);
    }
    }

    this code has same effect as previous.

    called this function from 2 places,
    one works normal
    this
    Code:
    void menuchoice(int i)
    {
    	switch(i)
    	{
    	case 2:
    		enterstring();
    		checkstring();
    		break;
    	}
    }
    whereas from this place requires additional enter
    Code:
    void startchoice()
    {
    	enterstring();
    	enterchar();
    	checkstring();
    }

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    for us to help you with your second question, you'd probably have to show the rest of your code.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    char *pt;
    int prt;
    int check_string(char *s);
    int is_in(char *s,char c);
    char str[20],chr;
    void startmenu();
    void startchoice();
    void enterstring();
    void enterchar();
    void checkstring();
    int checkchar();
    void menu();
    void menuchoice(int i);
    void strrmvlaschr(char *p);
    
    int main()
    {
    	startmenu();
    
    	return 0;
    }
    
    void startmenu()
    {
    	int i;
    
    	printf("\n\n\n\t\tString checker\n\n\n\n");
    	printf("\t1. Start\n\n");
    	printf("\t2. Exit\n\n");
    	printf("\tEnter a number to continue:");
    	scanf("%d",&i);
    	system("CLS");
    
    	if(i==1)
    	{
    		startchoice();
    	}
    	else
    		exit(0);
    }
    
    void menu()
    {
    	int i;
    
    	system("cls");
    	printf("\n\n\n\t\tString checker\n\n\n\n");
    	printf("\t1. Enter another string and char\n\n");
    	printf("\t2. Enter string ONLY\n\n");
    	printf("\t3. Enter char ONLY\n\n");
    	printf("\t4. Exit\n\n");
    	printf("\tEnter a number to continue:");
    	scanf("%d",&i);
    	system("CLS");
    
    	menuchoice(i);
    }
    
    void startchoice()
    {
    	enterstring();
    	enterchar();
    	checkstring();
    }
    
    void menuchoice(int i)
    {
    	switch(i)
    	{
    	case 1:
    		startchoice();
    		break;
    	case 2:
    		enterstring();
    		checkstring();
    		break;
    	case 3:
    		printf("\n\n");
    		enterchar();
    		checkstring();
    		break;
    	default:
    		exit(0);
    	}
    }
    
    int check_string(char *s)
    {
    	if(*s)
    		return 1;
    	else
    		return 0;
    }
    
    int is_in(char *s,char c)
    {
    	while(*s)
    	{
    		if(*s==c)
    		{
    			prt=s-pt;
    			return 1;
    		}
    		else
    		{
    			s++;
    		}
    	}
    	return 0;
    }
    
    void enterstring()
    {	
    	fflush(stdin);
    	printf("\n\n\n\n\n\t\t\tenter a string:");
    	fgets(str,20,stdin);
    	while(*str=='\n'||*str=='\0')
    	{
    		printf("\t\tenter a ****ing string:");
    		fgets(str,20,stdin);
    	}
    	pt=str;
    }
    
    void enterchar()
    {
    	while(getchar()!='\n');
    	printf("\n\t\t\tenter a char:");
    	scanf("%c",&chr);
    	while(chr=='\n')
    	{
    		printf("\n\t\t\tenter a ****ing char:");
    		scanf("%c",&chr);
    	}
    }
    
    void checkstring()
    {
    	strrmvlaschr(str);
    	if(is_in(str,chr))
    	{
    		printf("\n\t\t'%c' is located in \"%s\" at postion %d.\n\n",chr,str,prt+1);
    		for(long int t=0;t<90000000;t++);
    		menu();
    	}
    	else
    	{
    		printf("\n\t\t**** u '%c' no found.\n\n",chr);
    		for(long int t=0;t<90000000;t++);
    		menu();
    	}
    }
    
    void strrmvlaschr(char *p)
    {
    	p=str;
    	while(*p)
    	{
    		if(*p=='\n')
    			*p='\0';
    		p++;
    	}
    }
    int checkchar()
    {
    	if(chr=='\n')
    		return 0;
    	else return 1;
    }
    thanks for time and help

  8. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here

    to pause the code till enter key is pressed, use this function:

    Code:
    void waitforenterkeypress()
    {
    	char c;
    
    	do
    	{
    	} while((c = getch()) != 13); 	
    }
    getch is in conio.h

  9. #9
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Ok, it looks like it requires pressing enter an extra time because of how you go into startchoice(). I'm not familiar with scanf(). If it requires you to press enter, I would say that's probably the reason.
    Code:
    void startmenu()
    {
    	int i;
    
    	printf("\n\n\n\t\tString checker\n\n\n\n");
    	printf("\t1. Start\n\n");
    	printf("\t2. Exit\n\n");
    	printf("\tEnter a number to continue:");
    	scanf("%d",&i);
    	system("CLS");
    
    	if(i==1)
    	{
    		startchoice();
    	}
    	else
    		exit(0);

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    thx for time i try to play around with codes

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >thx for time i try to play around with codes
    This works, I changed a few names to avoid conflicts with the implementation, removed unused functions, and cleaned things up a bit. Past that, the biggest change was adding the FLUSH macro to deal with scanf:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define FLUSH while ( getchar() != '\n' );
    
    static char *pt;
    static int prt;
    static char str[20], chr;
    
    static int isIn(char *s,char c);
    static void startchoice(void);
    static void enterstring(void);
    static void enterchar(void);
    static void checkstring(void);
    static void menu(void);
    static void menuchoice(int i);
    static void rmvlaschr(char *p);
    
    int main(void)
    {
      menu();
      return 0;
    }
    
    void menu(void)
    {
      int i;
      
      system("cls");
      printf("\n\n\n\t\tString checker\n\n\n\n");
      printf("\t1. Enter another string and char\n\n");
      printf("\t2. Enter string ONLY\n\n");
      printf("\t3. Enter char ONLY\n\n");
      printf("\t4. Exit\n\n");
      printf("\tEnter a number to continue:");
      scanf(" %d",&i);
      FLUSH;
      system("CLS");
      
      menuchoice(i);
    }
    
    static void startchoice(void)
    {
      enterstring();
      enterchar();
      checkstring();
    }
    
    static void menuchoice(int i)
    {
      switch(i)
      {
      case 1:
        startchoice();
        break;
      case 2:
        enterstring();
        checkstring();
        break;
      case 3:
        printf("\n\n");
        enterchar();
        checkstring();
        break;
      default:
        exit(0);
      }
    }
    
    static int isIn(char *s,char c)
    {
      while(*s)
      {
        if(*s==c)
        {
          prt=s-pt;
          return 1;
        }
        else
        {
          s++;
        }
      }
      return 0;
    }
    
    static void enterstring(void)
    {	
      printf("\n\n\n\n\n\t\t\tenter a string:");
      fgets(str,20,stdin);
      while(*str=='\n'||*str=='\0')
      {
        printf("\t\tenter a ****ing string:");
        fgets(str,20,stdin);
      }
      pt=str;
    }
    
    static void enterchar(void)
    {
      printf("\n\t\t\tenter a char:");
      chr = (char)getchar();
      FLUSH;
      while(chr=='\n')
      {
        printf("\n\t\t\tenter a ****ing char:");
        scanf("%c",&chr);
      }
    }
    
    static void checkstring(void)
    {
      rmvlaschr(str);
      if(isIn(str,chr))
      {
        printf("\n\t\t'%c' is located in \"%s\" at postion %d.\n\n",chr,str,prt+1);
        getchar();
        menu();
      }
      else
      {
        printf("\n\t\t**** u '%c' no found.\n\n",chr);
        getchar();
        menu();
      }
    }
    
    static void rmvlaschr(char *p)
    {
      p=str;
      while(*p)
      {
        if(*p=='\n')
          *p='\0';
        p++;
      }
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21