Hey guys!

I'm trying to write a program that involves a switch/case on Microsoft Visual Studion 2010. Unfortunately, some of my variables are not being initialized. I am a very novie programmer so please have mercy on me! Two of my options (1,4) are working but options (2,3) are not. I"m getting a run-time error I think.

Here is my program so far...

Any help would be greatly appreciated!!

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    // Variable declarations
    string name;

    // Input

    int salary, earnings, yearsWorking, retireAge, option;
    double age, savings, savePercentage;

    cout << "What is your name? ";
    getline (cin, name);
    cout << "How old are you? ";
    cin >> age;

    if (age >=60)
        retireAge = 65;
    if ((age < 60) && (age >= 50))
        retireAge = 67;
    if ((age < 50) && (age >= 40))
        retireAge = 70;
    if (age < 40)
        retireAge = 72;

    cout << "Please select an option: " << endl;
    cout << "\t(1) Number of years to retirement" << endl;
    cout << "\t(2) Amount earned between now and retirement" << endl;
    cout << "\t(3) Amount saved at retirement" << endl;
    cout << "\t(4) Exit (do nothing)" << endl;
    cin >> option;
    switch (option)
    {
        case 1:
            yearsWorking = (retireAge-age);
            cout << "You have " << yearsWorking << " years until retirement.";
            break;
        case 2:
            earnings = (yearsWorking * (52 * salary));
            cout << "How much do you make per week in dollars? ";
            cin >> salary;
            cout << "You will earn $" << earnings << " between now and retirement.";
            break;
        case 3:
            savings = (earnings * (savePercentage/100));
            cout << "How much do you make per week in dollars? ";
            cin >> salary;
            cout << "What percentage will you save? ";
            cin >> savePercentage;
            cout << "You will have $" << savings << " when you retire.";
            break;
        case 4:
            break;
    }

}

Recommended Answers

All 4 Replies

the problem is you are using uninitialized variables to do your computations. in case 2 you are using yearsWorking without ever storing a number in there.

Thanks for your advice Nathan,

Unfortunately I already know that some of my variables (I think just salary and yearsWorking) are uninitialized. I tried to take your advice and place my yearsWorking equation into case 2 but I still did not have success. I guess what I might be trying to ask is how do you initialize a variable that requires an input value? Here's the changes I made.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	// Variable declarations
	string name;
	int salary, earnings, yearsWorking, retireAge, option;
	double age, savings, savePercentage;
	
    // Input
	
	cout << "What is your name? ";
	getline (cin, name);
	cout << "How old are you? ";
	cin >> age;

	if (age >=60)
		retireAge = 65;
	if ((age < 60) && (age >= 50))
		retireAge = 67;
	if ((age < 50) && (age >= 40))
		retireAge = 70;
	if (age < 40)
		retireAge = 72;

	cout << "Please select an option: " << endl;
	cout << "\t(1) Number of years to retirement" << endl;
	cout << "\t(2) Amount earned between now and retirement" << endl;
	cout << "\t(3) Amount saved at retirement" << endl;
	cout << "\t(4) Exit (do nothing)" << endl;
	cin >> option;
	switch (option)
	{
		case 1:
			yearsWorking = (retireAge-age);
			cout << "You have " << yearsWorking << " years until retirement.";
			break;
		case 2:
			cout << "How much do you make per week in dollars? ";
			cin >> salary;
			yearsWorking = (retireAge-age);
			earnings = (yearsWorking * (52 * salary));
			cout << "You will earn $" << earnings << " between now and retirement.";
			break;
		case 3:
			cout << "How much do you make per week in dollars? ";
			cin >> salary;
			cout << "What percentage will you save? ";
			cin >> savePercentage;
			savings = (earnings * (savePercentage/100));
			cout << "You will have $" << savings << " when you retire.";
			break;
		case 4:
			break;
	}
	
}

This worked fine for me, you need to specify a return type for your main function...
I.E.

int halt;
cin>>halt;
return halt;

The other thing my compiler said

(37,43) Converting to int from double

because your using a double and a int in a math function its going to return a double and your trying to set it equal to a int... That might be why your getting some weird problems.

commented: Thank You! +0

Thanks Sassy that did it!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.