Convert Console Program to Cammand Line

Below is my program in console format. Can anyone tell me how to convert it to a command line program. We didn't really go over it in class so I have no idea how to do it. Also startGamePieceLocation.horzLocation and startGamePieceLocation.vertLocation have to be pulled from the same line as the command to start the program (i.e program.exe A4; im guessing not sure how you do it)

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <iomanip>
#include <cstdlib> 
using namespace std;

struct gamePiece {
  char horzLocation;
  int vertLocation;
  int validMoves;
  int invalidMoves;
} startGamePieceLocation, gamePieceLocation;

struct gamePieceMI {
  string direction;
  int numOfSpaces;
  ;
} moveInfo;

gamePiece gamePieceMover(gamePiece location, gamePieceMI moves);
void displayGameLocation(gamePiece displayLocation);
char isOccupied(gamePiece gpLocation, int horzLine, int vertLine);


int main()
{
	int moveAttempt = 0;
	string line;
	ifstream intData;
	string startPoint;
	intData.open("C:/Users/T_Dub/Desktop/data.txt");

	startGamePieceLocation.horzLocation = 'A';
	startGamePieceLocation.vertLocation = 5;
	gamePieceLocation.horzLocation = startGamePieceLocation.horzLocation;
	gamePieceLocation.vertLocation = startGamePieceLocation.vertLocation;
	gamePieceLocation.invalidMoves = 0;

	if(!intData)
	{
		cout<<"Error opening file, the program will close.\n\n";
		_getch();
		return 0;
	}

	while( !intData.eof() )
	{
		getline(intData, line);
	}
	intData.close();

	intData.open("C:/Users/T_Dub/Desktop/data.txt");
	while ( !( intData.eof() ) )
	{
		intData >> moveInfo.direction >> moveInfo.numOfSpaces;
		cout << "\n\n" << setw(25) << "Previous Location: " << gamePieceLocation.horzLocation << gamePieceLocation.vertLocation << endl;
		cout << setw(20) << "Direction: " << moveInfo.direction << "  " << moveInfo.numOfSpaces << endl;
		gamePieceLocation = gamePieceMover(gamePieceLocation, moveInfo);
		cout << endl << setw(11) << gamePieceLocation.validMoves + gamePieceLocation.invalidMoves << " Total Moves"<< endl;
		cout << setw(9) << gamePieceLocation.validMoves << " Valid Moves Made" << endl;
		cout << setw(8) << gamePieceLocation.invalidMoves << " Invalid Moves Made" << endl << endl;
		cout << setw(25) << "The Gamepiece is now at: " << gamePieceLocation.horzLocation << gamePieceLocation.vertLocation << endl << endl;
		displayGameLocation(gamePieceLocation);
		
	}
	

	cout << endl << endl << endl << setw(19) << "FINAL";
	intData >> moveInfo.direction >> moveInfo.numOfSpaces;
	cout << "\n\n" << setw(25) << "Starting Location: " << startGamePieceLocation.horzLocation << startGamePieceLocation.vertLocation << endl << endl;
	displayGameLocation(startGamePieceLocation);
	cout << endl << setw(12) << gamePieceLocation.validMoves + gamePieceLocation.invalidMoves << " Total Moves"<< endl;
	cout << setw(9) << gamePieceLocation.validMoves << " Valid Moves Made" << endl;
	cout << setw(8) << gamePieceLocation.invalidMoves << " Invalid Moves Made" << endl << endl;
	cout << setw(23) << "Final Location: " << gamePieceLocation.horzLocation << gamePieceLocation.vertLocation << endl << endl;
	displayGameLocation(gamePieceLocation);
	getchar();
	intData.close();
	return 0;
}

