Thread: a menu system with string functions

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    a menu system with string functions

    Well, I am at it again. I am making another menu-driven system with a bunch of functions that does things to strings. I am set on the first two options, but I am having trouble with option 3. You will see it as the function occurrence. Here is the code. The problem specifically is that it wont let me input the string in the function.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    char str[250];
    
    /*Determines if the input string is a palindrome*/
    void palindrome(){
    
    char rev[250];
    
    strcpy(rev,str);
    strrev(rev);
    
    if (*str==*rev){
      printf("\nYes. The string is a palindrome\n\n");}
    else{
      printf("\nNo. The string is not a palindrom\n\n");}
    
    }
    
    /*Determines the number of words in the input string*/
    void number_words(){
    
    int words = 1;
    char *w;
    
        for (w=str; *w != '\0'; w++) {
             if (*w==' ')
               words++;
        }
      printf("\nThe number of words is %d\n\n", words);
    }
    
    
    /*With another string, it determines how many times the new string word exists in the original input string*/
    int occurrences(){
    
    int *p,*s,*b, occur;
    char occ[250];
    
    printf("Enter a word:");
    gets(occ);
    printf("\n");
    
    for (s=str; *s != '\0'; s++){
        b = strstr(s, occ);
      if (*b != 250){
         occur++;
         b = strstr(b+1, occ);
            if (*b != 250){
               occur++;
            }
            else
              break;
      }
      else
        break;
    }
    
    printf("The number of times occurred is %d\n\n", occur);
    
    }
    
    /*With yet another string, it determines if this input string exists a whole in the original input string*/
    void substring(){
    
    }
    
    /*displays the menu, select choice*/
    void menu()  {
    int a;
    
    printf("1.	Check if the string is a palindrome\n"
           "2.	Number of words in a string\n"
           "3.	Number of occurrences of input word\n"
           "4.	Substring test\n"
           "5.	Exit\n"
           "Enter an option: ");
    
    scanf ("%d", &a);
    
    switch (a)
      {
        case 1: palindrome(); break;
        case 2: number_words(); break;
        case 3: occurrences(); break;
        case 4: substring(); break;
        case 5: exit(0);;
      }
    }
    /*prints the menu and applies the entire menu through an infinite loop until exit is selected*/
    int main()
    {
    
    printf("Enter a string:");     /*gets the string from the user*/
    gets(str);
    fflush (stdin);
    printf("\n");
    
    while(1) {
          menu();
    }
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    I realize I forgot to clarify what the function is doing. The user inputs a second string. the first string is inputed when the program starts. The second string is then used to search through the first one for occurrences of the second string inside the first string. The occurences only applies to full words, so I think I have part missing.

    e.g. string 1 = AAA BBB
    string 2 = AA

    0 occurences....

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    I know I am talking to myself, but when someone can help me, this will help them.

    I changed the function to this for now, although it still doesnt work:

    Code:
    int occurrences(){
    
    int *s,*b, occur, n=1;
    char occ[250];
    
    printf("Enter a word:");
    fflush (stdin);
    gets(occ);
    printf("\n");
    
    s=str;
    
    b=strstr(s,occ);
    
     if (b !='\0'){
      occur++;
      while(b !='\0'){
        b=strstr(b+n,occ);
        occur++;
        n++;
      }
     }
     else{
      printf("The word does not occur.");}
    printf("The number of times occurred is %d\n\n", occur);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't let this get to you, but that is horrible code. Here's why:
    Code:
    int occurrences(/*1*/){
    
    int *s,*b, /*2*/ occur, n=1;
    char occ[250];
    
    printf("Enter a word:");
    /*3*/fflush (stdin);
    /*4*/ gets(occ);
    printf("\n");
    
    /*5*/s=str;
    
    b=strstr(s,occ);
    
     if (b !='\0'){
      /*6*/occur++;
      while(b !='\0'){
        b=strstr(b+n,occ);
        occur++;
        n++;
      }
     }
     else{
      printf("The word does not occur.");}
    printf("The number of times occurred is %d\n\n", occur);
    }
    /*1*/ If you dont' want the function to take any parameters, make it a void function. Empty parameters is just bad form.

    /*2*/ You haven't initialized this variable so...

    /*3*/ Never flush input streams. See the FAQ (This has finally made it into the FAQ, right?) or search the board for reasons why. Simply search for "fflush(stdin)" and you should get ample descriptions of why this is wrong.

    /*4*/ Never use gets. This is in the FAQ.

    /*5*/ Anything starting with str is a reserved keyword. Plus you're using a global here, at least I'm assuming you are, which makes your code hard to debug when you are simply giving one function to look at, all of which it references isn't included.

    /*6*/ ...it gives you wierd problem because of point #2 up above. You have no idea what value this variable is going to have in it.

    The rest I'll ignore for now, until you fix some of the problems. Perhaps someone else will care to continue where I left off.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're thinking too hard:
    Code:
    void occurrences()
    {
      char *s;
      int occur = 0;
      char occ[250];
    
      printf("Enter a word:");
      fflush(stdout);
      gets(occ);
      s = str;
      while ((s = strstr(s, occ)) != NULL){
        occur++;
        s++;
      }
      printf("The number of times occurred is %d\n\n", occur);
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Prelude, that wont let me type in anything....

    quzah, I had never used std's before, and we were given the stdin statement to use. Stupid TAs....
    Last edited by pxleyes; 04-08-2004 at 09:12 PM.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    um,. I just changed it to stdin on your function prelude and it started to let me type. I dont know what exactly is going on with stdin, but it works...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just changed it to stdin on your function prelude and it started to let me type
    Great. Replace a correct and necessary construct with an undefined construct that logically does something completely different because you unwisely mixed scanf and gets. I'm pretty sure we've told you about that before, and how to fix it.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    I have never been taught stdin or stdout statements. I am just saying it works, and I dont have a clue why.

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    stdin and stdout are streams
    functions (like fgets,fputc, etc...) operate on streams, stdin is the standard input stream. stdout is the standard output stream, they're just macros, defined in stdio.h

    the fflush function empties the buffer out into the stream, now that you know the defination it doesnt sound like the proper thing to use on a input stream? they only reason it might work for you is because your compiler was smart enough to do what you mean and not what you say.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    then why when I use stdout it just doesn't let me type when i need to give input?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    stdout is an output stream. That means, it outputs or displays, information. (Depending on how you're using it.)
    stdin is an input stream. That means is reads information in on the standard device used for input. (Usually the keyboard!)

    You do not read input on an output stream.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM