1. This forum is in read-only mode.

inputting decimals in a C program

Discussion in 'Computers & Modding' started by vivo, Oct 23, 2011.

  1. vivo

    vivo Well-Known Member

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* Quality Per Capital Calculator*/
    
    int main()
    { /*this is the EPS Calculation */
      float EPS,converter;
      char EPS_pre[20];
      printf("Welcome to the quality per capital caluculator (QPCC) Please do not use decimals. Instead, print numbers without them, like 100 for $1, or 50 for 50 percent (the last two digits are cents).\n");
      printf("First, please enter the EPS .\n") ;
      gets(EPS_pre);
      converter=atoi (EPS_pre);
      EPS=converter/0.01;
      /* This is Management cost per share */
      char convert1[10];
      char convert2[20];
      int MGMT_Cost;
      int SHRS_OUT;
      int MCPS;
      printf("Please enter the total management salary.\n");
      gets(convert1);
      MGMT_Cost= atoi(convert1);
      printf("Please enter the number of shares outstanding.\n");
      gets(convert2);
      SHRS_OUT=atoi(convert2);
      MCPS= MGMT_Cost/SHRS_OUT;
      /*VIX (subtract from preQPC determinations)*/
      int VIX;
      float VIXF;
      char VIXIN[3];
      printf("Please enter the current market VIX (Volatility Index).\n");
      gets(VIXIN);
      VIX=atoi(VIXIN);
      VIXF=VIX*0.01;
    /*Ethics Rating*/
      int ethics_score;
      char ethics [3];
      float ethicsfloat;
      printf("Please enter the ethics score.\n");
      gets(ethics);
      ethics_score=atoi (ethics);
      ethicsfloat=ethics_score*0.01;
      /*Margin of Saftey*/
      float assets;
      float liabilities;
      float netassets;
      char assets1[15];
      char liabilities1[15];
      float share_price;
      char shareprice[15];
      float margin_of_saftey;
      printf("What is the current share price?");
      gets(shareprice);
      share_price=atoi (shareprice);
      share_price=share_price*SHRS_OUT;
      printf("How much in assets does the company have?\n");
      gets(assets1);
      assets=atoi(assets1);
      printf("How much liabilities does the company have?\n");
      gets(liabilities1);
      liabilities=atoi(liabilities1);
      netassets=assets-liabilities;
      netassets=netassets-share_price;
      netassets=netassets/SHRS_OUT;
      netassets=netassets*0.01;
      netassets=netassets/1.00;
      margin_of_saftey=netassets;
      printf("The margin of saftey is %.2f.\n", margin_of_saftey);
    
    
    
    
    
    
    return (0);
    };
    
    Hello,
    I have been trying to get my program to allow for users to input monetary units by using 0.00 format instead of the 100 for 1 dollar (as an example). I have tried everything, but anytime I do try doing that, it just inputs the first two digits, and not the decimals. I have tried many code variations, so to put it up would be a huge pain. But I tried looking through the dummies AIO and no dice. If anyone could help me, it would be greatly appreciated. Thanks,
    vivo
     
  2. Hypr

    Hypr Well-Known Member

    First off, your code is a horrendous mess. Please use a blank line as a separator for each section of your code, and use proper indention. Also, DECLARE ALL YOUR VARIABLES AT THE BEGINNING OF YOUR CODE!

    Second, I tried compiling your code, and I get 38 errors. What are you trying to accomplish by converting an array of chars to a float data type? I'm referring to one of your examples here:

    Code:
      char EPS_pre[20];
      gets(EPS_pre);
      converter=atoi (EPS_pre);
    
    EDIT: I see what you are trying to do. You have all the inputs as string, and you use atoi to convert to integer (what?). Why not just use the float variable as the input?
     
  3. Loonylion

    Loonylion Administrator Staff Member

    you're using integers, they cannot have decimals. Change them to float.

    Also atoi() converts to integer. Use atof() instead.
     
  4. vivo

    vivo Well-Known Member

    I know it is rather messy :D, but I find it a tad easier to separate them by using this, because I want to work on them one at a time. So, for instance, I put the margin of safety parts together so I could locate the problems for the margin of safety part. The main point I was having problems with was the margin of safety. The only reason I included the other parts of the code is I used the SHRS_OUT (shares outstanding) in the margin of safety part. This is a work in progress and so I used the comments to perfect certain points of the code (since all of these are going to be combined, and it has to be perfect) . Thank you for the input. I am kind of new to C, so I appreciate the help.
     
  5. Hypr

    Hypr Well-Known Member

    If you want to succeed as a programmer, you better develop your organization skills. As an experienced programmer myself, I can tell you that you can save lots of time debugging your code if you kept your code neat and organized. Also, commenting your code is a must, otherwise, you won't be able to understand what you were doing when you wrote that piece of code.

    Here's what your code should have looked like. Notice how it is much more clear and easier to read?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* Quality Per Capital Calculator*/
    
    int main()
    { 
    	//Variables for EPS calculation
    	float EPS,converter;		//converted value of user EPS input
    	char EPS_pre[20];		//user input for EPS
    
    	//Variables for Management Cost per Share calculations
    	char convert1[10];		//user input of management cost
    	//Note to Vivo: please use more meaningful variable names than this!
    	//"convert1" is not an acceptable variable name for clear documentation purposes
    	char convert2[20];		//user input of outstanding shares
    	int MGMT_Cost;
    	int SHRS_OUT;
    	int MCPS;
    
    	//Variables for Volatile Index calculations
    	//I'll let you comment those variables yourself (see above examples)
    	int VIX;
    	float VIXF;
    	char VIXIN[3];
    
    	//Variables for Ethics Index calculations
    	int ethics_score;
    	char ethics [3];
    	float ethicsfloat;
    	
    	//Variables for Margin of Safety calculations
    	float assets;
    	float liabilities;
    	float netassets;
    	char assets1[15];
    	char liabilities1[15];
    	float share_price;
    	char shareprice[15];
    	float margin_of_saftey;
    
    	/*EPS Calculation */
    	printf("Welcome to the quality per capital caluculator (QPCC).\n");
    	printf("First, please enter the EPS .\n") ;
    	gets(EPS_pre);
    	converter=atof (EPS_pre);	//atof converts user string input to float, which is what you needed!
    	EPS=converter/0.01;
    
    	/*Management Cost per Share calculations*/
    	printf("Please enter the total management salary.\n");
    	gets(convert1);
    	MGMT_Cost= atof(convert1);
    	printf("Please enter the number of shares outstanding.\n");
    	gets(convert2);
    	SHRS_OUT=atof(convert2);
    	MCPS= MGMT_Cost/SHRS_OUT;
    
    	/*Volatile Index Calculation*/
    	/*(subtract from preQPC determinations)*/
    	printf("Please enter the current market VIX (Volatility Index).\n");
    	gets(VIXIN);
    	VIX=atof(VIXIN);
    	VIXF=VIX*0.01;
    
    	/*Ethics Rating Calculation*/
    	printf("Please enter the ethics score.\n");
    	gets(ethics);
    	ethics_score=atof (ethics);
    	ethicsfloat=ethics_score*0.01;
    
    	/*Margin of Saftey Calculation*/
    	printf("What is the current share price?");
    	gets(shareprice);
    	share_price=atof (shareprice);
    	share_price=share_price*SHRS_OUT;
    	printf("How much in assets does the company have?\n");
    	gets(assets1);
    	assets=atof(assets1);
    	printf("How much liabilities does the company have?\n");
    	gets(liabilities1);
    	liabilities=atof(liabilities1);
    	netassets=assets-liabilities;
    	netassets=netassets-share_price;
    	netassets=netassets/SHRS_OUT;
    	netassets=netassets*0.01;
    	netassets=netassets/1.00;
    	margin_of_saftey=netassets;
    	printf("The margin of saftey is %.2f.\n", margin_of_saftey);
    
    	return (0);	//End of program
    };
     
  6. vivo

    vivo Well-Known Member

    Good point. I kind of thought of comments more for general things, not for line by line. Thanks! Off topic: after C, what would another good language be to learn?
     
  7. Jonez001

    Jonez001 Well-Known Member

    Java. I am learning atm
     
  8. Loonylion

    Loonylion Administrator Staff Member

    java isnt a very good language, it's highly inefficient.

    C++ would be a natural progression from C, x86 assembly language has its uses too (and can be embedded in C/C++ programs).

    realistically though, it depends on what you're wanting to do, some languages are more suited to particular tasks than others.
     
  9. someirishkid

    someirishkid Well-Known Member

    Which is best for game programming?
     
  10. Seph

    Seph Administrator Staff Member

    That's probably one of the most advanced things you can start doing, but C and assembly if you want all fancy graphics and shit that needs to be optimized. It might be easier to start off with one of the Java game development libraries or the C# XNA framework
     
  11. Loonylion

    Loonylion Administrator Staff Member

    C++ tends to be more widely used than C these days
     
  12. someirishkid

    someirishkid Well-Known Member

    So... if I was to begin learning some kind of programming, where should I start?
     
  13. vivo

    vivo Well-Known Member

    I personally am having an easier time with C than C++ or Java (tried them both). Also, C++, Java, and many other languages are based off C, so to start I would suggest C.
     
  14. Loonylion

    Loonylion Administrator Staff Member

    C++ is object oriented C.
     
  15. vivo

    vivo Well-Known Member

    But I found it is easier to learn C before C++. I tried diving into C++ with no prior programming experience. I also tried Java. All I was saying is that it is easier to learn it without the OOP first (C) then try doing it later.
     
  16. Loonylion

    Loonylion Administrator Staff Member

    I disagree. When I started programming, OOP did not exist, so I learnt procedural, because that was all there was. Now OOP is the in thing, and I just cannot understand it no matter how hard I try, because procedural programming makes logical sense to me, OOP does not. Seph started learning OOP from the beginning, and he has no problems with it, because he never learnt procedural.