Thread: calculating total of floats that are part of a struct?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Question calculating total of floats that are part of a struct?

    I’m an absolute beginner, of C and programming all together.
    I am working with sructs that consists of 2 members(char[] and float).
    No problem reading values in and printing them out but,
    HOW CAN I ACCESS FLOAT VALUES AND CALCULATE THEIR TOTAL?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct buyaCbook
    {
        char gonow[ ANDBUYABOOK ];
        float learnthebasics;
    };
    ...
    struct buyaCbook now;
    
    now.learnthebasics = 1.0;
    Something like that.


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

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    1
    I had that exact same problem, and your code totally helped me out!
    Thanks!

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    Quote Originally Posted by quzah
    Code:
    struct buyaCbook
    {
        char gonow[ ANDBUYABOOK ];
        float learnthebasics;
    };
    ...
    struct buyaCbook now;
    
    now.learnthebasics = 1.0;
    Something like that.


    Quzah.


    Thank you for your help, I really appreciate it, but, unfortunately, it didn’t solve my problem.
    When I sad I am a beginner I meant I have not done much coding. I have learnt a bit of theory though, and I’m familiar with accessing members of a struct, name.float and other_name->float. What I need to do is add all the float values to get their total. Values are read in with scanf(), but name.float + other_name.float = float obviously does not work. What am I missing here?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Post some actual code, not a description.

    Can you for example add two floats which are not in a structure?

    You seem to be doing
    v1 + v2 = answer;

    What you should have is
    answer = v1 + v2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >but name.float + other_name.float = float obviously does not work.
    I think u might be well wish to have 2 varaiables of type struct.

    Something like this
    Code:
    struct buyaCbook now;
    struct buyaCbook now1;
    double res;
    
    now.learnthebasics = 1.0;
    now1.learnthebasics = 2.0;
    
    res = now.learnthebasics  + now1.learnthebasics;
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM