Modular Programming Help

This is a discussion on Modular Programming Help within the C Programming forums, part of the General Programming Boards category; Hello, just wondering if anyone could help with me a piece of code Code: #include <stdio.h> #include <ctype.h> void step1() ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Modular Programming Help

    Hello, just wondering if anyone could help with me a piece of code

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void step1()
    {
        printf("You have 3 choices\n");
        printf("Enter a for Student Name\n");
        printf("Enter b for Tutorial Time\n");
        printf("Enter a number to display positives\n");
        printf("Press q to quit\n");
    
        scanf("%c", &letter);
        printf("Input = %c\n", letter);
        return;
    }
    
    
    void step2()
    {
    
        switch(response)
    	  {
    		case 'a':
    		  printf("Name\n");
    		  break;
    
    		case 'b':
    		  printf("Date\n");
    		  break;
    
    		case 'c':
    
    
    		case 'q':
    		  break;
    
    		default:
    		  printf(“You haven't selected a valid option.\n”);
    		}
    
    	} while(response != 'q');
    
    }
    
    
    int main()
    {
        char letter
    
        step1();
    
       return(0);
    
    }

    I need to display a menu then once the user inputs an option it goes on to step 2. Step 2 is a switch case scenario. Just wondering if I am on the right track?

    also the scanf in step 1 doesn't work for some reason :/ it keeps coming up with an error.

    Don't worry about case 'c' and such yet, I haven't started that section
    Last edited by DJ_Steve; 09-08-2009 at 11:42 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to use a variable you must declare it. You have no variables declared in your step1 function, so you have no variables to use. That's the big rub with what you've got -- you need to get the variables passed along between the main and all the functions.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ack, I thought that would be the problem but when I tried it, it still didn't work.

    I just realised I missed out the ; at the end of it last time!

    it works now, thanks

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    Can anyone give me any tips on a link to a tutorial that would enable me to

    "Read in a positive number between 1-50 and display all numbers from 0 up to the number entered. E.g. if the user enters 5, the program will display 0 1 2 3 4 5. "

    The only thing I know about this is that you have to use count...etc

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    It's a simple program. Follow these steps-
    1. Input any integer using scanf.
    2. Run a loop starting from 0 to that number.
    3. In the body of the loop use printf to print the variable(for eg i) in the for loop.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    With letter declared in main, you may want to pass the address of letter as a argument of function step1().
    Prototype:
    Code:
    void step1(char letter)
    or declare char letter within the function and return a char
    Code:
    char step1( void )

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ok, this is what I have so far...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void step1()
    {
        char letter;
    
        printf("You have 3 choices\n");
        printf("Enter a for Student Name\n");
        printf("Enter b for Tutorial Time\n");
        printf("Enter a number to display positives\n");
        printf("Press q to quit\n");
    
        scanf("%c", &letter);
        printf("Input = %c\n", letter);
        printf("\n\n");
        return;
    }
    
    
    void step2()
    {
        char letter;
    
        {
        switch(letter)
        {
    		case 'a':
    		  printf("Name\n");
    		  printf("\n\n");
              break;
    
            case 'b':
    		  printf("Date\n");
    		  printf("\n\n");
    		  break;
    
    		case 'c':
    		  step3();
              break;
    
        }
    
    	} while(letter != 'q');
    
    	return;
    
    }
    
    void step3()
    {
    
        int value
        int num
    
        scanf("%d", &num);
        while(value <= num)
            {
            printf("%d", value);
            value +1;
            }
    
        printf(“\n”);
    
        return;
    
    }
    
    
    int main()
    {
        step1();
        step2();
    
       return(0);
    
    }

    There is a problem with Void Step 3 though...I wanted to move the 3rd option into each own segment but it keeps coming up with an error?

    How is my loop? Will it work?

    Thanks

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I think that 2nd posting of code confused us even more the specs on this.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    lol

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Quote Originally Posted by DJ_Steve View Post
    Can anyone give me any tips on a link to a tutorial that would enable me to

    "Read in a positive number between 1-50 and display all numbers from 0 up to the number entered. E.g. if the user enters 5, the program will display 0 1 2 3 4 5. "

    The only thing I know about this is that you have to use count...etc
    Code:
    #include <stdio.h>
    
    int main()
    {
            int number, i ;
    
            printf("Enter a number: ") ;
            scanf("%d", &number) ;
    
            if ( number >= 0 && number <= 50 )
                    for ( i = 0 ; i <= number ; i++ )
                            printf("%d ", i );
            else
                    printf("Invalid number!") ;
    
            printf("\n") ;
            return 0 ;
    }
    /*
    Enter a number: 20
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    
    Enter a number: 99
    Invalid number!
    */

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    wow, you seemed to ease through that, thanks!

    my loop was way off

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Is this last snippet relevant to the 3-step program you're working on?

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    hmm, how would I go back to loop 1 after the person has selected an option?

    I know that return; will go back to int main() so I replaced break; with return;

    the problem is that, it goes back to Step 1 (e.g displays the menu) but then doesn't allow any input

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    last snippet?

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Quote Originally Posted by DJ_Steve View Post
    hmm, how would I go back to loop 1 after the person has selected an option?

    I know that return; will go back to int main() so I replaced break; with return;

    the problem is that, it goes back to Step 1 (e.g displays the menu) but then doesn't allow any input
    Here's a cheesy example of how I've been coding simple menus recently

    Code:
    #include <stdio.h>
    int main()
    {
    
            int MenuOptions() ;
            int choice ;
    
            while ( ( choice = MenuOptions() ) != 4 )
            {
                    switch(choice)
                    {
                            case 1:
                                    printf("You have chosen choice alpha!\n") ;
                                    break ;
    
                            case 2:
                                    printf("You have chosen choice bravo!\n") ;
                                    break ;
    
                            case 3:
                                    printf("You have chosen choice charlie!\n") ;
                                    break ;
    
                            default:
                                    break ;
                    }
            }
            return 0 ;
    }
    
    int MenuOptions()
    {
            int x ;
    
            printf("1. Choice Alpha\n") ;
            printf("2. Choice Bravo\n") ;
            printf("3. Choice Charlie\n") ;
            printf("4. Quit\n") ;
            printf("Select> ") ;
            scanf("%d", &x) ;
    
            return x ;
    }
    
    /* EXECUTION OUTPUT
    
    1. Choice Alpha
    2. Choice Bravo
    3. Choice Charlie
    4. Quit
    Select> 0
    1. Choice Alpha
    2. Choice Bravo
    3. Choice Charlie
    4. Quit
    Select> 1
    You have chosen choice alpha!
    1. Choice Alpha
    2. Choice Bravo
    3. Choice Charlie
    4. Quit
    Select> 2
    You have chosen choice bravo!
    1. Choice Alpha
    2. Choice Bravo
    3. Choice Charlie
    4. Quit
    Select> 3
    You have chosen choice charlie!
    1. Choice Alpha
    2. Choice Bravo
    3. Choice Charlie
    4. Quit
    Select> 4
    
    */

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modular c programming
    By akrlot in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 07:00 AM
  2. Modular
    By loopshot in forum Game Programming
    Replies: 7
    Last Post: 01-20-2006, 06:24 PM
  3. Modular math
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-01-2003, 08:35 AM
  4. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM
  5. Completely modular motherboards?
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-23-2003, 11:56 AM

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