0

I've got some code with if, else if, and else statements that evaluate some inequalities.
My question is: How can I assign the result of the statements (which are Monoatomic, Diatomic, Polyatomic in this case) to a string variable, so that later I can annotate a graph using this variable

/* Determine the type of gas - theoretical gamma for: 
     - monoatomic = 1.66 
     - diatomic = 1.4
     - polyatomic = 1.33 */

if (gamma <=1.36)
    printf("This gas is POLYATOMIC\n");

else if (gamma > 1.36 && gamma <= 1.5)
    printf("This gas is DIATOMIC\n");

else
    printf("This gas is MONOATOMIC\n");

As you can see, at the minute I'm only able to print out the result. But this doesn't enable me to use the result later.

joce
  • 9,624
  • 19
  • 56
  • 74

2 Answers2

2

Use a variable to store this info:

#define POLYATOMIC 3 
#define DIATOMIC 2 
#define MONOATOMIC 1 
#define INVALID 0 

int atomicity = INVALID;
const char* gasTypeName = "ERROR";

if (gamma <=1.36)
{
    atomicity = POLYATOMIC;
    gasTypeName = "Polyatomic";
}
else if (gamma > 1.36 && gamma <= 1.5)
{
    atomicity = DIATOMIC;
    gasTypeName = "Diatomic";
}
else
{
    atomicity = MONOATOMIC;
    gasTypeName = "Monoatomic";
}

printf("The gas is %s", gasTypeName);
joce
  • 9,624
  • 19
  • 56
  • 74
  • Thanks for the reply. I'd still like to be about to store the word of which type it is. So that later I could say something like: printf("The gas is %s", gasTYPE); with gasTYPE holding the values MOOATOMIC, DIATOMIC or POLYATOMIC...is this possible? Sorry if i wasn't clear – user2137944 Mar 18 '13 at 18:49
  • @user2137944 Other solution: Store the type of gas as well as the string in a variable at the same time. – joce Mar 18 '13 at 19:00
  • Thanks a lot. Your updated other solution is the type of thing I was looking for. I had tried to do something similar but it wasn't working for me. Issue resolved – user2137944 Mar 18 '13 at 21:11
  • @user2137944 You're welcome. :-) Could you mark my answer as approved? – joce Mar 18 '13 at 21:13
1

You could use an enum for the gastypes and an array of description strings for the printable representation. Storing the result state as an integer/enum has the advantage that it can be compared easily, e.g., used in a switch. Comparing strings is a bit cumbersome in contrast.

Here's an example implementation using X-Macros:

#include <stdio.h>

#define GASTYPES \
    ENTRY(MONOATOMIC) \
    ENTRY(DIATOMIC) \
    ENTRY(POLYATOMIC)

typedef enum {
#define ENTRY(x) x,
    GASTYPES
#undef ENTRY
} gastype_t;

const char const * gastype_str[] = {
#define ENTRY(x) #x,
    GASTYPES
#undef ENTRY
};

int main() {
    double gamma; 
    gastype_t gastype;
    if(scanf("%lf", &gamma)) {
        if (gamma <= 1.36)
            gastype = POLYATOMIC;
        else if (gamma <= 1.5)
            gastype = DIATOMIC;
        else
            gastype = MONOATOMIC;
        printf("This gas is %s\n", gastype_str[gastype]);
        return 0;
    }
    else {
        printf("Failed to parse input :(\n");
        return -1;
    }
}

Hereby the preprocessor expands the definition of the enum and the array of description strings to the following before the actual compilation:

typedef enum {
    MONOATOMIC,
    DIATOMIC,
    POLYATOMIC,
} gastype_t;

const char const * gastype_str[] = {
    "MONOATOMIC",
    "DIATOMIC",
    "POLYATOMIC",
};

Usage example:

$ gcc test.c && echo "1.4" | ./a.out
This gas is DIATOMIC
Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Thanks for your help. A lot of that code is a bit beyond me at the minute! But I'll play around and learn a bit more. Thanks – user2137944 Mar 18 '13 at 21:12
  • While I love x-macros, and this is a perfect use for them, I think that suggesting them to a newbie user is probably counterproductive. – Vicky Apr 29 '13 at 14:17
  • @Vicky As I see it, this is a learning platform. And you can only learn something new, if someone points you at things you don't know yet. Especially when these things are in context to things you care for at the moment. – moooeeeep May 16 '13 at 06:53