[edit] ok everything works perfectly thanks for all the help!
Printable View
[edit] ok everything works perfectly thanks for all the help!
C and W are undefined identifiers. 'C' and 'W' on the other hand would be character constants.
if (surface_code == 'C')
Same for the others. Characters must be surrounded by single quotes '' and strings must be surrounded by double quotes "", otherwise the compiler treats them like variables or functions.
wow, you guys are right. It works now. Thanks a bunch!
Here is my code again, I fixed the first problem but now when I echo the initial height it shows a number like 0.94 which is what the update statement is producing. How do I set it so that it echos the intial height that the user inputs, while still working the way it does?
here is the output by the way,Code:// File: Golf_Ball.cpp
// Author: ----------------
// Class: ITCS 1214 TR 5pm
// Date: 03/20/08
// Purpose: To determine number of bounces a golf ball
// will make given type of surface and height from which
// it is dropped.
#include <iostream>
using namespace std;
int main ()
{
string surface_name = " "; // Name of each type of surface
float bounce_counter = 0.0,
height; // Height given by user
char surface_code; // Code for each type of surface, C, W, or R
// Prompt for and read in height in feet
cout << "Enter the drop height (in feet): ";
cin >> height;
// Prompt for and read in type of surface
cout << "Enter the type of surface (C, W or R): ";
cin >> surface_code;
// Test for which type of surface entered and how many bounces will occur
if (surface_code == 'C')
{
while (height > 1)
{
height = height * 0.85;
bounce_counter = bounce_counter + 1;
surface_name = "concrete";
}
}
else
{
if (surface_code == 'W')
{
while (height > 1)
{
height = height * 0.60;
bounce_counter = bounce_counter + 1;
surface_name = "wooden";
}
}
else
{
while (height > 1)
{
height = height * 0.20;
bounce_counter = bounce_counter + 1;
surface_name = "rug";
}
}
}
// Echo surface type and height, then display number of bounces
cout << "On a " << surface_name << " floor from " << height;
cout << " feet the ball will bounce " << bounce_counter << " times.\n";
return 0;
}
Enter the drop height (in feet): 15
Enter the type of surface (C, W or R): C
On a concrete floor from 0.946701 feet the ball will bounce 17 times.
Everything is right, except the 0.94 should just be 15 again.
Look at where you are echoing your output. It's after you have made all your calculations! You probably need to create another local variable for height (say, input_height), and set it equal to the input height value right after the user enters that value.