5 Replies - 2510 Views - Last Post: 22 September 2010 - 11:58 AM Rate Topic: -----

#1 evilcook818  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 10

I need help with the char and switch statement idea

Posted 22 September 2010 - 11:29 AM

The program should prompt the user for the appropriate information: a character code for the pet type, and a yes/no response for the neutered status. Use a code letter to determine the kind of animal (i.e. D or d represents a dog, C or c represents a cat, B or b represents a bird, R or r represents a reptile, and anything else represents some other kind of animal). Any others animals beside those listed are invalid. Use a code letter to determine the neutered status(i.e. Y or y represents yes, N or n represents no).



#include <iostream>
using namespace std;

int main()
{
  char neutered, opselect, animal;
  const int dogcost = 80, catcost = 60, birdOrReptilecost = 0; 

  cout << "What kind of animal do you have?\n";
  cout << "d for dog, c for cat, b for bird or reptile and o for others.\n\n";
  cin >> animal;



  cout << "Answer the following questions\n";
  cout << "With Y for yes or N for no.\n\n";
  cout << "Is your pet neutered? ";
  cin >> neutered;


This is how I start but I know that I did something wrong but I cannot figure it out, because thing dont run correctly. I tried to "char d = dog" but it did not work also. What Can I do to make D equal to dog and from that I can compute the price...

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 22 September 2010 - 12:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I need help with the char and switch statement idea

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: I need help with the char and switch statement idea

Posted 22 September 2010 - 11:37 AM

Well yes, you do need a switch. A switch case can capture more than one input.. so setting the case statements to 'd' and 'D' will work for you.

Check this out:
http://www-numi.fnal...rib/switch.html

I am unclear what you are doing with "char d = dog", but I think it is taken care of by the switch.

Inside your switch statement I would figure you do your specific math to get your total, and then output it right?
Was This Post Helpful? 1
  • +
  • -

#3 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: I need help with the char and switch statement idea

Posted 22 September 2010 - 11:39 AM

Let me give you an example of switch-case
char letter;
// letter Initialization

switch (letter)
{
   case 'a':
   case 'A':
      cout << "apple" << endl;
      break;
   
   case 'b':
   case 'B':
      cout << "boy" << endl;
      break;

   default:
      cout << "i have no clue..." << endl;
      break;
}


This post has been edited by Splatocaster: 22 September 2010 - 11:39 AM

Was This Post Helpful? 1
  • +
  • -

#4 evilcook818  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 10

Re: I need help with the char and switch statement idea

Posted 22 September 2010 - 11:46 AM

Write a program that prints the insurance fee to pay for a pet according to the following rules:
(Note: must use a switch statement to determine pet fee)
A dog that has been neutered costs $50.
A dog that has not been neutered costs $80.
A cat that has been neutered costs $40.
A cat that has not been neutered costs $60.
A bird or reptile costs nothing.
Any other animal generates an error message.

The program should prompt the user for the appropriate information: a character code for the pet type, and a yes/no response for the neutered status. Use a code letter to determine the kind of animal (i.e. D or d represents a dog, C or c represents a cat, B or b represents a bird, R or r represents a reptile, and anything else represents some other kind of animal). Use a code letter to determine the neutered status(i.e. Y or y represents yes, N or n represents no). The user should be allowed to enter the input in either upper or lower case.
It prints out the type of animal (full name of animal) and the insurance fee. Any error in input data should generate an error message “Invalid data – no fee calculated”


That was the question and this is what I have so far but its not correct somehow.

#include <iostream>
using namespace std;

int main()
{
  char neutered, opselect, animal;
  const int dogcost = 80, catcost = 60, birdOrReptilecost = 0; 

  cout << "What kind of animal do you have?\n";
  cout << "d for dog, c for cat, b for bird or reptile and o for others.\n\n";
  cin >> animal;

  
  
  cout << "Answer the following questions\n";
  cout << "With Y for yes or N for no.\n\n";
  cout << "Is your pet neutered? ";
  cin >> neutered;
  
  cout << "Enter a select code: ";
  cout << "\n1)A dog that has been neutered: ";
  cout << "\n2)A dog that has not been neutered: ";
  cout << "\n3)A cat that has been neutered costs: ";
  cout << "\n4)A cat that has not been neutered costs: ";
  cout << "\n5)A bird or reptile: ";
  
  cin >> opselect;

  switch (opselect)
  {
   case 1:
     cout << "The price for dog that has been neutered is " << dogcost - 30;
	 break;
   case 2:
	 cout << "The price for dog that has not been neutered is " << dogcost << endl;
    break;
   case 3:
	 cout << "The price for cat that has been neutered is " << catcost - 20 << endl;
    break;
   case 4:
	 cout << "The price for cat that has not been neutered is " << catcost << endl; 
	break;
   case 5:
	 cout << "The price for bird or reptile is " << birdOrReptilecost << endl;
	break;
   


  return 0;
}


MOD EDIT: AGAIN, when posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 22 September 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: I need help with the char and switch statement idea

Posted 22 September 2010 - 11:57 AM

As opselect is a char type you should use character to check the case like this:
case '1':

Also you have'nt put a } (before return 0) to end the switch.

This post has been edited by aaa111: 22 September 2010 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: I need help with the char and switch statement idea

Posted 22 September 2010 - 11:58 AM

What do you mean by "That was the question and this is what I have so far but its not correct somehow."?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1