Computer problems,Computer help
*AX SOFT>>>Programming & Design

C++ Leap Year program with "variable is being used without being initialized" and always return true problem


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 <iostream>
#include <cstdlib>
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<<" is a leap year\n";
}else{
cout<<" is not a leap year\n";
}
carryOn=moreData();
}
system("pause");
}
int getYear(){
int year;
cout<<"Enter a year: ";
cin>>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<<"Do you want to enter more data? (y/n) ";
return carryOn;
}

Here is the requirements for this program:

Create a program that prompts the user to enter a year and then determines whether it is a leap year. The program should have a loop and continue while the user enters the 鈥榶鈥?character.

Write three functions according to the following descriptions:

getYear has no formal parameters, ask the user to enter a year, and returns an integer value that is assigned to the integer variable year.

isLeap has an integer formal parameter year, determines whether the year is a leap year, and returns the Boolean value true if the year is a leap year and false if it is not. A year is a leap year if it is divisible by 4, but is not divisible by100 except when divisble by 400 (The year 2000 was a leap year).

moreData has no formal parameters and returns a character value.

I think there are two problems in your code.

First, the warning is talking about this function
char moreData(){
char carryOn;
cout<<"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<<"Do you want to enter more data? (y/n) ";
cin >> 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 && ( year % 100 != 0 || year % 400 == 0 ) ) {
return true;
}
else {
return false;
}
}

hi,
i did some modification in this program..just verify.
#include <iostream>
#include <cstdlib>
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<<" is a leap year\n";

else
cout<<" is not a leap year\n";

carryOn=moreData();
}
system("pause");
return 0;
}

int getYear(){
int year;
cout<<"Enter a year: ";
cin>>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<<"Do you want to enter more data? (y/n) ";
cin>>carryOn;
return carryOn;
}

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • How do i store data in order from visual basic6 to MS access?

    Do you have an index on this table? If not, create one in Access or use "select * from table order by Field1" as your adodc source.

    ...
  • Why is this javascript not working in FireFox? Nothing shows.?

    I've added a statement after the line function randomlyselectMessage1() { --------------------------------------... var Message1 = document.getElementById( 'Message1' ); -----...

  • If computer are ones and zeros how do sounds and moving images work?

    I want to write a huge answer. I'll try to keep it short. The wonderful variety of media you are used to seeing is simply the result of very complex and numerous sets of these 0's and...

  • Need help calculating in access?

    First off, you don't need [order line id] field in your order table: in fact you don't need [order line id] at all: you can create a combined key on [order id] and [item id]. Design a ...

  • Creating and using dll files in VC++ and its advantages?

    The creation and use of DLLs with VC++ can be somewhat complicated depending on your requirements. If you create your own using the MFC wizard, it's not too bad at all. A great explanation w...

  • I need a real advice please about making my website duplicatable...?

    You should hire a web design company to do the work for you. Once they do it, you can give everyone what they need with ease.

    ...
  • How do you add a html code to a image?

    Hi! The HTML Image tag is as follows <img src="path_to_the_image.jpg" width="image_width" height="image_height" border="0" alt="image_descripti...

  • What is the purpose and functions of a network operating system?

    A network operating system (NOS) is a piece of software that controls a network and its message (e.g. packet) traffic and queues, controls access by multiple users to network resources such as file...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster