Need desperate C++ help, FH-OT, you've got 50 minutes!

Manisch Depressiv

Part of the furniture
Joined
Mar 6, 2005
Messages
7,727
Nothing of it is C++ though as it should be, so either change topic to "Need desperate C help" or give C++ code :p.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
const int RAINY = 1;
const int WINDY = 2;
const int SUNNY = 3;
const int TRUE = 1;
const int FALSE = 0;

int weather, take_brolly, take_overcoat, take_sunglasses;
take_brolly = take_overcoat = take_sunglasses = FALSE;

if (weather == RAINY)
 take_brolly = TRUE;
else if (weather == WINDY)
 take_overcoat = TRUE;
else if (weather == SUNNY)
 take_sunglasses = TRUE;
else
 printf("ERROR: unknown type of weather\n");

Anyone want to convert this to the equivalent switch construct?
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
#include<stdio.h>

void main()

{
int CarAge, EngineSize, Convictions;

printf("Please enter the age of your Car, followed by it's engine size and finally the amount of convictions you have had: \n");
scanf("%i%i%i" &CarAge, &EngineSize, &Convictions);


   if (CarAge >= 15 && EngineSize >= 2000 && Convictions >= 3)
	   printf("Policy loaded by 45%");
   else if (CarAge >= 15 && EngineSize >= 2000 && Convictions < 3)
       printf("Policy loaded by 20%");
   else if (CarAge < 15 && EngineSize < 2000 && Convictions < 3)
       printf("Policy loaded by 0%");

}

omg what is wrong with this code... nothing. error C2296: '&' : illegal, left operand has type 'const char [7]' gays
 

crispy

Can't get enough of FH
Joined
Mar 9, 2004
Messages
2,706
Maybe its because the data you type in is stored as chars and not integers. Try casting the input from from chars to integers.

I have no C++ experience, so its just my two cents from a java perspective :p
 

Syri

FH is my second home
Joined
Jan 4, 2004
Messages
1,019
ok, the switch function is designed to check for several specific possible entries for a variable, for example, choosing options from a menu.
So for example, we could have a program where the user is prompted to enter a number between 1 and 5, and that is stored in the variable iMenuSelection.
Instead of writing an if statement for each option, we can just use switch, and write a case for each one, which is much easier.
the if statement version would be something like
Code:
if (iMenuSelection == 1)
{
   printf("One selected");
}
else if (iMenuSelection == 2)
{
   printf("Two selected");
}
else if (iMenuSelection == 3)
{
   printf("Three selected");
}
else if (iMenuSelection == 4)
{
   printf("Four selected");
}
else if (iMenuSelection == 5)
{
   printf("Five selected");
}
else
{
   printf("You didn't enter a valid number");
}

For a five option one, with just a single line for each option, it's not THAT bad, but imagine expanding the scope, and the actions taken... it'd get messy quick. Therefore, the switch version below is better.
Code:
switch (iMenuSelection)  // start the switch, using the variable
{
   case 1:  // if it's equal to 1...
   {
      printf("One selected");
   {
   break;
   case 2:  // if it's equal to 2...
   {
      printf("Two selected");
   {
   break;
   case 3:  // if it's equal to 3...
   {
      printf("Three selected");
   {
   break;
   case 4:  // if it's equal to 4...
   {
      printf("Four selected");
   {
   break;
   case 5:  // if it's equal to 5...
   {
      printf("Five selected");
   {
   break;
   default:  // if it doesn't match any of the above
   {
      printf("You didn't enter a valid number");
   {
}
Hopefully that should be easy enough to follow and adapt from there.
You don't have to use the curly brackets on single line responses, but I tend to anyway as it makes no performance difference, but looks tidier.
The most important thing is to include the "break;" after each case, as without that, it will execute all of the other cases below that one until it reaches a break, or the end of the switch. so for example, if there were no breaks in the above, and you entered 3, it would output "Three selectedFour selectedFive selectedYou didn't enter a valid number"

hope this is some help, the reason I won't just solve your own problem is because I've found I never learn anything from looking at someone else's, it's much better absorbed to take an example and integrate your own code to use it.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Thanks again Syri. I managed to do it alright in the class, but it was a bit of guess work because i just didn't really understand what the switch statement did tbh, so that's a lot of help :)
 

Users who are viewing this thread

Top Bottom