Help with code

This is a discussion on Help with code within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <iomanip> #include <ios> #include <iosfwd> #include <iso646.h> #include <istream> #include <ostream> #include <sstream> #include ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Smile Help with code

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <ios>
    #include <iosfwd>
    #include <iso646.h>
    #include <istream>
    #include <ostream>
    #include <sstream>
    #include <string>
    #include <strstream>
    
    
    
    
    
    using namespace std;
    
    
    void openTheFile(ifstream &myInFCB)
    {
        char filename[80];
        cout<<"Input file name please! ";
        cin.getline(filename, '\n');
        if (strcmp(filename,"")==0)
            return;
        myInFCB.open(filename);
        while (myInFCB.fail())
        {
            myInFCB.clear();
            cout<<"Is not a file name, please try again: ";
            cin.getline(filename, '\n');
            myInFCB.open(filename);
        }
    }
    
    void printHeading(ofstream&goingAwayFile, int &k)
    {
        goingAwayFile << "Stock" << "     " << "Description" << "     " << "Total Item" << "       " << "Total Potential" << "       " << "Number of Items" << "   " << endl;
        goingAwayFile << "Number" << "      " << "of item" << "          " << "cost" << "              " << "Profit" << "            " << "Needing Ordered" <<endl<<endl;
        k=0;
    }
    
    
    int multiply(int a, int b)
    {
        int c;
        c=0;
        c=a*b;
        return c;
    }
    
    float profitCounter(float c,float d, float f)
    {
        float e;
        e=0;
        e=(c-d)*f;
        return e;
    }
    
    
    int main()
    {
        ifstream inputFile;
        openTheFile(inputFile);
        ofstream outputFile;
        outputFile.open("funsies.txt");
    
        char departmentCode[7];
        char stockNumber[8];
        char description[15];
    
        float cost;
        float profit;
        float totalPotentialProfit;
        float itemsOrdered;
        float departmentTotal;
        float grandTotal;
        float totalItemCost;
    
        float sellPrice;
        int quantInStock;
        int quantLowLimit;
        int quantUpLimit;
        int subtotal;
        int lineCount;
        int swizzle;
    
        lineCount=0;
        subtotal=0;
        cost=0;
        profit=0;
        totalPotentialProfit=0;
        itemsOrdered=0;
        departmentTotal=0;
        grandTotal=0;
        totalItemCost=0;
        sellPrice=0;
        quantInStock=0;
        quantLowLimit=0;
        quantUpLimit=0;
        swizzle=0;
    
        char prevdept[7];
        strcpy(prevdept,"WHY WON'T YOU WORK??????????????");
    
        printHeading(outputFile, lineCount);
    
        while (!inputFile.eof())
        {
            inputFile.get(departmentCode, 6);
            inputFile.ignore(1);
            inputFile.get(stockNumber, 6);
            inputFile.ignore(3);
            inputFile.get(description, 12);
            inputFile >> cost >> sellPrice >> quantInStock >> quantLowLimit >> quantUpLimit >> ws;
            lineCount++;
            if (lineCount%60==0)
            {
                outputFile << (char) 12 <<endl<< endl<<endl;
                printHeading(outputFile, lineCount);
            }
            grandTotal = totalPotentialProfit;
            totalItemCost = multiply(cost, quantInStock);
            totalPotentialProfit = profitCounter(sellPrice,cost,quantInStock);
            subtotal = totalItemCost + totalPotentialProfit;
    
    
            if (quantInStock < quantLowLimit)
            {
                itemsOrdered = quantUpLimit - quantInStock;
            }
    
            outputFile << setprecision(2) << right << fixed << stockNumber << "    " << setw(12) << description << "   " << setw(10) << totalItemCost << "         " << setw(10) <<totalPotentialProfit << "                ";
    
            if (quantInStock < quantLowLimit)
            {
                outputFile << setprecision(0) << itemsOrdered;
            }
    
    
            //outputFile << "The value of prevdept is: " << prevdept << " and departmentCode was: " << departmentCode << endl;
            cout << "prevdept equals: " << prevdept << "departmentCode equals: " << departmentCode <<endl;
            if (strcmp(departmentCode, prevdept)!=0)// && strcmp(prevdept, "WHY WON'T YOU WORK??????????????")==1)
            {
                outputFile <<endl << "Department subtotal" << endl << subtotal <<endl;
                strcpy(prevdept, departmentCode);
    
            }
    
            outputFile << endl;
    
        }
    
        outputFile << setprecision(2) << "Grand Total" << endl << grandTotal;
        inputFile.close();
        outputFile.close();
    
    }
    Here is my code, I've been working on it for about 10 hours straight and haven't gotten any sleep and its due in about 3 hours. I'm starting to blank and I was wondering if anyone could possible help me write a code to find a grandTotal that equals the totalItemCost and the totalPotentialProfit for all items in the list. Thanks for any help!

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    well what have you tried already and where is it going wrong?

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    well its going wrong in two places it prints a subtotal after the first department and works fine from there
    Code:
     if (strcmp(departmentCode, prevdept)!=0)// && strcmp(prevdept, "WHY WON'T YOU WORK??????????????")==1)
            {
                outputFile <<endl << "Department subtotal" << endl << subtotal <<endl;
                strcpy(prevdept, departmentCode);
    and i tried using to variables lets just say x and y and += both with the totalItemCost and totalPotentialProfit and it gives me a negative answer in the end.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    .

    i am trying to look and see whats going on... but one thing is this??
    Code:
    char prevdept[7];
        strcpy(prevdept,"WHY WON'T YOU WORK??????????????");
    prevdept only has 7 character space.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    I would see that you use std::string instead of char, unless char is some sort of requirement for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21