Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I made C ++ code,but there has a problem! Even though I check for valid input, I

ID: 3776091 • Letter: I

Question

I made C ++ code,but there has a problem!

Even though I check for valid input, I don't do anything about it - so if a user entered 200,it would still work!!

please help me to solve this problem! :)

//Chapter5-#3: OceanLevel

#include <iostream>
#include <iomanip>

using name space std;
int main()
{

   int nyears, year = 2016;
   double risinglevel = 0;


   cout << "Enter n of years to predict :";
   cin >> nyears;

   if (nyears > 0 && nyears<150)
   {
       cout << "Year RisingLevel ";
       cout << "--------------------------- ";

   }
   else
       cout << "Error:Invalid Input Value of years. ";

   for (int i = 1; i <= nyears; i++)
       {
               risinglevel += 1.5;
               cout << ++year << " " << fixed << setprecision(1) << risinglevel << endl;
       }
          
   system("pause");
   return 0;
}

Chapter 5 Exercise Ocean Levels p. 293 #3 with MoDIFICATIONS Assuming the ocean's level is currently rising at about 1.5 mm per year, write a program that displays a table showing the number of mm that the ocean will have risen each year for the next nYears years. Ask the user for how many years they want to predict, and put this number in nYears. n your output, year 1 should be this year plus 1 input validation: nYears is greater than zero and must be less than 150. Sample output (nYears 3, year 0 2016) 2017 1.5 2018 3.0 2019 4.5

Explanation / Answer

Its because the loop that you have applied is incorrect. Hence it is working for value 200 as well.

YOur code is as follows:

if (nyears > 0 && nyears<150)
   {
       cout << "Year RisingLevel ";
       cout << "--------------------------- ";

   }
   else
       cout << "Error:Invalid Input Value of years. ";

//Here it will go to else and print the value but then it will go to the for loop as well and execute it.

   for (int i = 1; i <= nyears; i++)
       {
               risinglevel += 1.5;
               cout << ++year << " " << fixed << setprecision(1) << risinglevel << endl;
       }
          
   system("pause");
   return 0;
}

The correct solution is to the write the for loop inside if as follows:

#include <iostream>
#include <iomanip>
using name space std;
int main()
{
int nyears, year = 2016;
double risinglevel = 0;

cout << "Enter n of years to predict :";
cin >> nyears;
if (nyears > 0 && nyears<150)
{
cout << "Year RisingLevel ";
cout << "--------------------------- ";
       for (int i = 1; i <= nyears; i++)
{
risinglevel += 1.5;
cout << ++year << " " << fixed << setprecision(1) << risinglevel << endl;
}
}
else
cout << "Error:Invalid Input Value of years. ";
  
  
system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote