Thread: having trouble with some code for my class

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    having trouble with some code for my class

    [edit] ok everything works perfectly thanks for all the help!
    Last edited by green11420; 03-20-2008 at 10:34 AM. Reason: This problem is solved

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    C and W are undefined identifiers. 'C' and 'W' on the other hand would be character constants.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    wow, you guys are right. It works now. Thanks a bunch!

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    one more problem here!

    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?

    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;
    }
    here is the output by the way,

    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.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    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.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by kcpilot View Post
    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.
    Ok I got it working perfectly now. After I prompted the user for the height I set the "initial_height" equal to "height" and that seemed to fix everything. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM