Thread: Beginners question:

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Beginners question:

    Okay, so my assignment for my C class is to generate a program that will allow the user to enter data for a stock and calculate the amount of stocks that ended up being positive, negative, and neutral.

    I tried to do this using one stock, here is my code;

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>



    Code:
    void main()
    {
    
    
    
    
        float Neg;
        float incst;
        float shrs;
        float bpps;
        float crnt;
        float crntcst;
        float yrfes;
        float pft;
        float tpft;
    
    
    
    
        printf("IMB number of shares:");
        scanf("%f", &shrs);
        printf("IBM buying price per share: ");
        scanf("%f", &bpps);
        printf("IBM current price per share: ");
        scanf("%f", &crnt);
        printf("IBM yearly Fee: ");
        scanf("%f", &yrfes);
    
    
    
    
    
    
        incst = shrs*bpps;
        crntcst = shrs*crnt;
        pft = crntcst - incst - yrfes;
        tpft = pft;
        
    
    
        printf("The initial cost is %0.2f\n", incst);
        printf("The Current cost is %0.2f\n", crntcst);
        printf("The profit is %0.2f\n", pft);
        if (pft < 0 )
        {
            int Sum;
            int Neg;
            Sum = 0;
            int add;
            add = 1;
            
            Neg=Sum -= add;
            printf("given stock is negative");
            
    
    
    
    
    
    
        }
        
        else if (pft > 1)
        {
            printf("given stock is positive");
            
    
    
        }
        else if (pft == 0)
        {
            printf("given stock did not change");
            
        }
    
    
        printf("the negatives were %d", Neg);
        
        system("pause");
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Where does it hurt?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    So, do you have a question, or do you just have a "post homework on the internet" fetish?

    Some passing comments, since you haven't asked any questions.

    1) main() returns int, not void.
    2) <iostream> is a C++ header, and should not be in C code.
    3) _CRT_SECURE_NO_WARNINGS is usually meaningless unless you're using a Microsoft compiler.
    4) There are better (in the sense of being standard C and portable) options to cause a program to wait for user input than system("pause"). system("pause") is system specific.
    5) Floating point variables are not a good idea for representing currency values. Among other things, a floating point variable cannot represent values like 0.1 or 0.01 exactly (it only represents an approximation to such values) - which means floating point variables can't reliably represent values to the nearest cent. That is problematical if you do lots of calculations involving such values, since the errors tend to build up.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Code:
    printf("IMB number of shares:");
    I'm sure it's supposed to be
    Code:
    printf("IBM number of shares:");

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    To whomever gave you the advice to use system("pause"), here is my middle finger.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    seeing i use (and program for) mostly win systems, i still use system();

    and yes i know the risks....same as back in the xp days of renaming cmd.exe to login.scr!

    i miss that trick!

    which as a side note, what would work instead of system("cls");

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Sure, there are potential risks with using system(), but sometimes you just need to call other programs in your code for the sake of making your program simpler. Fortunately what's in your user's PATH is their problem, not yours, and so long as you can make it easy for them to change the execution string, calling external programs from your program can be a pretty useful thing to do.

    With that said, this isn't a situation where it's a good choice. My advice would be to either write your program so that it works well with standard streams (and forget the concept of terminals entirely), use a library that's designed for terminals like curses, or use a prettier, graphical interface instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginners question
    By James Charny in forum C Programming
    Replies: 8
    Last Post: 02-09-2013, 12:55 AM
  2. beginners question
    By facebook1978200 in forum C Programming
    Replies: 18
    Last Post: 01-14-2013, 04:49 AM
  3. C beginners question
    By incipientmind in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 02:06 PM
  4. Beginners question
    By jamesstuart in forum C Programming
    Replies: 9
    Last Post: 09-24-2007, 03:24 PM
  5. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 06:10 AM

Tags for this Thread