Good evening,
I've managed to find some time and clean the code a bit.
1) Also I added "intends", maybe not in professional way but it should be more "readable" now.
2)I know that gets() is no longer in use- I was just playing with it. Was curious if get any decent output from it.
3)The problem exist when I want to implement 2nd converter. To do this I want to ask question about converter type. The whole issue is that when I am responding on 1st question regarding converter type the code omits 2nd question, goes straight to 3rd one (amount of volts) and even if I put anything the code stops. It seems like the answer from 1st question was stored in memory and other scanf's won't work.
4) Ok I have modified code and now I am getting what I wanted, however it seems that my if statement is not working correctly. If i type "volt" script is going correctly, however if I type amp it still goes same way as volt converter. It seems like that if was not read at all.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
//Variable setup
char myreply[10];
int unit; //unit of measurement
double amt,x_mv,x_v; //amount of volts depends of unit of measurement
//-------------------------------------------------------------------
//Constant values for unit conversion
double mv_1 = 0.001;
double v_1 = 0.000001;
double mv_2 = 1000;
double v_2 = 0.001;
double mv_3 = 1000;
double v_3 = 1000000;
//-------------------------------------------------------------------
//Initiate intro information window
intro();
printf("Please chose type of converter:\n");
printf("volt - Volt converter / amp - Ampere converter\n");
fgets(myreply,sizeof(myreply),stdin);
//!! IF IS NOT READ CORRECTLY
if(strcmp(myreply,"volt")){
printf("Please specify unit of measurement:\n");
printf("1 - microVolt / 2 - miliVolt / 3 - Volt\n ");
unit = getchar();
printf("Please provide amount of volts in chosen unit:\n");
scanf("%lf", &amt);
switch(unit){
case '1':
x_mv = amt * mv_1;
x_v = amt * v_1;
if(amt!=0){
printf("%lf uV is equal to: %lf mV \n",amt,x_mv);
printf("%lf uV is equal to: %lf V \n",amt,x_v);
}
else if(amt==0){
printf("You are multiplying by 0! It is not allowed!\n");
}
break;
case '2':
x_mv = amt * mv_2;
x_v = amt * v_2;
if(amt!=0){
printf("%lf mV is equal to: %lf uV \n",amt,x_mv);
printf("%lf mV is equal to: %lf V \n",amt,x_v);
}
else if(amt==0){
printf("You are multiplying by 0! It is not allowed!\n");
}
break;
case '3':
x_mv = amt * mv_3;
x_v = amt * v_3;
if(amt!=0){
printf("%lf V is equal to: %lf mV \n",amt,x_mv);
printf("%lf V is equal to: %lf uV \n",amt,x_v);
}
else if(amt==0){
printf("You are multiplying by 0! It is not allowed!\n");
}
break;
}
}
else if(strcmp(myreply,"amp")){
printf("New code");
}
return 0;
}
//Into function
int intro()
{
printf("***Welcome in volt unit converter***\n");
printf("\t ver. 0.3\n");
printf("--------------------------------------\n\n");
}
Once again, many thanks for the effort and help. https://cboard.cprogramming.com/images/icons/icon14.png
Rgds,
T