How do I add a loop to this code

How can I ...
1. Put the logic in TODO #2-4 into a loop (do-while preferably if possible) that asks the user to enter 'y' (or 'Y') if there's more data to be entered.



2. Keep a running total of the valid inflation rates and the number of computed rates to calculate the average rate.



3. Print the results after the loop

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
53
54
55
56
57
58
59
60
61
62
63
  //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 the function and add a variable and assign it.
    double Rate = InflationRate(old_cpi, new_cpi);
    
   // TODO #4: print the results
   cout << "Inflation rate is: " << Rate << endl;

  //Part 2 add a loop
  char userInput;
  do  
  { 
  cout << "Try again? [y or Y]" 

    
  } 
  while (userInput = 'y','Y')
  
  
  
   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

}
I added this part but i know its probably totally wrong. Any help?
" char userInput;
do
{
cout << "Try again? [y or Y]"


}
while (userInput = 'y','Y')"

1
2
3
4
5
6
7
 do  
  { 
  cout << "Try again? [y or Y]" 

    
  } 
  while (userInput = 'y','Y')


The only thing your program is looping is "Try again? [y or Y]". Try moving your do up in the code to what you would like to repeat.
while (userInput = 'y','Y')
do { ... } while (userInput == 'y' || userInput == 'Y');
It hit me with this error:

" InflationRate.cpp:39:10: error: ‘userInput’ was not declared in this scope
while (userInput == 'y' || userInput == 'Y'); "
^~~~~~~~~
Show your updated code.
You should also put std::cin >> userInput; somewhere within the loop.
Last edited on
Ok here it is :)

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
53
54
55
56
57
58
59
//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 the function and add a variable and assign it.
    double Rate = InflationRate(old_cpi, new_cpi);
    
   // TODO #4: print the results
   cout << "Inflation rate is: " << Rate << endl;
   
   //Part 2 add a loop 
  do  
  {
  cout << "Try again? [y or Y]";

  } 
  while (userInput == 'y' || userInput == 'Y');  

   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

}
How can i declare userInput in that scope without making the compiler cause problems?
And do i need a cin after the cout "Try again?"??
I don't understand why you removed it. You declared it in the first post. Re-add the declaration of your variable.

You also aren't looping over any actual code, that will just be a infinite loop as you have it.
You need to loop over the actual logic in your program.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    char userInput;
    do
    {
        // [ Actual logic in program here ]
   
        // [ Ask user Try again Y/N]

    } while (userInput == 'y' || userInput == 'Y');  

}


You also will need std::cin >> userInput; somewhere within the do-while loop.

And do i need a cin after the cout "Try again?"??
Try it out and see what happens :)
Last edited on
I tried this but i don't think its right and did not seem to compile. I'm not sure how much logic i want to take and put into the do-while loop. Do i just want to copy TODO #2-4 and put it into the do-while loop? but i tried that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 do  
  {
  char userInput;
  
  float old_cpi, new_cpi;
  cout << "Enter the old and new consumer price indicies:";
  cin >> old_cpi >> new_cpi;
  
  double Rate = InflationRate(old_cpi,new_cpi);
  cout << "Inflation rate is: " << Rate endl;
  std::cin >> userInput;
  
  cout << "Try again? [y or Y]";

  } 
  while (userInput == 'y' || userInput == 'Y');  

   return 0;
}
Last edited on
Oh wait i was putting char userInput; inside the do while loop... maybe if i fix that it might change
Ok... it runs but it is not giving the output correctly.

Example of test case (what it should be):
Enter the old and new consumer price indices: Inflation rate is -0.0390204
Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.167049
Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.0752572
Try again? (y or Y): Average rate is -0.0436042

My output:

Enter the old and new consumer price indicies:Inflation rate is: -0.0390204
Enter the old and new consumer price indices:Inflation rate is 0
Try again? y or Y
Last edited on
Topic archived. No new replies allowed.