Why is my code not working??? I tried everything.... Please help

Why is my code not working??? I tried everything....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//This program calculates the inflation rate given two Consumer Price Index values and prints it to the monitor.

#include <iostream>
using namespace std;

/*
 * InflationRate - calculates the inflation rate given the old and new consumer price index
 * @param old_cpi: is the consumer price index that it was a year ago
 * @param new_cpi: is the consumer price index that it is currently 
 * @returns the computed inflation rate or 0 if inputs are invalid.
 */
 double InflationRate(float old_cpi, float new_cpi);
 

int main()   //C++ programs start by executing the function main
{

   // TODO #1: declare two float variables for the old consumer price index (cpi) and the new cpi
   
    float old_cpi, new_cpi
    cout << "Enter the old and new consumer price indicies:";

   // TODO #2: Read in two float values for the cpi and store them in the variables
    cin >> old_cpi >> new_cpi;

   // TODO #3: call the function InflationRate with the two cpis
    double InflationRate(old_cpi, new_cpi); //call, add a variable and assign it.  


   // TODO #4: print the results
    cout << "Inflation rate is: ";

   return 0;
}


// double InflationRate(float old_cpi, float new_cpi)
// precondition:   both prices must be greater than 0.0
// postcondition:  the inflation rate is returned or 0 for invalid inputs
double InflationRate (float old_cpi, float new_cpi)
{
   // TODO: Implement InflationRate to calculate the percentage increase or decrease
   if(old_cpi<0||new_cpi<0||old_cpi==0)
   {
    return 0;
   }
   return (new_cpi - old_cpi) / old_cpi * 100;
   // Use (new_cpi - old_cpi) / old_cpi * 100

}

You forgot the semi-colon at the end of line 20.

Line 27 doesn't declare a variable and assign the value of the function to it. Combine lines 31 and 27 as
cout << "Inflation rate is: " << InflationRate(old_cpi, new_cpi);

You can't spell "indices".
Last edited on
Thank you but still having trouble with the compiler. :(
Why do you not show us the exact error messages that the compiler gives you? :(
Copy and paste the error message along with your updated code.
Last edited on
More on "how to call a function" is in: http://www.cplusplus.com/doc/tutorial/functions/

There are many approaches:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double foo {}; // zero initialized variable
foo = bar(); // assignment to existing variable
std::cout << foo;


double foo = bar(); // copy initialization of variable
std::cout << foo;


auto foo = bar(); // copy initialization of variable. Type deduced
std::cout << foo;


std::cout << bar(); // no variable 
It's having trouble with lines 21 - 24 but I have no idea why...
Oh and I'll post the expected output again below:






expected output for InflationRate Part1:



Enter the old and new consumer price indices: 238.170 239.513
Inflation rate is 0.563884



Write a program to read in two consumer price indexes and print out the inflation rate.



1. Start with the student starter code and follow the instructions in the code.



2. Instructions to complete are tagged in comments like the ones below. They will always begin with // TODO.



// TODO #1: declare two float variables for the old consumer price index (cpi) and the new cpi


// TODO #2: Read in two float values for the cpi and store them in the variables<br>


// TODO #3: call the function InflationRate with the two cpis


// TODO #4: print the results
It's having trouble with lines 21 - 24

Re-read lastchance's post. Apply changes. Post updated code. Post exact error messages.
Last edited on
Okay here is the updated code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//This program calculates the inflation rate given two Consumer Price Index values and prints it to the monitor.

#include <iostream>
using namespace std;

/*
 * InflationRate - calculates the inflation rate given the old and new consumer price index
 * @param old_cpi: is the consumer price index that it was a year ago
 * @param new_cpi: is the consumer price index that it is currently 
 * @returns the computed inflation rate or 0 if inputs are invalid.
 */
 double InflationRate(float old_cpi, float new_cpi);
 

int main()   //C++ programs start by executing the function main
{

   // TODO #1: declare two float variables for the old consumer price index (cpi) and the new cpi
   
    float old_cpi, new_cpi
    cout << "Enter the old and new consumer price indicies:";

   // TODO #2: Read in two float values for the cpi and store them in the variables
    cin >> old_cpi >> new_cpi;

   // TODO #3: call the function InflationRate with the two cpis
                     //call, add a variable and assign it.  


   // TODO #4: print the results
   cout << "Inflation rate is: " << InflationRate(old_cpi, new_cpi);

   return 0;
}


// double InflationRate(float old_cpi, float new_cpi)
// precondition:   both prices must be greater than 0.0
// postcondition:  the inflation rate is returned or 0 for invalid inputs
double InflationRate (float old_cpi, float new_cpi)
{
   // TODO: Implement InflationRate to calculate the percentage increase or decrease
   if(old_cpi<0||new_cpi<0||old_cpi==0)
   {
    return 0;
   }
   return (new_cpi - old_cpi) / old_cpi * 100;
   // Use (new_cpi - old_cpi) / old_cpi * 100






The exact error message is: InflationRate.cpp:21:5: error: expected initializer before 'cout'
cout << "Enter the old and new consumer price indicies:";
^~~~

InflationRate.cpp:24:23: error: 'new_cpi' was not declared in this scope.
cin >> old_cip >> new_cpi;
^~~~~~~

InflationRate.cpp:31:37: error: expected primary expression before 'double'
cout << "Inflation rate is: " << double InflationRate(old_cpi, new_cpi);
^~~~~~~
Last edited on
Look at line 20. Then look at lastchance's post: "You forgot the semi-colon at the end of line 20."
You need to add a semi-colon (;) at the end of line 20.

Learning how to decipher compiler errors is a vital skill in C++. You'll get the hang of it eventually. In general, always look around the lines that it complains about to see if there's anything noticeably wrong within the immediate vicinity (like forgetting a semi-colon).
Last edited on
Thank you but even after adding the semicolon when i try to test it on Mimir all 5 of the cases are marked as Failed..... I seriously don't know where to go from here.
We don't know the 5 cases of Mimir, nor what it expects.


Edit:
1
2
3
// precondition:   both prices must be greater than 0.0

   if( old_cpi<0 || new_cpi<0 || old_cpi==0 )

You test whether old_cpi is less than or equal to 0, or new_cpi is less than 0.
(Note: what does <= test?)
Does that match the both greater?
Last edited on
I am not entirely sure.. I went over this with my professor and she said to use the less than or equal to 0
What cases do these two constructs cover?
1
2
3
4
5
if ( old_cpi <= 0 || new_cpi <= 0 ) {
  return 0;
}

return ...


1
2
3
4
5
6
if ( 0 < old_cpi && 0 < new_cpi ) {
  return ...
}
else {
  return 0;
}
I'm not sure T T
Do you know the operators < <= || &&?
Topic archived. No new replies allowed.