My program do not run properly, what I'm missing??

I have this problem to solve:
Design a program that prompts a user to enter a number representing a measurement and then identify whether it is pounds(enter a P) or yards (enter a Y). The program should convert the measument into either kilograms or meters and output the result as a number with two decimal places.

Use four user-defined functions with the following names that accomplish the following:

.getNumber- obtains from the user a number representing a measument.This function should use a loop to validate that the input is a number greater than zero.


.identifyInput- asks the user to identify the number input as pounds by entering a 'P' or yards by entering a 'Y'. This function should use a predefined function to convert the input to an uppercase letter and use a loop to validate that the input is a 'P' or a 'Y.'

.getKilos- convert pounds to kilograms by multiplying the pounds by 0.45359237 and returns the result.

.getMeters- convert yards to meters by multlpying the yeards by 0.9144 and ruturns the result.

So far I have this, but is not running properly... can you see what is wrong or what I am missing? Thanks:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

//function prototypes
int getNumber (string message);
double getKilos (double);
double getMeters (double);
char identifyInput (string message);


int main()
{

int measure, pounds, yards;
char pickpounds = 'p';
char pickyards = 'y';


do
{
measure = getNumber ("Please enter a number");

}
while (measure != 0);

while (pickpounds == 'p')
{
pickpounds = identifyInput ("Enter p to convert to pounds or y to convert to yards");
pickpounds = tolower(pickpounds);

}
system ("pause");

return 0;

}

int getNumber (string message)
{
int measurement;
cout<<message;
cin>>measurement;
return measurement;
}

char identifyInput (string message)
{
char letter;
cout << message;
cin >> letter;
return letter;
}

double getKilos (double p)
{
double kilos = p * 0.45359237;
return kilos;
}

double getMeters (double y)
{
double meters = y*0.9144;
return y;
}







Last edited on
getnumber should return a double.

If you are to use a do while loop, put the while on the same line as the closing brace of the do. Otherwise it looks like a while with a null statement.

Think about the logic in the test expressions of your loops.

USe a debugger to step through your program watching the values of your variables.

Or use cout statements where you think there might be a problem, to see the values of the variables.

Hope this helps
this is what your program does: (it skips the rest)
you're not even making a call getmetres and to getpounds.
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

int main()
{

int measure, pounds, yards;
char pickpounds = 'p';
char pickyards = 'y';


do
{
measure = getNumber ("Please enter a number");

}
while (measure != 0); // keeps asking until you  say 0

while (pickpounds == 'p')
{
pickpounds = identifyInput ("Enter p to convert to pounds or y to convert to yards");
pickpounds = tolower(pickpounds);

} //keeps asking until you say 'y'
system ("pause");

return 0;

}

so this is what you should do:
i have tested 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//function prototypes
double getNumber (string message);
double getKilos (double);
double getMeters (double);
char identifyInput (string message);


int main()
{

  int measure;
  char yardsOrPounds;

  measure = getNumber ("Please enter a number"); //put your loop inside getNumber

  yardsOrPounds = identifyInput("Enter p to convert to pounds or y to convert to yards");//put loop inside identifyInput

  if ( yardsOrPounds=='p') {
    cout<<measure<<" pounds is equal to "<< setprecision(2) << fixed <<getKilos (measure)<<" kilos."<<endl;
  } else { 
    cout<<measure<<" yards is equal to "<< setprecision(2) << fixed <<getMeters   (measure)<<" Metres."<<endl;
  }

system ("pause");
return 0;
}

double getNumber (string message) {
  double measurement=0;
  while (measurement<=0) {
    cout<<message << endl;
    cin>>measurement;
  }
  return measurement;
}

char identifyInput (string message) {
  char letter='a';//random value
  while ( tolower(letter)!='y' && tolower(letter)!='p' ) {
    cout << message << endl;
    cin >> letter;
  }
  return tolower(letter);
}

double getKilos (double p) {
  double kilos = p * 0.45359237;
  return kilos;
}

double getMeters (double y) {
  double metres = y*0.9144;
  return metres;
}
	

Last edited on
I will analize your answer. Thanks you guys for your support.
Topic archived. No new replies allowed.