Solved

Can you help me fix my errors so that it will run?

Posted on 2013-12-05
5
196 Views
Last Modified: 2015-05-17
Write a program that will allow the user to enter the energy bills from January to December from the year prior to going green. Next allow, the user to enter the energy bills from January to December of the past year after going green. The program should calculate the energy difference from the past two years and display the two years worth of data, along with the savings.


This is a C++ Code

This is a code that I tried to do on my own can you help me fix my errors so that it will run.


#include <iostream>
#include <cmath>
using namespace std;

int main()
{

      //variable
      char Months;
      double NotGreenCost;
      double GoneGreenCost;
      double GoneGreenCost;
      double Savings;
      int counter
      double TwoYearsData
      



      //input'
      cout<< "Enter NOT GREEN Energy Cost For January";
      cin>>NotGreenCost >>January>>;
      

      cout<< "Enter NOT GREEN Energy Cost For Febuary";
      cin>>NotGreenCost >>Febuary>>;
      
      cout<< "Enter NOT GREEN Energy Cost For March";
      cin>>NotGreenCost >>March>>;
      
      cout<< "Enter NOT GREEN Energy Cost For May";
      cin>>NotGreenCost >>May>>;

      cout<< "Enter NOT GREEN Energy Cost For April";
      cin>>NotGreenCost >>April>>;

      cout << "Enter NOT GREEN Energy Cost For July";
      cin>> NotGreenCost >>July>>;

      cout << "Enter NOT GREEN Energy Cost For June";
      cin>> NotGreenCost >>June>>;

      cout << "Enter NOT GREEN Energy Cost For August";
      cin >> NotGreenCost>>August>>;

      cout << "Energy NOT GREEN Energy Cost  For September";
      cin  >> NotGreenCost >>September>>;

      cout << "Enter NOT Green Energy Cost For October";
      cin  >> NotGreenCost >>October>>;
      
      cout <<"Enter NOT GREEN Energy Cost For November";
      cin  >>NotGreenCost >>November>>;

      cout << "Enter NOT GREEN Energy Cost For December";
      cin  >>NotGreenCost >>December>>;


--------------------------------------------------------------------------------------------------------------------------------------

      cout<< "Enter GONE GREEN Energy Cost For January";
      cin>>GoneGreenCost >>January>>;
      

      cout<< "Enter GONE GREEN Energy Cost For Febuary";
      cin>>GoneGreenCost >>Febuary>>;
      
      cout<< "Enter GONE GREEN Energy Cost For March";
      cin>>GoneGreenCost >>March>>;
      
      cout<< "Enter GONE GREEN Energy Cost For May";
      cin>>GoneGreenCost >>May>>;

      cout<< "Enter GONEGREEN Energy Cost For April";
      cin>>GoneGreenCost >>April>>;

      cout << "Enter Energy Cost For July";
      cin>> GoneGreenCost >>July>>;

      cout << "Enter GONEGREEN Energy Cost For June";
      cin>> GoneGreenCost >>June>>;

      cout << "Enter GONEGREEN Energy Cost For August";
      cin >> GoneGreenCost>>August>>;

      cout << "Energy GONEGREEN Energy Cost  For September";
      cin  >> GoneGreenCost >>September>>;

      cout << "Enter GONE GREEN Energy Cost For October";
      cin  >> GoneGreenCost >>October>>;
      
      cout <<"Enter GONE GREEN Energy Cost For November";
      cin  >>GoneGreenCost >>November>>;

      cout << "Enter GONE GREEN Energy Cost For December";
      cin  >>GoneGreenCost >>December>>;



      //processing
             int counter = 0 ;

      char Months [12];
       Months [0]= 1000;
       Months [1] = January;
       Months [2] = Febuary;
       Months [3] = March;
     Months [4] = May;
       Months [5] = June;
       Months [6] = July;
       Months [7] = August;
       Months [8] = September;
       Months [9] = October;
       Months [10] = November;
     Months [11]= December;
       Months [12]= April;
      

      int NotGreenCost [12];
            NotGreenCost [0] = 1000;
            NotGreenCost [1] = [789];
            NotGreenCost [2] = [790];
            NotGreenCost [3] = [890];
            NotGreenCost [4] = [773];
            NotGreenCost [5] = [723];
            NotGreenCost [6] = [759];
            NotGreenCost [7] = [690];
            NotGreenCost [8] = [681];
            NotGreenCost [9] = [782];
            NotGreenCost [10] = [791];
            NotGreenCost [11] = [898];
            NotGreenCost [12] = [923];

      

      int GoneGreenCost [12];
            GoneGreenCost [0] = 1000;
            GoneGreenCost [1] = 546;
            GoneGreenCost [2] = 519;
            GoneGreenCost [3] = 493;
            GoneGreenCost [4] = 472;
            GoneGreenCost [5] = 432;
            GoneGreenCost [6] = 347;
            GoneGreenCost [7] = 318;
            GoneGreenCost [8] = 453;
            GoneGreenCost [9] = 489;
            GoneGreenCost [10]= 516;


      int Savings [35];
            Savings [0] = 1000;
            Savings [1] = 243;
            Savings [2] = 254;
            savings [3] = 371;
            savings [4] = 280;
            Savings [5] = 251;
            Savings [6] = 327;
            Savings [7] = 343;
            Savings [8] = 363;
            Savings [9] = 329;
            Savings [10]= 789;
            Savings [11]= 790;
            Savings [12]= 890;
            savings [13]= 773;
            Savings [14]= 723;
            savings [15]= 759;
            Savings [16]= 690;
            Savings [17]= 681;
            Savings [18]= 782;
            Savings [19]= 546;
            Savings [20]= 536;
            Savings [21]= 519;
            Savings [22]= 493;
            Savings [23]= 472;
            Savings [24]= 432;
            Savings [25]= 347;
            Savings [26]= 318;
            Savings [27]= 453;
            Savings [28]= 302;
            Savings [29]= 459;
            Savings [30]= 407;
            Savings [31]= 791;
            Savings [32]= 898;
            Savings [33]= 923;
            Savings [34]= 489;
            Savings [35]= 439;
            Savings [36]= 516;

            cout>> "Do You Want To End The Program?" >> Yes/No>>;
            
            cout>> Months [12] + NotGreenCost [12] + GoneGreenCost [12] = Savings;
            cin<<Savings;

            cout>> Months [12] + NotGreenCost [12] + GoneGreenCost [12] = TwoYearsData
            cin<<TwoYearsData;
return 0;
0
Comment
Question by:LamaKAN13
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39700660
because of homework i can only give you some hints:

you cannot define a variable  as 'char Months;' and as ' char Months[12];'

- c++ does not accept a variable defined twice.
- Months is not a single char like 'A' and is not a single char[12] array like "January"
- Months would be an array of strings where you have 13 elements if you want the 0 element have handled as well.

for an array of months you would do like

const char * Months[13] = { "", "January", .... };

Open in new window

the 'const' would help from writing deliberately to the array. the compiler would complain. the char * is pointer to a char array (a literal) which is terminated by a binary zero char (which is not visible for you but by the compiler). an array can be initialized directly with the definition as you see in the above code.

the other arrays in your code can be defined and initialized in similar way.

note, if this is your first c++ code, you should begin with little code (for example only the Months array and nothing else) and compile. then, add more code and always compile. you can post the errors if you don't understand their meaning.
0
 

Author Comment

by:LamaKAN13
ID: 39702121
I made corrections... It sill is not running... Please Help
I'm showing you what I know... And I am trying


#include <iostream>
#include <cmath>
using namespace std;

int main()
{

      //variable
     
        string January;
        string Febuary;
        string March;
        string April;
        string May;
        string June;
        string July;
        string August;
        string September;
        string November;
        string December;
        string October;
        string December;
        string  Months[12];
      double NotGreenCost;
      double GoneGreenCost;
      double GoneGreenCost;
      double Savings;
      int counter = 0;
     
     
        Months[13] = {January, Febuary, March, April, May, June, July, August, September, November, December, October, December};

      //input'
     
        
        
        
        cout<< "Enter NOT GREEN Energy Cost For January";
      cin>>NotGreenCost >January;
      cout<< "Enter Now ---> 789"

      cout<< "Enter NOT GREEN Energy Cost For Febuary";
      cin>>NotGreenCost cin>>Febuary;
      cout<< "Enter ----> 790"


      cout<< "Enter NOT GREEN Energy Cost For March";
      cin>>NotGreenCost>>March;
        cout<< "Enter ----> 890"
     
      cout<< "Enter NOT GREEN Energy Cost For May";
      cin>>NotGreenCost >>May>>;
        cout<< "Enter -----> 773"

      cout<< "Enter NOT GREEN Energy Cost For April";
      cin>>NotGreenCost >>April>>;
        cout<< "Enter -----> 723"

      cout << "Enter NOT GREEN Energy Cost For July";
      cin>> NotGreenCost >>July>>;
        cout<< "Enter -----> 759"

      cout << "Enter NOT GREEN Energy Cost For June";
      cin>> NotGreenCost >>June>>;
        cout<< "Enter -----> 690"

      cout << "Enter NOT GREEN Energy Cost For August";
      cin >> NotGreenCost>>August>>;
        cout << "Enter ----> 681"

      cout << "Energy NOT GREEN Energy Cost  For September";
      cin  >> NotGreenCost >>September>>;
        cout <<"Enter -----> 782"      

      cout << "Enter NOT Green Energy Cost For October";
      cin  >> NotGreenCost >>October>>;
      cout << "Enter ----> 791"

      cout <<"Enter NOT GREEN Energy Cost For November";
      cin  >>NotGreenCost >>November>>;
        cout << "Enter ----> 898"

      cout << "Enter NOT GREEN Energy Cost For December";
      cin  >>NotGreenCost >>December>>;
        cout << "Enter ----> 923"

--------------------------------------------------------------------------------------------------------------------------------------

      cout<< "Enter GONE GREEN Energy Cost For January";
      cin>>GoneGreenCost >>January>>;
      cout<< "Enter -----> 546"

      cout<< "Enter GONE GREEN Energy Cost For Febuary";
      cin>>GoneGreenCost >>Febuary>>;
      cout<< "Enter -----> 536"

      cout<< "Enter GONE GREEN Energy Cost For March";
      cin>>GoneGreenCost >>March>>;
      cout<< "Enter ----> 519"

      cout<< "Enter GONE GREEN Energy Cost For May";
      cin>>GoneGreenCost >>May>>;
        cout<< "Enter ----> 493"

      cout<< "Enter GONEGREEN Energy Cost For April";
      cin>>GoneGreenCost >>April>>;
        cout<< "Enter ----> 472"

      cout << "Enter Energy Cost For July";
      cin>> GoneGreenCost >>July>>;
        cout<< "Enter ----> 432"

      cout << "Enter GONEGREEN Energy Cost For June";
      cin>> GoneGreenCost >>June>>;
        cout<< "Enter ----> 347"

      cout << "Enter GONEGREEN Energy Cost For August";
      cin >> GoneGreenCost>>August>>;
        cout << "Enter ----> 318"

      cout << "Energy GONEGREEN Energy Cost  For September";
      cin  >> GoneGreenCost >>September>>;
        cout << "Enter ----> 453"

      cout << "Enter GONE GREEN Energy Cost For October";
      cin  >> GoneGreenCost >>October>>;
      cout<< "Enter ----> 489"

      cout <<"Enter GONE GREEN Energy Cost For November";
      cin  >>GoneGreenCost >>November>>;
        cout << "Enter ----> 439"

      cout << "Enter GONE GREEN Energy Cost For December";
      cin  >>GoneGreenCost >>December>>;
        cout << "Enter ----> 516"


      //processing
      int counter = 0 ;

      string Months [13];
       Months [0] = 0;
       Months [1] = January;
       Months [2] = Febuary;
       Months [3] = March;
       Months [4] = May;
       Months [5] = June;
       Months [6] = July;
       Months [7] = August;
       Months [8] = September;
       Months [9] = October;
       Months [10]= November;
       Months [11]= December;
       Months [12]= April;
     

      double NotGreenCost [13];
            NotGreenCost [0] ;
            NotGreenCost [1] ;
            NotGreenCost [2] ;
            NotGreenCost [3] ;
            NotGreenCost [4] ;
            NotGreenCost [5] ;
            NotGreenCost [6] ;
            NotGreenCost [7] ;
            NotGreenCost [8] ;
            NotGreenCost [9] ;
            NotGreenCost [10];
            NotGreenCost [11];
            NotGreenCost [12];

     

      double GoneGreenCost [13];
            GoneGreenCost [0] ;
            GoneGreenCost [1] ;
            GoneGreenCost [2] ;
            GoneGreenCost [3] ;
            GoneGreenCost [4] ;
            GoneGreenCost [5] ;
            GoneGreenCost [6] ;
            GoneGreenCost [7] ;
            GoneGreenCost [8] ;
            GoneGreenCost [9] ;
            GoneGreenCost [10];
                  GoneGreenCost [11];
                  GoneGreenCost [12];

     double savings [36];
            savings [0] ;
            savings [1] ;
            savings [2] ;
            savings [3] ;
            savings [4] ;
            savings [5] ;
            savings [6] ;
            savings [7] ;
            savings [8] ;
            savings [9] ;
            savings [10];
            savings [11];
            savings [12];
            savings [13];
            savings [14];
            savings [15];
            savings [16];
            savings [17];
            savings [18];
            savings [19];
            savings [20];
            savings [21];
            savings [22];
            savings [23];
            savings [24];
            savings [25];
            savings [26];
            savings [27];
            savings [28];
            savings [29];
            savings [30];
            savings [31];
            savings [32];
            savings [33];
            savings [34];
            savings [35];
            savings [36];

            cout>> "Do You Want To End The Program?" >> "Yes/No">>;
           
                  Months[13] * {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "November", "December", "October", "December"};

            cout>> Months[13] *  NotGreenCost[13] -  GoneGreenCost[13] = Savings;
            cin<<Savings;

             
return 0;
}
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 500 total points
ID: 39702821
you should look again at the snippet i posted:

const char * Months[13] = { "", "January", "February", .... };

Open in new window

c++ requires exact syntax. any part of the statement needs to be used exactly as be shown in the code (beside of the .... which is only indicate that you should add the other months accordingly:

the 'const' means that the contents of the variable cannot be overwritten by another statement without that the compiler complains.

the 'char' is the type of the value the pointer variable is pointing to.

the '*' means that the variable defined is a pointer variable, what means its contents is not a character like 'J' but an address (number) in memory (where the 'J' can be found).

the 'Months' is the name of the variable. you cannot have a second definition of 'Months' in the same scope (that is the block of statements between curly brackets { }, in your case the body of function main).

the [13] tells that the variable (the char pointer variable) is not a single variable but an array with 13 elements. later you may access each element of the array by index running from 0 to 12. there are 13 elements. though there are only 12 months available, it is one too much. however, arrays in c and c++ are zero-based while months were counted from 1. so, using an array of 13 elements allows to use the month number directly for index of the array. that is a big advantage while the array size of 13 doesn't harm.

the '=' tells the compiler that the newly defined variable (now an array of char pointers) will be initialized.

the '{' opens the expression where you can define the element values, separated by commas.

the "" is an empty char literal which initializes Months[0] element. as we don't use this element, the contents is arbitrary. "" actually has a size of 1 in memory. internally it contains of a binary zero character '\0' which is not a printable character and may not be mixed-up with the digit '0' which has character code 48.

the ',' separates elements in the initializing array expression.

the "January" is the literal the Months[1] is pointing to, remember that we have an array of pointers where each of them contains of an address. at the address of Months[1] the characters 'J', 'a', 'n', 'u', 'a', 'r', 'y', '\0' could be found where '\0' is a binary zero.

the '}' closes the initializing array.

the ';' ends the statement.


note, the code you posted is in no way code that can be 'repaired'. all these repetitions and explicitly use of each single array element are not necessary and must be removed.  I would recommend to start with a blank source file. then, add the include statements,. then define the body of function main which is 4 lines (you already have correctly) : the 'int main()', the curly opening bracket '{', the return statement and the closing bracket.

after that define the Months array above or within the body of function main. 'above' would define a local static array that can be used by all functions in the same source file. 'within' would mean that the array can only used in function main. at the beginning the difference is not important. if you later have more functions where the Months array was needed you could also pass the array as argument. or move it to the function that needs it.

if done so far you should compile. if there are errors post them here.

the next step would be to use a loop where you could put the cin and cout statements into.

Sara
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 109
C++ Language error 28 218
Task manager indicates my c++ program memory consumption is growing? 12 111
max float value 3 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question