gamePiece gamePieceMover(gamePiece location, gamePieceMI moves)
{	
	if (moves.direction.length() == 1)
	{ 
	if ( !( (moves.direction.at(0) == 'L' && location.horzLocation - moves.numOfSpaces < 'A') || (moves.direction.at(0) == 'R' && location.horzLocation - moves.numOfSpaces > 'H') || (moves.direction.at(0) == 'U' && location.vertLocation + moves.numOfSpaces > 8) || (moves.direction.at(0) == 'D' && location.vertLocation - moves.numOfSpaces < 1) ) )
	{
		location.validMoves++;
		cout << setw(26) << "Movement was Valid" << endl;
	} else {
		location.invalidMoves++;
		cout << setw(27) << "Movement was Invalid" << endl;
	}
	}

	if (moves.direction.length() == 2)
	{
	if ( ( !( (moves.direction.at(0) == 'L' && location.horzLocation - moves.numOfSpaces < 'A') || (moves.direction.at(0) == 'R' && location.horzLocation - moves.numOfSpaces > 'H') || (moves.direction.at(0) == 'U' && location.vertLocation + moves.numOfSpaces > 8) || (moves.direction.at(0) == 'D' && location.vertLocation - moves.numOfSpaces < 1) ) ) && ( (moves.direction.at(1) == 'L' && location.horzLocation - moves.numOfSpaces > 'A') || (moves.direction.at(1) == 'R' && location.horzLocation + moves.numOfSpaces < 'H' ) ) )
	{
		location.validMoves++;
		cout << setw(26) << "Movement was Valid" << endl;
	} else {
		location.invalidMoves++;
		cout << setw(27) << "Movement was Invalid" << endl;
	}
	}

	if ( !( (moves.direction.at(0) == 'L' && location.horzLocation - moves.numOfSpaces < 'A') || (moves.direction.at(0) == 'R' && location.horzLocation + moves.numOfSpaces > 'H') || (moves.direction.at(0) == 'U' && location.vertLocation + moves.numOfSpaces > 8) || (moves.direction.at(0) == 'D' && location.vertLocation - moves.numOfSpaces < 1) ) )
	{
		if (moves.direction.length() == 2)
		{
			if ( (moves.direction.at(1) == 'L' && location.horzLocation - moves.numOfSpaces > 'A') || (moves.direction.at(1) == 'R' && location.horzLocation + moves.numOfSpaces < 'H' ) )
			{
				switch (moves.direction.at(1)) //Second
				{
				case 'L':
					location.horzLocation -= moves.numOfSpaces;
					break;
				
				case 'R':
					location.horzLocation += moves.numOfSpaces;
					break;
				
				default:
					cout << "critical error.";
				}
			} else {
				return location;
			}
		}
			
			switch (moves.direction.at(0)) //First
			{
			case 'U':
				location.vertLocation += moves.numOfSpaces;
				break;
			
			case 'D':
				location.vertLocation -= moves.numOfSpaces;
				break;
			
			case 'L':
				location.horzLocation -= moves.numOfSpaces;
				break;
			
			case 'R':
				location.horzLocation += moves.numOfSpaces;
				break;
			
			default:
				cout << "critical error.";
			}
		}
	return location;
}

void displayGameLocation(gamePiece displayLocation)
{
	int horzLine;
	int vertLine;

	for (vertLine=8; vertLine>=1; vertLine--)
	{
		cout << vertLine << setw(4);
		
		for (horzLine=1;horzLine<=8; horzLine++)
		{
			cout << setw(4) << isOccupied(displayLocation, horzLine, vertLine);
		}
		
		cout<<endl;
	}
	cout << setw(5) << "A" << setw(4) << "B" << setw(4) << "C" << setw(4) << "D" << setw(4) << "E" << setw(4) << "F" << setw(4) << "G" << setw(4) << "H" << endl;
	return;
}

char isOccupied(gamePiece gpLocation, int horzLine, int vertLine)
{
	char occupied;
	

	if ( ( (gpLocation.horzLocation - 64) == horzLine) && (gpLocation.vertLocation == vertLine) )
	{
		occupied = 'X';
		return occupied;
	} else {
		occupied = '0';
		return occupied;
	}
}
Last edited on
How would you describe a difference between "console program" and "command line program"?

EDIT: Oh, I get it now:
http://www.cplusplus.com/forum/articles/13355/
Last edited on
I think what s/he meant to ask is: "how would you write a program where you can specify its internal arguments from the command line e.g. program_name.exe -h "this" -t "that" ?" or something like that...
@JockX yeah the int main (int argc, char* argv[]) is what I was looking for.

@Matrix X No not that. I believe it would be more like program_name.exe text_file_name.txt startingLocation but I don't know how to start programs in the command line.

Starting location should be a string "A4" (for example). Then I need to store
startingLocation.at(0) into startGamePieceLocation.horzLocation and startingLocation.at(1) into startGamePieceLocation.vertLocation as an integer.

So something like:

1
2
3
4
5
6
7
int main (int argc, char* argv[])
{
    startGamePieceLocation.horzLocation = startingLocation.at(0);
    startGamePieceLocation.vertLocation = startingLocation.at(1) - 64;

return 0;
}

but replacing startingLocation with whatever "A4" is actually called (Which I don't know because i'm new to command line)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main (int argc, char* argv[])
{
    //assume user called "program.exe A4". Then:
    // argc = 2;
    // argv[0] = "program.exe"
    // argv[1] = "A4"
    startGamePieceLocation.horzLocation = argv[1][0]; // 'A'
    startGamePieceLocation.vertLocation = argv[1][1]; // '4' (as char, not int)

   // NOTE: this program crashes when no command line argument is passed
   // NOTE: this program crashes when first command line arument is a single character

return 0;
}
Last edited on
OK thank you JockX. That's what I was looking for. Ill go try it now.
Try this little example on how those main() function parameters work and how best you can fit them into your game code:

1
2
3
4
5
6
7
8
#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

If you run this program with program_name.exe a4 1 2 3 4, the output would be

Have 6 arguments:
program_name.exe
a4
1
2
3
4
@JockX
I made it program_name.exe text_file_name.txt startingLocation so argv[2] will be "C:/Users/T_Dub/Desktop/data.txt". What I need to know now is do I put intData.open(argv[2]); or intData.open("argv[2]");

@Matri X
Ahh I think I get it know. Though you have int main(int argc, char** argv) {. When do you need one * and when do you need two?
Last edited on
char** arg is equal to char *arg[]
Now, about arguments, intData.open("argv[2]"); would be wrong, because it would make the open() function search your hdd for file named "argv[2]". You pass variables without quotes.
Topic archived. No new replies allowed.