Thread: beginner help!!!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Talking beginner help!!!

    Hi everyone. I'm new to c++ and thought i would come to the experts for some advice. Im' having a bit of trouble with a switch statement problem. If anyone could help it would be much appreciated. I am using a switch statement to make a choice. I then need to go to the same switch statement to make the choice again but save the answer at a different address. I've put an extract from the code below as i appreciate i'm not the best at explaining it. If the technical talk could be kept to a minimum aswell that would be great as my tiny little brain can't cope Thanks for the help.
    The first one uses the address leaving=

    scanf ("%d", &leaving);


    switch (leaving)
    {
    case 1:
    system ("CLS");
    printf("\nYou have selected : ");
    printf(ash);
    break;
    case 2:
    system ("CLS");
    printf("\nYou have selected : ");
    printf(brent);
    break;
    etc,


    then i need to use the switch statement again but the scanf i need to use is=

    scanf("%d", &arriving);

    which obviously doesn't got to the switch statement which is initialised with 'leaving'

    Sorry for the poor explanation but any help would be greatly appreciated.

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    What you'd probably like is to use a function. Have you learned about functions yet?

    A function is a subroutine, and you can pass a variable in. It could look something like this:

    Code:
    int main ( void )
    {
    	int leaving, arriving;
    	
    	...some code...
    	
    	choice_selection_function(leaving);
    	
    	...some code....
    	
    	choice_selection_function(arriving);
    }
    
    void choice_selection_function ( int var )
    {
    	switch (var)
    	{
    		case 1:
    			system ("CLS");
    			printf("\nYou have selected : ");
    			printf(ash);
    			break;
    		case 2:
    			system ("CLS");
    			printf("\nYou have selected : ");
    			printf(brent);
    			break;
    	}
    }
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Hi
    Thanks very much for your reply. Its very appreciated.
    I've only had very limited use of functions so its still a little bit lost on me. Sorry but i'm a bit lost with what you've put there. I see you've set the switch statement with (var). Then you have
    choice_selection_function(leaving); and choice_selection_function(arriving); Are these functions that are set up elsewhere?
    I'm also not sure where the void comes into it as i haven't used that before. Sorry but i've just started and its been a steep learning curve. I've put the whole section of code in so you can get a better idea of what i'm trying to do.

    Code:
                                                         
               printf("\nEnter the station you will be leaving from by \nentering the corresponding number from the list below");
               printf("\n 1 = Ashford\n 2 = Brentworth\n 3 = Canonbury Cross\n 4 = Dowgate\n 5 = Edbury\n");
               printf(" 6 = Fenchurch Street\n 7 = Gresham\n 8 = Hampstead\n 9 = Islington\n 10 = Jamaica Road\n");
               scanf ("%d", &leaving);
               
      
              switch (leaving)
              {
                     case 1:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ash);
                          break;
                     case 2:
                          system ("CLS"); 
                          printf("\nYou have selected : ");
                          printf(brent);
                          break;
                     case 3:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(can);
                          break;
                     case 4:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(dow);
                          break;
                     case 5:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ed);
                          break;
                     case 6:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(fen);
                          break;
                     case 7:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(gres);
                          break;
                     case 8:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ham);
                          break;
                     case 9:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(isl);
                          break;
                     case 10:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(jam);
                          break;
                     
              }
              
              
              
              printf("  as your leaving station\n");
              
              
              do
              {
              
              printf("\nEnter the station you will be arriving at by \nentering the corresponding number from the list below");
              printf("\n 1 = Ashford\n 2 = Brentworth\n 3 = Canonbury Cross\n 4 = Dowgate\n 5 = Edbury\n");
               printf(" 6 = Fenchurch Street\n 7 = Gresham\n 8 = Hampstead\n 9 = Islington\n 10 = Jamaica Road\n");
              scanf("%d", &arriving);
              
                    
              
              
              switch (arriving)
              {
                     case 1:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ash);
                          break;
                     case 2:
                          system ("CLS"); 
                          printf("\nYou have selected : ");
                          printf(brent);
                          break;
                     case 3:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(can);
                          break;
                     case 4:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(dow);
                          break;
                     case 5:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ed);
                          break;
                     case 6:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(fen);
                          break;
                     case 7:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(gres);
                          break;
                     case 8:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(ham);
                          break;
                     case 9:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(isl);
                          break;
                     case 10:
                          system ("CLS");
                          printf("\nYou have selected : ");
                          printf(jam);
                          break;   
              }
              
              printf(" as your arriving station.\n\n");
              
              
              }while (leaving == arriving);
    As you can see it should be possible to use the one switch statement for both selections as the choices are the same. Thanks again.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    I think the following tutorial would help you out:

    Cprogramming.com Tutorial: Functions

    Here's an explanation of what I did:

    I wrote a function, which is a subroutine. It allows me to use some kind of functionality that I often use more than once in my program. Subroutines allow me to pass in "parameters". Essentially, when I call a subroutine, I can give it something different everytime.

    For example, we could have a subroutine called EatLunch, and we can pass in a parameter like Pizza, Sandwidch, Salad, or Cake. It could look like this:

    Code:
    EatLunch ( Pizza );
    EatLunch ( Salad );
    Subroutines can also return things to the main flow of the program. For example, after I eat lunch, I might get indigestion

    Code:
    bool DoIHaveIndigestion = EatLunch ( Pizza );
    When you define a function, it needs three things: a return type, a name, and a list of parameters. You can define a function like this:

    Code:
    bool EatLunch ( FoodType type )
    {
       ...code...
    }
    Now go and apply that
    My Website

    "Circular logic is good because it is."

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thanks very much for that. I have lots more to do with the program so no dought i'll be back on asking for advice. I'll be sure to look through that tutorial. Tanks for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 02:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. 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
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM