Thread: Menu

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    Exclamation Menu

    I need to make a menu for my program that links to the following functions that I already put in other files but the menu needs to go back to it after each function finishes.

    char *StrChr(char *str, int ch);
    char *StrRChr(char *str, int ch);
    int StrSpn(char *str, char *set);
    int StrCSpn(char *str, char *set);

    Only thing is i'm only allowed to use #include <stdio.h>
    And it must be written in all C. So if someone could either write it or walk me through writting it by Nov. 2 that would be great. Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define W switch(Y){\
    case'_':/*functioncall*/break;\
    case'_':/*functioncall*/break;\
    case'_':/*functioncall*/break;\
    case'_':/*functioncall*/break;}
    #define x goto
    #define X printf(
    #define z );
    #define Z Y=fgetc(stdin);while(fgetc(stdin)!='\n');
    hey:
    X">"z
    Z
    x buddy;
    here:
    X"Pick an option:\n"z
    X") Something.\n"z
    x your;
    is:
    X") In.\n"z
    x homework;
    your:
    X") Goes.\n"z
    x is;
    homework:
    X") Here.\n"z
    x  hey;
    buddy:
    W
    Hm.. I haven't compiled it, but I think that should do what you want. Of course, it's slightly incomplete code. You'll need to do some simple stuff like change the underscores to valid input, and add some numbers to the choice lines to match.

    Simple stuff really.
    [edit]
    Oh, add the following two lines right after the last #define

    int Y;
    x here;
    [/edit]

    Let me know what grade you get.

    Quzah.
    Last edited by quzah; 10-31-2002 at 09:58 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    that's only in c right?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Krush
    that's only in c right?
    Hehe. Yeah. That's C.

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

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    That wasn't the homework the hard part was implementing those 4 codes.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    can you please explain what each of those things are doing? I've never programmed before.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    Actual Assignment

    Assignment 1:

    In this assignment, you are going to implement the functions whose
    declarations are specified below. I am also providing a brief
    description of each function to avoid any confusion.

    Function 1: char *StrChr(char *str, int ch);
    Function 2: char *StrRChr(char *str, int ch);
    Function 3: int StrSpn(char *str, char *set);
    Function 4: int StrCSpn(char *str, char *set);

    -----------------------------------------------------------------

    Descriptions:

    The function StrChr search the string str for the first occurrence
    of the character ch. If the character ch is found in the string, a
    pointer to the first occurrence is returned. If the character is not
    found, a null pointer is returned.

    The function StrRChr is like StrChr except that it returns a pointer
    to the last occurrence of the character ch. If the character is not
    found, a null pointer is returned.

    Important Note:
    Consider the terminating null character of str to be a part of the
    string. That is, if ch is the null character '\0', the functions will
    return the position of the terminating null character of str.

    Look at the following fragment of code to understand the behaviour
    of the above two functions:

    char *teststring = "Here is a string of text";
    char *textptr;

    /* find first 's' in teststring */
    if ( (textptr = StrChr(teststring, 's')) != NULL )
    printf("%s\n", textptr);

    /* find last 's' in teststring */
    if ( (textptr = StrRChr(teststring, 's')) != NULL )
    printf("%s\n", textptr);

    The results displayed by the printf statement must be:

    s a string of text
    string of text

    ------------------------------------------------------------------------

    The functions StrSpn and StrCSpn count characters: the first StrSpn
    counts the number of consecutive characters in the first argument
    str (which is null-terminated) that consist entirely of character
    specified in the second argument string set (which is also null-
    terminated). Counting begins with the first character in the string.

    StrCSpn works like StrSpn except that the sense of how the characters
    in the second argument string set are interpreted is reversed. That
    is, starting from the beginning of the first character string argu-
    ment, StrCSpn counts the number of consecutive characters in the
    string that consist of characters NOT included in the second argument
    string.

    Look at the following fragment of code to understand the behaviour
    of the two functions:

    char *teststr = "self-explanatory program";
    char *teststr2 = "123 is the number";
    int count;

    /* count number of initial lowercase letters */
    count = StrSpn(teststr, "abcdefghijklmnopqrstuvwxyz");
    printf("%d\n", count);

    /* count number of initial digits */
    count = StrSpn(teststr2, "0123456789");
    printf("%d\n", count);

    /* count number of initial characters up to a space */
    count = StrCSpn(teststr, " ");
    printf("%d\n", count);

    The results displayed by the printf statement must be:

    4
    3
    16

    -------------------------------------------------------------------

    Description of Program:

    When I execute your program, I must get a menu (similar to the one
    that was implemented in my very first email) that allows me to first
    choose which of the above four functions I would like to implement,
    second it must allow me to input the first argument of the function
    I just chose, and finally it must allow me to input the second argu-
    ment of the function (for the first two functions, I should be able
    to enter a character, while for the last two functions, I must be
    able to enter a string of characters).

    After displaying the correct information from the data I have just
    entered, your program must again go back to the menu. Also note that
    if the user simply wants to quit from your program, he/she must be
    able to simply type in either 'q' or 'Q' to quit the application.

    -----------------------------------------------------------------------

    Grading:

    70% of grades will be for correctness of the four functions.

    20% of grades will be for a robust implementation of the menu system.
    Robust here means that if you have 5 choices in your menu (4 to choose
    the function and the fifth to quit your application) then the only
    characters that must make any sense to the menu must be '1', '2', '3',
    '4', '5', 'q' and 'Q'. If I type in anything else, the menu system must
    prompt again for one of the five choices.

    10% of grades will be for cleanliness, neatness, readability and
    documentation of your code. Cleanliness, neatness, and readability imply
    that your program makes good use of white space and is easy on the eyes
    and is easy to read. Documentation aids in understanding your algorithm
    and the code you have written to implement that algorithm.

    -----------------------------------------------------------------------

    Due Date:

    By midnight 11/01/02


    ----------------------------------------------------------------------

    Important Notes:

    1. I will accept assignments only via email. You should place your source
    code and any other relevant files (readme.txt for instance which might
    contain instructions on how to run your programs) in a folder. The name
    of your folder MUST only contain the digits that make up your school id.

    2. The subject of the email must simply say:

    "CS120A: Assignment 1"

    or

    "CS120B: Assignment 1"

    depending on whether you are in section A or B.

    3. I will accept late assignments under the following formula: Every week
    that you are late with your assignment will mean that you loose 10% of
    the grade for that assignment.

    4. This assignment is NOT a group project. This is meant to be implemented
    by each student individually. Consulation among students is ok, but there
    has to be no sharing of source code. A big motivation for every student
    to implement the assignment individually is that some of the methods used
    in solving the assignment will turn up in forthcoming quizzes and exams.

    5. NO Standard Library functions are to be used in your assignment except
    for input/output functions such as printf, scanf, etc.

    6. I WILL NOT grade any assignment that contains a .cpp file or any other
    references to C++. This is an assignment that MUST be implemented in C.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    My teacher really sucks so if anyone has free time and could do this before tomorrow night. And explain to me what's going on, that would be great. And I going to make a new post requesting tutoring from people on the site.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Krush
    That wasn't the homework the hard part was implementing those 4 codes.
    Yes it was. Can't you read?

    When I execute your program, I must get a menu (similar to the one
    that was implemented in my very first email) that allows me to first
    choose which of the above four functions I would like to implement,

    second it must allow me to input the first argument of the function
    I just chose, and finally it must allow me to input the second argu-
    ment of the function (for the first two functions, I should be able
    to enter a character, while for the last two functions, I must be
    able to enter a string of characters).
    Furthermore, it sounds like they've already have shown you how to make a menu.

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

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Ya but I dont think he wants me copying his code. And I'd like to know why it does what it does. Right now I have this:

    Code:
    #include <stdio.h>
    #include "Functions.h"
    
    char Name;
    char *str;
    char *set;
    int ch;
    
    int Menu()
    {	
    	printf("Which function to run?\n\n");
    	printf("1. Function 1\n");
    	printf("2. Function 2\n");
    	printf("3. Function 3\n");
    	printf("4. Function 4\n\n");
    	printf("Your pick: ");
    		
    	scanf("%c", &Name);
    	
    	if(Name == '1')
    	{
    		Func1();
    	}
    //	else if(Name == '2')
    //	{
    //		Func2();
    //	}
    //	else if(Name == '3')
    //	{
    //		Func3();
    //	}
    //	else if(Name == '4')
    //	{
    //		Func4();
    //	}
    	else if(Name == 'Q')
    	{
    		return 0;
    	}
    	else if(Name == 'q')
    	{
    		return 0;
    	}
    	
    
    	return 0;
    }
    for some reason it isn't working, I get a error after choosing selection 1

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Code:
    --------------------Configuration: CS120 - Win32 Debug--------------------
    Compiling...
    StrCSpn.c
    Linking...
    LINK : LNK4073: cannot create map for .ILK file; linking nonincrementally
    StrSpn.obj : error LNK2005: _StrSpn already defined in StrCSpn.obj
    StrSpn.obj : error LNK2005: _i already defined in StrCSpn.obj
    Debug/CS120.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.
    
    CS120.exe - 3 error(s), 0 warning(s)

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Code:
    #include <stdio.h>
    #include "Functions.h"
    
    char *str;
    char *set;
    int i;
    int counter;
    
    int StrCSpn(char *str, int ch)
    {
    	long nc;
    
    	nc = 0;
    	while(getchar() != 0)
    		++nc;
    
    	i = 0;
    	while(i < nc)
    	{
    		if(str != set)
    			counter++;
    		
    		i++;
    	}
    
    	return counter;
    }
    I changed it to that now i get this:
    Code:
    --------------------Configuration: CS120 - Win32 Debug--------------------
    Compiling...
    StrCSpn.c
    C:\Program Files\Microsoft Visual Studio\MyProjects\CS120\StrCSpn.c(10) : warning C4028: formal parameter 2 different from declaration
    Linking...
    LINK : LNK4073: cannot create map for .ILK file; linking nonincrementally
    
    CS120.exe - 0 error(s), 1 warning(s)

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Code:
    #include <stdio.h>
    #include "Functions.h"
    
    char *str;
    int ch;
    int i;
    
    char* StrChr(char *str, int ch)
    { 
         for(i = 0; str[i] != 0; i++) 
              if(str[i] == ch) 
                   return &str[i]; 
         return 0; 
    }
    
    char line[1024];
    char letter;
    char *search;
    
    void Func1()
    {	
    	printf("Input a sentence:\n"); 
        scanf("%s", line);
        printf("\n"); 
        printf("Enter what you wish to search for:\n");
        scanf("%c", &letter);
        printf("\n");
        if( (search = StrChr(line, letter)) != NULL )
            printf("Your input's first occurrence:\n%s\n\n", search);
        else
    	{
            printf("Input not found.\n\n");
    		Menu();
    	}
    }
    It keeps skipping the second input. Any ideas?

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    How about using getchar? How would i do that for the second part?

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    right now the way you put it the function will never find nething that matches.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM