Thread: Menu

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Menu

    Hello I wrote this program. But it prints out everything all in the same screen. I want the program to print out only the information for sydney only when the user enters 1 or the information for london only when the user enters 2. I don'nt know how to do it can someone please show me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 2
    
    typedef struct{
      float Fish;
      float Cotton;
     }Goods_g;
    
    typedef struct{
      char Sydney[10];  /*!! you need more that a char for a string */
      char London[10];
      char LosAngles[10];
      char NewYork[10];
      char Paris[10];
      char Tokyo[10];
    }Ports_p;
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[]);
    
    int London(Goods_g *G, Ports_p *P, float variables[]);
    
    int LosAngles(Goods_g *G, Ports_p *P, float variables[]);
    
    int NewYork(Goods_g *G, Ports_p *P, float variables[]);
    
    int  Paris(Goods_g *G, Ports_p *P, float variables[]);
    
    int Tokyo(Goods_g *G, Ports_p *P, float variables[]);
    
    int main()
    {
      Goods_g G;
      Ports_p P;       
    
      char name[A];
      int i;
     //int x=1000;       
      int j;
      float variables[SIZE];
    
      printf("Please enter your name: ");
      scanf("%s",name); /*!! No & here, name is an array */
    
      printf("Please select which pont you want to start in: ");
      scanf("%d",&i);
    
      if(i==1)
        printf("Welcome to Sydney\n");
    
      if(i==2)
        printf("Welcome to London\n");
        
    if(i==3)
    printf("Welcome to LosAngles\n");
    
    if(i==4)
    printf("Welcome to NewYork\n");
    
    if(i==5)
    printf("Welcome to Paris\n");
    
    if(i==6)
    printf("Welcome to Tokyo\n");
    
      srand((unsigned)time(NULL));
      for (j=0; j<2; j++)
        variables[j] = (rand()%26)/14.0f;
      strcpy( P.Sydney, "Sydney" );
      strcpy( P.London, "Lndon" );
      strcpy( P.LosAngles, "LosAngles");
      strcpy( P.NewYork, "NewYork");
      strcpy( P.Paris, "Paris");
      strcpy( P.Tokyo, "Tokyo");
      
     Sydney(&G, &P, variables);
      London(&G, &P, variables);
    LosAngles(&G, &P, variables);
    NewYork(&G, &P, variables);	
    Paris(&G, &P, variables);
    Tokyo(&G, &P, variables);
      
    
      return 0;
    }
    
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[])
    {
      G->Fish   = 50*variables[0];     
      G->Cotton = 80*variables[1];
    
      
    
      printf("Welcome to %s\n",P->Sydney);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
      
    
      return 0;
    }
    
    int London(Goods_g *G, Ports_p *P, float variables[])
    
    {
      G->Fish   = 50*variables[0];     
      G->Cotton = 80*variables[1];
    
    
    
      printf("Welcome to %s\n",P->London);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
      
    return 0;
    }
    
    int LosAngles(Goods_g *G, Ports_p *P, float variables[])
    {
    G->Fish   = 50*variables[0];       
    G->Cotton = 80*variables[1];
    
    
      printf("Welcome to %s\n",P->LosAngles);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
    
    return 0;
    }
    
    int NewYork(Goods_g *G, Ports_p *P, float variables[])
    {
    G->Fish   = 50*variables[0];       
    G->Cotton = 80*variables[1];
    
    
      printf("Welcome to %s\n",P->NewYork);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
      
      
    return 0;
    }
    
    int  Paris(Goods_g *G, Ports_p *P, float variables[])
    {
    G->Fish   = 50*variables[0];    
      G->Cotton = 80*variables[1];
    
      printf("Welcome to %s\n",P->Paris);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
    
    return 0;
    }
    
    int Tokyo(Goods_g *G, Ports_p *P, float variables[])
    {
    	G->Fish   = 50*variables[0];     
      G->Cotton = 80*variables[1];
    
    
      printf("Welcome to %s\n",P->Tokyo);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
    
      
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then why don't you put all of the related stuff together, instead of just sticking it all over the place?
    Code:
    if( i == 1 )
    {
        printf("Welcome to Sydney\n");
        strcpy( P.Sydney, "Sydney" );
        Sydney(&G, &P, variables);
    }
    ... and so on ...
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    JUst 1 more question

    Thank for the tip it worked. But the other question I had is what does the line "strcpy( P.Sydney, "Sydney");" do. I posted a question on this forum earlier and this line of code was new to me andconsidering I was in a hurry I forgot ro ask what it does. What is the function of the "strcpy" function.

  4. #4
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    strcpy will copy one string (array of chars) to another string (array of chars).

    char * strcpy ( char * dest, const char * src );
    Copy string.
    Copies the content pointed by src to dest stopping after the terminating null-character is copied.
    dest should have enough memory space allocated to contain src string.

    Parameters.

    dest
    Destination string. Should be enough long to contain string2.
    string2
    Null-terminated string to copy.
    Return Value.
    dest is returned.

    Portability.
    Defined in ANSI-C.

    Example.

    Code:
    /* strcpy example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str1[]="Sample string";
      char str2[40];
      char str3[40];
      strcpy (str2,str1);
      strcpy (str3,"copy successful");
      printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
      return 0;
    }
    Output:
    str1: Sample string
    str2: Sample string
    str3: copy successful


    Hope this helps.
    Chris
    Last edited by chriscolden; 01-13-2006 at 08:44 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's nice to see such a level of blind copy and pasting from previous posts.

    Think about what your ports structure would look like with hundreds of ports.
    Also think about the problem you would have adding and removing ports.

    Then look here
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 2
    
    typedef struct{
      float Fish;
      float Cotton;
    }Goods_g;
    
    typedef struct{
      char PortName[10];
    }Ports_p;
    
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[]);
    
    int main()
    {
      Goods_g G;
      Ports_p P[6] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
      char name[A];
      int i;
      int j;
      float variables[SIZE];
    
      printf("Please enter your name: ");
      scanf("%s",name);
    
      printf("Please select which port you want to start in: ");
      scanf("%d",&i);
    
      srand((unsigned)time(NULL));
      for (j=0; j<SIZE; j++)
        variables[j] = (rand()%26)/14.0f;
    
      if ( i >= 1 && i <= 6 ) {
        DisplayPort ( &G, &P[i-1], variables);
      }
    
      return 0;
    }
    
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[])
    {
      G->Fish   = 50*variables[0];
      G->Cotton = 80*variables[1];
    
      printf("Welcome to %s\n",P->PortName);
      printf("The prices for the material is as follows\n");
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
    
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Please explain

    I don'nt understand a few things in the above code I would be highly obliged if some can explain a few things to me.

    1.Why was'nt all the ports decleared here

    Code:
     
    
    typedef struct{
      char PortName[10];
    }Ports_p;
    2. What was the pourpose of this? Why is it like this? What does it do?

    Code:
     Ports_p P[6] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
    3. I don't understand the logic if the user was to enter 1 then it would be P[0] which does'nt exist.

    Code:
     if ( i >= 1 && i <= 6 ) {
        DisplayPort ( &G, &P[i-1], variables);
      }

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1) consider Salem's comment:
    >Think about what your ports structure would look like with hundreds of ports.
    2) it creates an array of 6 Ports_p and initializes them.
    3) in c/c++ array indexes go from 0 ... n-1
    Kurt

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Ports

    I did think about salem comments and after posting the last reply I came to the conclusion that he was right. It's just that the way ne decleared the ports was un known to me. So all I have to do to add new ports is that

    Code:
    Ports_p P[7] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
        { "Melbourne" },
      };
    I just did'nt know that you can do that.

    But for readbility's sake would'nt it be easier if the array of ports were decleared before.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by HAssan
    But for readbility's sake would'nt it be easier if the array of ports were decleared before.
    Yeah but you did not declare an array of ports you declared a struct where each field was the name of a port. This is not how one usually uses a struct.
    Normally structs are used to group related information of different type together like
    Code:
    struct port {
      char name[10];
      long capacity;
      ...
    };
    If you want to acces the individual elements of an arry via a symbolic name I would use an enum like
    Code:
    enum ports {
       Sydney=0,
       London,
       LosAngles,
       NewYork,
       Paris,
       Tokyo,
    };
    then you could do things like

    Code:
       struct port thePorts[6];
       char * name = thePorts[London].name;
    But that would give you the same problem with the enum that you had with your struct.
    Kurt
    Last edited by ZuK; 01-14-2006 at 04:25 PM.

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