Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Case Switch Question

Ask ALL your questions here

Case Switch Question

Postby DreamscapeEagle on Wed Oct 21, 2009 6:23 pm

Most of you know I'm pretty new at C++, so bear with me please. I have this assignment that I am to write the code for. An employer wants to be able to enter in number of years employed and show weeks of vacation allowed. 0 years, 0 weeks. Years 1-5, 1 week. Years 6-10, 2 weeks. Years 11 and over get 3 weeks. I'm OK until I get to the 11 and over part. I have tried all I know and nothing works.
I'm not asking for the answer, just a hint or two. I like to work out the problem so I can learn, but this is over my head.

//Ch6AppE05.cpp
//Displays the number of vacation weeks due an employee
//Created/revised by Paul Lawry on 10/20/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int main()
{ //declare constants
int yearsWithTheCompany = 0;
int weeksOfVacation = 0;

//enter input data
cout << "Years with the company: ";
cin >> yearsWithTheCompany;

//assign vacation time
switch (yearsWithTheCompany)
{
case 0:
weeksOfVacation = 0;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
weeksOfVacation = 1;
break;
case 6:
case 7:
case 8:
case 9:
case 10:
weeksOfVacation = 2;
break;
default:
cout << "Invalid years" << endl;

} //end switch

//display weeks of vacation
cout << "Weeks of vacation: " << weeksOfVacation << endl;

return 0;
} //end of main function

Paul
Paul Lawry
Abilene TX
Fulltime in a 1968 Silver Eagle Bus Conversion Model 01
User avatar
DreamscapeEagle
 
Posts: 6
Joined: Tue Oct 13, 2009 6:38 pm
Location: Abilene Texas

Re: Case Switch Question

Postby noobgrammer on Wed Oct 21, 2009 6:50 pm

You would want to use if statements instead of switches

Code: Select all
if (yearsWithTheCompany >= 11)
std::cout<< "You get three weeks\n";


since you want to figure it out I won't show how to do the others up to it unless you need help with that then I will

I noticed one of your comments you called them constants but they are not constants in C++

Code: Select all
//declare constants
int yearsWithTheCompany = 0;

const int unchangeable  = 0 // this would be a const in C++


this is better

Code: Select all
using std::cout;
using std::cin;
using std::endl;

than
Code: Select all
using namespace std;


but to use it better would be to put it inside of your main helps eliminate globals
that was just a helpful comment there

please try to use the code tags it makes it cleaner and easier for us to copy and put into our compiler.

if any part of this confuses you just ask
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Case Switch Question

Postby DreamscapeEagle on Wed Oct 21, 2009 6:59 pm

Thanks, I'll go back and make them if's, no problem.

What do mean by code tags?
Paul Lawry
Abilene TX
Fulltime in a 1968 Silver Eagle Bus Conversion Model 01
User avatar
DreamscapeEagle
 
Posts: 6
Joined: Tue Oct 13, 2009 6:38 pm
Location: Abilene Texas

Re: Case Switch Question

Postby noobgrammer on Wed Oct 21, 2009 7:06 pm

when you post edit or reply you will notice some options like a bold or quote buttons and one of them is the code buttom if you click on it and put your code between them you can always click the preview button to see if it worked before submitting

click here if your not sure
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Case Switch Question

Postby DreamscapeEagle on Thu Oct 22, 2009 12:00 pm

OK, got it.

I could not use the case switch, ended up using else/if. Code is below

[code]#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int main()
{
//declare constants
int yearsWithTheCompany = 0;
int weeksOfVacation = 0;

//enter input data
cout << "Years with the company: ";
cin >> yearsWithTheCompany;

//assign vacation time

if (yearsWithTheCompany <= -1)
cout << "Input error" << endl;
else if (yearsWithTheCompany == 0)
cout << "Weeks of vacation: 0";
else if (yearsWithTheCompany <= 5)
cout << "Weeks of vacation: 1";
else if (yearsWithTheCompany <= 10)
cout << "Weeks of vacation: 2";
else if (yearsWithTheCompany >= 11)
cout << "Weeks of vacation: 3";
else

//end ifs

return 0;
} //end of main function
[code]
Paul Lawry
Abilene TX
Fulltime in a 1968 Silver Eagle Bus Conversion Model 01
User avatar
DreamscapeEagle
 
Posts: 6
Joined: Tue Oct 13, 2009 6:38 pm
Location: Abilene Texas

Re: Case Switch Question

Postby noobgrammer on Thu Oct 22, 2009 12:19 pm

not bad you don't need this header though

Code: Select all
#include <iomanip>


and you almost have it with the code tag the bottom code put a / before code so you would have /code between [] the bottom one only
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests