0

I've written the following code for the ideal gas equation and been copying and pasting my input block from other areas of my code. When I get to debugging, the console isn't waiting for an input for the variable 'N'. When I placed an input before it accesses the vol function, it seemed to work but this isn't really what I want it to do. Any help with how to fix this?

#include <stdio.h>
#include <stdlib.h>
#define R 8.3145 //J/(mol.K)

double P; //Pressure in Pascals
double p; //Pressure in kilopascals
char str1[6];
char str2[6];
char str3[6];
double T; //Temperature in degrees Celsius
double TK; //Temperature in degrees Kelvin

double N; // moles in kmol
double n; // moles in mol
double V; // Volume in m3

void flusher(){
    //Function to flush the memory register
    fflush(stdout);
    fflush(stdin);
}

void Press(){
    //P is needed in Pa, needs to be converted from kPa
    //Obtaining pressure from user input and converting to the right magnitude of units
    printf("Please enter the partial pressure of the component (kPa): ");
    p = atof(fgets(str1,6,stdin));
    printf("You have entered %.3f kPa \n",p);

    //To use the Ideal Gas equation, pressure is needed in pascals
    P = 1000*p; //1000 Pa = 1 kPa
    printf("Therefore P = %f Pa \n\n",P);
    flusher();
}

void Temp(){
    /* T is in K, needs to be converted from deg C */
    printf("Please enter the system temperature (deg C): ");
    T = atof(fgets(str2,6,stdin));
    printf("You have entered %.3f deg C \n",T);
    TK = T + 273.15; //K
    printf("Therefore T = %f K \n\n",TK);
    flusher();
}

void vol(double Vm){
    // Calculates the occupied volume from the molar volume calculated through the Ideal Gas Law
    printf("Please enter the moles of component i (kmol): ");
    N = atof(fgets(str3,6,stdin));
    printf("\nYou have entered %f kmol\n",N);
    n = N*1000;
    V = Vm*n;
    printf("V = %f m3", V);
    flusher();
}

int main(){
    printf("R = %f \n",R);
    Press();
    Temp();

    // Calculating the volume of the component
    //PV = nRT tfr V_m = \frac{RT}{P}
    printf("P = %f Pa and T = %f K \n",P,TK);

    double Vm;
    Vm = (R*TK)/(P);
    printf("Molar volume = %f m3/mol \n\n",Vm);

    printf("Do you want to work out the occupied volume? [Y/N] ");
    char i = getchar();
    switch(i){
        case 'Y':
        case 'y':
        printf(" \n");
            vol(Vm);
        break;

        case 'N':
        case 'n':
            printf("You have picked No\n ");
        break;

        default:
        printf("Input not recognised\n");
    }
    flusher();
    return 0;
}

Console Output

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
Matt C
  • 63
  • 5
  • 3
    `getchar();` leaves a newline in the buffer. If you are going to use `fgets` then use it for *all* the input - don't mix your methods. – Weather Vane Jun 03 '20 at 18:30
  • Don't flush `stdin`, it is undefined behavior – Eugene Sh. Jun 03 '20 at 18:37
  • 1
    ... note that `fflush(stdin);` is not supported on most systems. – Weather Vane Jun 03 '20 at 18:37
  • 1
    Make `str1`, `str2` and `str3` bigger (and change the `fgets` calls accordingly), your problem probably is that char arrays of length 6 are too short to fit all the digits of the numbers you are typing plus the new line character plus the null character. – isrnick Jun 03 '20 at 18:47
  • Yes, and the code will be less error prone and easier to maintain if instead of `fgets(str1, 6, stdin)` you use `fgets(str1, sizeof str1, stdin)`. – Weather Vane Jun 03 '20 at 18:51
  • `char i = getchar();` should be `int i = getchar();` ( [getchar()](https://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm) ... returns the character read as an unsigned char cast to an int or EOF on end of file or error. ) – ryyker Jun 03 '20 at 18:52
  • @WeatherVane Just changed the getchar to fgets and changed up the switch statement to match an integer input as the compiler didn't like it. Also removed the `fflush(stdin)` and still am getting the same bug occurring. – Matt C Jun 03 '20 at 18:52
  • @BobJarvis-ReinstateMonica - LOL, yes. Thanks – ryyker Jun 03 '20 at 18:53
  • So instead of your `char i = getchar(); switch(i){...}` I would have `char opstr[4]; fgets(opstr, sizeof opstr, stdin); switch(opstr[0]){...}` and another problem as @isrnick said is buffer size. If the input is too large for a buffer it does not vanish, it arrives at the following input. Don't be tight with your buffer sizes, mostly we are not short of a few bytes. – Weather Vane Jun 03 '20 at 18:59

0 Answers0