Thread: Need some help with this code. Struggling

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    Need some help with this code. Struggling

    Struggling with this, can't seem to get it to work. Here is what I need to do:
    1. Ask the user how many weeks were in the landscaping season this year.
    2. Dynamically create an array of double floating point data that can be used to store the sales for each week of the season.
    3. Using a for loop, have the user enter in the sales data for the season.
    4. Pass the array (and its size) into a function that will display all the sales for the weeks in a nice table.
    5. Call another function that will take in the array as an argument and will total the sales for the season (returning the total to the main function).
    6. Have the program tell the user the total sales for the season and the average sales per week.
    7. After this delete the allocated memory and set the pointer to 0.

    Any assistance helps always I don't quite understand functions, or pointers. and I've read about both a lot!!

    Code:
     #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int Weeks = 0;
    double total;
    double sales;
    void SalesTable(int);
    double Total (int, double[]);
    double average;
    
    int main()
    {
        double Season[Weeks];
        
        cout << "How many weeks were in the landscaping season this year: ";
        cin >> Weeks;
       
        for (int i = 1; i<Weeks+1; i++)
        {
            cout << "Enter sales data for week " << i <<": ";
            cin >> sales;
        }
        Season[Weeks]=sales;
        
        SalesTable(Weeks);
        Total(Weeks, Season);
        
        average = total/Weeks;
        
        cout << "The total sales for the season are: "<<setprecision(2)<<showpoint<<fixed<<total<< endl;
        cout << "The average weekly sales were: "<<setprecision(2)<<showpoint<<fixed<<average;
        return 0;
    }
    void SalesTable(int Weeks)
    {
        for(int i = 1; i<Weeks+1; i++)
        {
            cout << "Sales for week "<< i <<":" << sales <<endl;
        }
    }
    double Total(int Weeks, double Season[])
    {
        for(int i = 1; i<Weeks+1; i++)
        {
            total += Season[Weeks];
        }
        return total;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    First, don't declare the number of weeks in the season as a year. What you are doing is allocating an array of size 0 on the stack, I do believe. Otherwise, you're using a pointer on the stack that points to a memory region of size 0 because you initialize the variable Weeks to 0 which means it's size is 0.

    Just declare it as a normal int or float depending on how you want to define a week (as in, .2 or .3 weeks can exist in this code).

    Second, dynamically create means you're allocating on the heap, not the stack. To do so requires the use of new(). Look up how to use new() to dynamically allocate the required sized array. And yes, it's sized by your Weeks variable.

    Third, using a for-loop and your prior knowledge of Weeks, you "cin" your way through the array.

    Get this part of your code working before you start working on the others. You can use a print loop to confirm the values of your array using a separate for-loop after your cin-loop.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Second, dynamically create means you're allocating on the heap, not the stack.
    No, it doesn't imply that. However, it is true that the currently only way to do this in the language is to use new (that is what the libraries do).
    However... that brings me to my next point.

    Quote Originally Posted by MutantJohn View Post
    To do so requires the use of new(). Look up how to use new() to dynamically allocate the required sized array. And yes, it's sized by your Weeks variable.
    No, just use std::vector.
    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. Struggling to understand C, and where I am going wrong.
    By DARK_STALKER in forum C Programming
    Replies: 1
    Last Post: 11-29-2012, 10:44 AM
  2. Struggling with filtering arrays
    By Boborjan in forum C Programming
    Replies: 2
    Last Post: 04-24-2011, 02:18 PM
  3. Struggling with Reverse of String
    By dnguyen1022 in forum C Programming
    Replies: 11
    Last Post: 12-09-2008, 12:23 AM
  4. Struggling w/ string_replacement function
    By cwafavre in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 07:08 AM
  5. **struggling Programmer Needs Help Badly**
    By mindofpoison in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2005, 08:11 AM

Tags for this Thread