can anyone tell me whats wrong

This is a discussion on can anyone tell me whats wrong within the C Programming forums, part of the General Programming Boards category; Hi! can anyone tell me whats wrong with this: Code: #include <stdio.h> struct com {char brand [80]; float price; char ...

  1. #1
    Unregistered
    Guest

    can anyone tell me whats wrong

    Hi!
    can anyone tell me whats wrong with this:
    Code:
    #include <stdio.h>
    struct com {char brand [80];
                float price;
                char colors;
                char spec [100];
                int gur; };
    
    int main ()
    {   int i; float min;
    struct com computers[20];
    computers[0].brand="Compaq";
    computers[1].brand="IBM";
    computers[2].brand="MAC";
    computers[3].brand="Apple";
    computers[4].brand="Dell";
    computers[5].brand="ACER";
    computers[0].colors='W';
    computers[1].colors='B';
    computers[2].colors='G';
    computers[3].colors='O';
    computers[4].colors='W';
    computers[5].colors='G';
    computers[0].price=122.35;
    computers[1].price=1223.53;
    computers[2].price=522.35;
    computers[3].price=162.214;
    computers[4].price=1212.311;
    computers[5].price=120.035;
    computers[0].spec="1.7 Ghrz, 512 DDRAM, 21 in Moniter, 256MB AGP";
    computers[1].spec="3 Ghrz, 64 SDRAM, 14 in Moniter, 32MB AGP";
    computers[2].spec="4 Ghrz, 32 RAM, 17 in Moniter, 12 MB AGP";
    computers[3].spec="1.7 Ghrz, 512 DDRAM, 21 in Moniter, 256MB AGP";
    computers[4].spec="1.0 Ghrz, 384 SDRAM, 15 in Moniter, 128MB AGP";
    computers[5].spec="1.4 Ghrz, 128 SDRAM, 14 in Moniter, 64MB AGP";
    computers[0].gur=1;
    computers[1].gur=5;
    computers[2].gur=2;
    computers[3].gur=4;
    computers[4].gur=3;
    computers[5].gur=1;
    printf ("***********************************");
    printf ("* Computer Brand           Price  *");
    printf ("***********************************");
    for(i=0; i < 5; i++)
    printf ("*     %s                     %f   *",computers[i].brand,computers[i].price);
    printf ("***********************************");
    for(i=0; i < 4; i++){
    if (computers[i].price < computers[i+1].price)
    min=computers[i].price;
    else
    min=computers[i+1].price; }
    printf ("the cheapest price is %f",min);
    for (i=0; i < 5; i++) {
    float discount= 15 * computers[i].price /100;
    printf("%3s%3c%3s%3f\n",computers[i].brand,computers[i].colors,computers[i].spec,computers[i].price - discount);
    }
    for(i=0; i < 4; i++){
    if (computers[i].gur < computers[i+1].gur)
    int max=computers[i+1].gur;}
    printf ("the brand which has more period is %s",computers[i-1].brand);
    return 0;
    }
    i always get this error: Lvalue required
    plz help!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    You can't assign arrays

    computers[0].brand="Compaq";

    should be

    strcpy( computers[0].brand, "Compaq" );

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    1
    Good observation, (the last reply), but there is another way out, if you are not too worried about memory constraints. You can always, instead of having the brandname as an array within the structure, just keep a pointer to a character (or a string, more technically speaking) and then assign just as you did without any errors.
    So the declaration would look like this:

    struct com{
    ...
    char * brand;
    ...
    ...
    }

    and now you can easily assign
    computers[0].brand="Compaq"


    Also, in your source code you seemingly forgot to put the \n in the printf-s. It wuld not give you the proper result you might be looking for
    Happy Computing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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