Thread: please help me with this distance problem of inches and feet

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    please help me with this distance problem of inches and feet

    Hi

    Please check the code given below. The following are the errors I get. I don't get the reason for the bold errors.

    Code:
    reference to 'distance' is ambiguous|
    candidates are: struct distance|
    template<class _InputIterator> typename std::iterator_traits::difference_type std::distance(_InputIterator, _InputIterator)|
    reference to 'distance' is ambiguous|
    candidates are: struct distance|
    template<class _InputIterator> typename std::iterator_traits::difference_type std::distance(_InputIterator, _InputIterator)|
    expected ';' before 'd1'|
    'd1' was not declared in this scope|
    expected '}' before 'else'|
    expected unqualified-id before 'return'|
    expected constructor, destructor, or type conversion before '(' token|
    expected declaration before '}' token|
    Edit: I have corrected one of the bold errors - the one with the else block.

    Code:
    /* Define distance structure, int feet, float inches. Read two distances. Add them to find sum */
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////////////
    struct distance
    { int feet; int inches; };
    ////////////////////////////////////////////////////
    
    int main()
    
    {
        distance d1; int x, y, z;
    
    	cout<<"enter feet: "; cin>>d1.feet;
    
    	cout<<"enter inches: "; cin>>d1.inches;
    
            {
    
    	if (d1.inches<12)
            {
            cout << "the distance is: " << d1.feet << "." <<d1.inches << endl;
            }
            
            else
            {
            x = d1.inches % 12;
            y = d1.inches - x;
            z = y/12;
            d1.feet += z;
    
            cout << "the distance is: " << d1.feet << "." << x << endl;
            }
    
            }
    
        return 0;
    
        system("pause");
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    std::distance is the name of a function in the C++ standard library (in header <iterator>, IIRC).

    Presumably either <iostream> or <cstdlib> include <iterator> for your compiler/library. While that is not required by the standard, it is also not forbidden.

    Two options to fix your problem are

    1) The professional approach: remove the "using namespace std;" line from your code (and explicitly specify cout as std::cout).

    2) The amateur approach: rename distance to something else that does not clash with a name in the standard library, cross fingers, and hope some other name you use in future code does not clash with the standard library.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is a guess from an C Programmer working on learning C++

    Code:
    using namespace std;
    I am guessing distance is used in many std namespaces; I suggest trying one of the following. Try option 2 first since it should be faster.

    1. Remove the using namespace and see if Error goes away.

    2. Try renaming your structure with an capital "D" at the start.

    Tim S.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, everyone. You people are so nice.

    It's fixed now. I simply changed "distance" to "dis" and it works fine. I used amateur approach because I'm one!

    Is there another simple and short amateur way to do the following code? Please let me know. Thank you.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////
    struct date
    { int d, m, y; };
    ////////////////////////////////////////////
    
    int main()
    
    {
        system("cls");
    
        date d1;
    
    	cout << "enter date: "; cin >> d1.d;
    
    	cout << "enter month: "; cin >> d1.m;
    
    	cout << "enter year: "; cin >> d1.y;
    
    	cout << endl;
    
    	switch(d1.d)
            {
    	    case 1: cout << "1st";
    		break;
    		case 2: cout << "2nd";
    		break;
    		case 3: cout << "3rd";
    		break;
    		case 4: cout << "4th";
    		break;
    		case 5: cout << "5th";
    		break;
    		case 6: cout << "6th";
    		break;
    		case 7: cout << "7th";
    		break;
    		case 8: cout << "8th";
    		break;
    		case 9: cout << "9th";
    		break;
    		case 10: cout << "10th";
    		break;
    		case 11: cout << "11th";
    		break;
    		case 12: cout << "12th";
    		break;
    		case 13: cout << "13th";
    		break;
    		case 14: cout << "14th";
    		break;
    		case 15: cout << "15th";
    		break;
    		case 16: cout << "16th";
    		break;
    		case 17: cout << "17th";
    		break;
    		case 18: cout << "18th";
    		break;
    		case 19: cout << "19th";
    		break;
    		case 20: cout << "20th";
    		break;
    		case 21: cout << "21th";
    		break;
    		case 22: cout << "22th";
    		break;
    		case 23: cout << "23th";
    		break;
    		case 24: cout << "24th";
    		break;
    		case 25: cout << "25th";
    		break;
    		case 26: cout << "26th";
    		break;
    		case 27: cout << "27th";
    		break;
    		case 28: cout << "28th";
    		break;
    		case 29: cout << "29th";
    		break;
    		case 30: cout << "30th";
    		break;
    		case 31: cout << "31th";
            }
    
            cout << " ";
    
    	switch(d1.m)
            {
    	    case 1: cout << "January" << " " << d1.y; break;
    		case 2: cout << "Feburary" << " " << d1.y; break;
    		case 3: cout << "March" << " " << d1.y; break;
    		case 4: cout << "April" << " " << d1.y; break;
    		case 5: cout << "May" << " " << d1.y; break;
    		case 6: cout << "June" << " " << d1.y; break;
    		case 7: cout << "July" << " " << d1.y; break;
    		case 8: cout << "August" << " " << d1.y; break;
    		case 9: cout << "September" << " " << d1.y; break;
    		case 10: cout << "October" << " " << d1.y; break;
    		case 11: cout << "November" << " " << d1.y; break;
    		case 12: cout << "December" << " " << d1.y; break;
            }
    
            return 0;
    
    	system("pause");
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Just a nit-pick, but many of your case statements are incorrect. 21th, 22th, 23th, 31th?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Raigne View Post
    Just a nit-pick, but many of your case statements are incorrect. 21th, 22th, 23th, 31th?
    Raigne, thanks a lot for pointing this out.

    Is there some other simple and short method to do that problem? Please let me know if there is one. Thank you.
    Last edited by jackson6612; 05-08-2011 at 01:38 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like this perhaps.
    Code:
    	cout << d1.d;
    	switch(d1.d)
            {
    	    case 1: case 21: case 31: cout << "st"; break;
    	    case 2: case 22: cout << "nd"; break;
    	    case 3: case 23: cout << "rd"; break;
    	    default: cout << "th"; break;
    Also, consider using an array of month names, and using d.m as the index into that array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Salem View Post
    Also, consider using an array of month names, and using d.m as the index into that array.
    Thanks a lot, Salem.

    I have never used arrays so far. Could you please provide me the array and index for month names so that I could try to learn about this new idea in the context of this present problem? I think I would be able to understand arrays better this way. If you have time, then do me this favor. Otherwise, it's okay.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. meters to feet inches (rounding inches)
    By protofarmer720 in forum C Programming
    Replies: 3
    Last Post: 11-06-2010, 03:55 PM
  2. Replies: 12
    Last Post: 07-05-2010, 07:57 PM
  3. Replies: 2
    Last Post: 12-09-2009, 07:49 PM
  4. How to convert inches into pixels?
    By jat421 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2005, 02:32 PM