When I run it, it shows "Run-Time Check Failure #3 - The variable 'carryOn' is being used without being initialized."
And I found out no matter I input a leap year or not a leap year, it always return it "is a leap year" I'm new to C++ so please help me
#include %26lt;iostream%26gt;
#include %26lt;cstdlib%26gt;
using namespace std;
int getYear();
bool isLeap(int year);
char moreData();
int main()
{
int year;
char carryOn='y';
while(carryOn=='y'||carryOn=='Y'){
year=getYear();
if(isLeap(year)){
cout%26lt;%26lt;" is a leap year\n";
}else{
cout%26lt;%26lt;" is not a leap year\n";
}
carryOn=moreData();
}
system("pause");
}
int getYear(){
int year;
cout%26lt;%26lt;"Enter a year: ";
cin%26gt;%26gt;year;
return year;
}
bool isLeap(int year){
if(year%4==0){
if (year%100!=0){
return(true);
}else if (year%400==0){
return(false);
}
return isLeap;
}}
char moreData(){
char carryOn;
cout%26lt;%26lt;"Do you want to enter more data? (y/n) ";
return carryOn;
}
C++ Leap Year program with "variable is being used without being initialized" and always return true problem
I think there are two problems in your code.
First, the warning is talking about this function
char moreData(){
char carryOn;
cout%26lt;%26lt;"Do you want to enter more data? (y/n) ";
return carryOn;
}
Notice your 'carryOn' variable is return without being assigned anything. I guess you intend to have something like this?
char moreData(){
char carryOn;
cout%26lt;%26lt;"Do you want to enter more data? (y/n) ";
cin %26gt;%26gt; carryOn;
return carryOn;
}
Your check for leap year is also wrong. Try this (I haven't tested it though).
bool isLeap(int year){
if ( year % 4 == 0 %26amp;%26amp; ( year % 100 != 0 || year % 400 == 0 ) ) {
return true;
}
else {
return false;
}
}
Reply:hi,
i did some modification in this program..just verify.
#include %26lt;iostream%26gt;
#include %26lt;cstdlib%26gt;
using namespace std;
int getYear();
bool isLeap(int year);
char moreData();
int main()
{
int year;
char carryOn='y';
while(carryOn=='y'||carryOn=='Y')
{
year=getYear();
if( isLeap(year) )
cout%26lt;%26lt;" is a leap year\n";
else
cout%26lt;%26lt;" is not a leap year\n";
carryOn=moreData();
}
system("pause");
return 0;
}
int getYear(){
int year;
cout%26lt;%26lt;"Enter a year: ";
cin%26gt;%26gt;year;
return year;
}
bool isLeap(int year){
if(year%4==0){
if (year%100!=0)
return(true);
else if (year%400==0)
return(true);
}
else
return false;
}
char moreData(){
char carryOn;
cout%26lt;%26lt;"Do you want to enter more data? (y/n) ";
cin%26gt;%26gt;carryOn;
return carryOn;
}
marigold
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment