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;
}