Pretty simple for you good programers...

This is a discussion on Pretty simple for you good programers... within the C++ Programming forums, part of the General Programming Boards category; Im a newbie of course and im taking a class to learning c++. We are in the functions chapter and ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    Unhappy Pretty simple for you good programers...

    Im a newbie of course and im taking a class to learning c++. We are in the functions chapter and this is my code which i have made so far.... i have no idea where to go what to do etc.

    btw, I am doing problem 3.12
    [Source Code]


    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    Code:
    #include <iomanip>
    
    using std::setprecision;
    using std::setiosflags;
    using std::setw;
    
    
    int calculateCharges ( int )  //function prototype
    
    int main()
    {
    
    	int hr1,    //first hour
    	    hr2,    //second hour
    	    hr3,    //third hour
    	    ttlhr,  //Total hours
    	    cr1,    //First charge
    	    cr2,    //second charge
    	    cr3,    //third charge
    	    ttlcr;  //Total charge 
    
    	cout << "Enter 3 hours: " ;
    	cin >> hr1 >> hr2 >> hr3;
    
    	ttlhr = hr1 + hr2 + hr3;
    
    	cout << "Car" << setw( 10 ) << "Hours" << setw( 10 ) << "Charge"
    	     << "\n  1" << setw( 10 ) << hr1 << setw( 10 ) <<  
    	     << "\n  2" << setw( 10 ) << hr2 << setw( 10 ) <<
    	     << "\n  3" << setw( 10 ) << hr3 << setw( 10 ) <<
    	     << "\nTotal" << setw( 10 ) << ttlhr << setw( 10 ) << ttlcr;
    
    
    return 0;
    }
    
    
    //function definition
    int calculateCharges ( int hours )
    
    {
    	int crg = 0;
    	
    	if ( hours <= 3 )
    	crg = 2;
    
    	if ( hours > 3 )
    	crg = ( ( hours - 3 ) * .5 ) + 2
    
    	if ( hours == 24 )
    	crg = 10;
    
    	return crg;
    }
    This is the original problem out of the book:
    http://members.cox.net/eazhar/prob3.jpg
    Last edited by eazhar; 06-28-2002 at 01:57 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    That's an interesting use of code tags, useing them after the start of your code...
    Anyway, w/o reading the original problem, you have an error right off the bat with your if statements.
    You check if hours are greater than 3, and if they equal 24. Both conditions can be true. You might look at else if or case statements.
    You also never call calculateCharges in main, but maybe that's elsewhere in the problem.
    Truth is a malleable commodity - Dick Cheney

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    New code

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <iomanip>
    
    using std::setprecision;
    using std::setiosflags;
    using std::setw;
    
    
    double calculateCharges ( double );  //function prototype
    
    int main()
    {
    
    	double   hr1,    //first hour
    	         hr2,    //second hour
    	         hr3,    //third hour
    	         ttlhr,  //Total hours
    	   	     cr1,    //First charge
    		     cr2,    //second charge
    		     cr3,    //third charge
    		     ttlcr;  //Total charge 
    
    	cout << "Enter 3 hours: " ;
    	cin >> hr1 >> hr2 >> hr3;
    
    	cr1 = calculateCharges(hr1);
    	cr2 = calculateCharges(hr2);
    	cr3 = calculateCharges(hr3);
    
    	ttlcr = cr1 + cr2 + cr3;
    	ttlhr = hr1 + hr2 + hr3;
    
    	cout << "Car" << setw( 10 ) << "Hours" << setw( 10 ) << "Charge"
    	     << "\n  1" << setw( 10 ) << hr1 << setw( 10 ) <<  cr1
    	     << "\n  2" << setw( 10 ) << hr2 << setw( 10 ) <<  cr2
    	     << "\n  3" << setw( 10 ) << hr3 << setw( 10 ) <<  cr3
    	     << "\nTotal" << setw( 10 ) << ttlhr << setw( 10 ) << ttlcr;
    
    
    return 0;
    }
    
    
    //function definition
    double  calculateCharges ( int hours )
    
    {
    	double crg = 0;
    	
    	if ( hours <= 3 ){
    	crg = 2.0;
    	}
    
    	if ((hours > 3.0) && (hours < 24.0)) {
    	crg = ( ( hours - 3.0 ) * .5 ) + 2.0;
    	}
    
    	if ( hours == 24.0 )
    	crg = 10.0;
    
    	return crg;
    }
    2 errors-
    cred.obj : error LNK2001: unresolved external symbol "double __cdecl calculateCharges(double)" (?calculateCharges@@YANN@Z)

    Debug/cred.exe : fatal error LNK1120: 1 unresolved externals




    UPDATE: Nevermind i found my problem

    double calculateCharges ( int hours ) should be double hours not int
    Last edited by eazhar; 06-28-2002 at 03:04 PM.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In fact there is just one error, the second error is the result of the first error.

    The prototype has double as input parameter, the implementation has int. So the compiler thinks of this as two different functions. The first is not implemented, the second not prototyped.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    2nd question

    okay now im doing the second question on the link i've posted (3.13)

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <iomanip>
    
    using std::setw;
    
    #include <cmath>
    
    
    
    double roundNum ( double );  //function prototype
    
    int main()
    {
    
    	double num;  //inputted number
    	int res;     //outputted result
    
    
    	cout << "Enter number: ";
    	cin >> num;
    
    	res = roundNum ( num );	
    
    	cout << "Rounded result is: " << res << endl;
    	cout << "\n";
    	
    
    	return 0;
    }
    
    
    //function definition
    double roundNum ( double x )
    {
    
    int y = 0;
    
    y = floor( x + .5 );
    
    	return x;
    }
    Its executes and everything great but the instructions want it to be able to process several numbers.

    I was thinking of using a while loop but im a bit confused on where i am supposed insert it

    Thanks for any replies.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    double num = 0;
    
    while (num != -1)
    {
    	cout << "Enter number (-1 to exit): ";
    	cin >> num;
       	
    	res = roundNum ( num );	
       	
    	cout << "Rounded result is: " << res << endl;
    	cout << "\n";
    }
    Couple of other problems...

    - The res variable is an int, which conflicts with the return type of the roundNum() function. Suggest changing the function return type.
    - In roundNum() you are returning x, whereas I think you meant to return y.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  2. A good, simple web editor
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-19-2003, 07:20 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. good place for some simple c++ source?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-05-2002, 04:45 PM
  5. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21