Two questions

First, I'm getting ambiguous cout error but I cannot what's causing it. Can someone find it?

Second, on Line 23, I know there isn't a null character in strings. So, what would I put to represent there not being a character at direction.at(1)? Pretty much how can i keep the if loop from happening if there isn't a second character?

*Note line 16-21 is one line in my program. I just broke it down so it wouldn't stretch the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
	char horzL = 'B';
	int vertL = 5;
	int invalidMoves = 0;
	string direction = "UR";
	int numOfSpaces = 2;

	cout << horzL << vertL << endl;
	
	if ( !( 
( (direction.at(0) == 'L' || direction.at(1) == 'L') && horzL - numOfSpaces < 'A' ) 
|| ( (direction.at(0) == 'R' || direction.at(1) == 'R') && horzL - numOfSpaces > 'H' ) 
|| (direction.at(0) == 'U' && vertL + numOfSpaces > 8) 
|| (direction.at(0) == 'D' && vertL - numOfSpaces < 1)
           )  )
	{
		if (direction.at(1) != '/0')
		{
			switch (direction.at(1)) //Second 
			{
			case 'L':
				horzL -= numOfSpaces;
				break;
			
			case 'R':
				horzL += numOfSpaces;
				break;
			
			default:
				cout << "critical error.";
			}
		}
		
		switch (direction.at(0)) //First
		{
		case 'U':
			vertL += numOfSpaces;
			break;
			
		case 'D':
			vertL -= numOfSpaces;
			break;
		
		case 'L':
			horzL -= numOfSpaces;
			break;
		
		case 'R':
			horzL += numOfSpaces;
			break;
		
		default:
			cout << "critical error.";
		}
	}
	else 
	{
		invalidMoves++;
	}
	
	cout << invalidMoves << endl;
	cout << horzL << vertL;
	getch();
	return 0;
}
Last edited on
Post error here.
There's no error. It complies fine. It has the error line under it and say its ambiguous when you roll over it (visual C++ 2010). Sometime it goes away for no reason than it comes back later.
closed account (3qX21hU5)
If you want to determine is a std::string is only 1 characters long or 2 or more you can use the length or size member function which would look like this direction.length(). That returns the length of the string.
lol Zereo I was trying that just as you posted. Also, the ambiguous went away so it must have been unbalanced parenthesis or something.

This is what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
	char horzL = 'B';
	int vertL = 5;
	int invalidMoves = 0;
	string direction = "U";
	int numOfSpaces = 2;

	cout << horzL << vertL << endl;
	
	if ( !( (direction.at(0) == 'L' && horzL - numOfSpaces < 'A')
|| (direction.at(0) == 'R' && horzL - numOfSpaces > 'H')
|| (direction.at(0) == 'U' && vertL + numOfSpaces > 8)
|| (direction.at(0) == 'D' && vertL - numOfSpaces < 1) ) )
	{
		if (direction.length() == 2)
		{
			if ( (direction.at(1) == 'L' && horzL - numOfSpaces < 'A') 
                        || (direction.at(1) == 'R' && horzL - numOfSpaces > 'H' ) )
			{
				switch (direction.at(1)) //Second
				{
				case 'L':
					horzL -= numOfSpaces;
					break;
				
				case 'R':
					horzL += numOfSpaces;
					break;
				
				default:
					cout << "critical error.";
				}
			}
Topic archived. No new replies allowed.