1. ## 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");

}```

2. 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.

3. 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. 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");
}```

5. Just a nit-pick, but many of your case statements are incorrect. 21th, 22th, 23th, 31th?

6. Originally Posted by Raigne
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.

7. 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.

8. Originally Posted by Salem
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

Popular pages Recent additions