Switch that almost works

Apr 6, 2009 at 6:43pm
Good day to all, I am attempting to construct a program that will accept input of lbs or stones in ordered to calculate the end reuslt and display that result. New C++ Child and attempting to find my way. Confusion is still pretty high.Here is the code I ahve written so far and at present i am stuck without completion. Any suggestions of guidance is muchly apperciated.

:#include <iostream>
using namespace std;
int main() //start of main function
{
float s; // input: number of stones
float lbs; // output: in pounds
int (sOrLbs);

// get stones or pounds
cout << " Enter the number of stones by entering s or enter lbs if weight in pounds: ";

switch (sOrLbs)
{
case 'S': case 's':
cout << "Enter the number of stones" << endl;
cin >> s;
break;
case 'P': case 'p':
cout << "Enter the weight in lbs" << endl;
cin >> lbs;
break;
}

// calculate lbs = Weight in stones * pounds
lbs = s * 14;
s = lbs / 14;

system ("cls");

//output
cout << " Weight in stones: " << " = " << " " << s <<endl;
cout << " Weight in pounds " << "=" << " "<< lbs << endl;

Apr 6, 2009 at 7:05pm
Please use [code] ... [/code] blocks.

The sOrLbs appears that it should be a char. You must also give clear instructions and accept input before switching on it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main()
  {
  float stones;
  float pounds;
  char S_or_P;

  cout << "Enter\nS for stones or\nP for pounds\n> " << flush;
  cin >> S_or_P;

  switch (S_or_P)
    {
    case 'S': case 's':
      cout << "Enter the number of stones> " << flush;
      cin >> stones;
      pounds = stones * 14;
      break;

    case 'P': case 'p':
      cout << "Enter the weight in lbs> " << flush;
      cin >> pounds;
      stones = pounds / 14;
      break;

    default:
      cout << "Hey there, I don't know what '" << S_or_P << "' means.\n";
      return 1;
    }

  cout << "Number of stones = " << stones << endl;
  cout << "Weight in pounds = " << pounds << endl;

  return 0;
  }

Hope this helps.
Last edited on Apr 6, 2009 at 7:06pm
Apr 6, 2009 at 7:38pm
Thank You very much, the light in tunnels is getting bright through your help. Very thankful for your input.
MDGDOC
Topic archived. No new replies allowed.