relative paths not working

Jan 9, 2016 at 11:27am
Hello Everyone! Thanks for the help in advance! I am trying to write a primary quiz program. My problem is on lines 35 - 40. I do not get an error message its just that the relative paths are not directing to the right files. I made sure the paths are relative to the current working directory, so that should be fine. So do you have any idea what is wrong with my relative paths?
(Oh and please dont tell me how stupid I am for using goto and system(), because I know :D)

I am using VS 2013

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
194
#include <iostream>
#include <Windows.h> 
#include <string> 
#include <fstream> 
#include <cstdlib> 
#include <time.h> 
#include <iomanip>

using namespace std; 

void cleanUP();

int main(){ 
	system("title Primary School Quiz by Nanyo"); 

	int firstNumber, secondNumber, result, answer, score = 0;
	char sign;
	string name, tutor; 

	cout << "Please enter your full name: ";
	getline(cin, name); 
	cout << "Thanks!"; 
	cleanUP(); 
TutorReg: // Called at line 30
	cout << "Please enter your tutor group: ";
	cin >> tutor; 
	if (tutor != "A" && tutor != "a" && tutor != "B" && tutor != "b" && tutor != "C" && tutor != "c"){
		cout << "There is no tutor group \"" << tutor << "\"";  
		cleanUP();
		goto TutorReg; // "TutorReg" is on line 24
	}
	cout << "Thanks!"; 
	cleanUP(); 

	ofstream fileA("../Scores/A.txt", ofstream::out | ofstream::app); //Problem here
	ofstream fileB("../Scores/B.txt", ofstream::out | ofstream::app); //Problem here
	ofstream fileC("../Scores/C.txt", ofstream::out | ofstream::app); //Problem here 
	ifstream fileAr("../Scores/A.txt"); //Problem here
	ifstream fileBr("../Scores/B.txt");  //Problem here
	ifstream fileCr("../Scores/C.txt"); //Problem here


	for (int i = 1; i <= 10; ++i){
		srand(time(NULL)); 
		firstNumber = rand() % 10; 
		secondNumber = rand() % 10; 

		cout << "******QUIZ******\n" << "Q" << i << endl; 

		int fwd;

		fwd = rand() % 3; 
		if (fwd == 0){
			sign = '+';
			result = firstNumber + secondNumber;
			cout << firstNumber << sign << secondNumber << "="; 
			cin.width(1); 
			cin >> answer; 
			cin.ignore(99, '\n');
			while (cin.fail()){
				cout << "Not an integer. Try again:\n"; 
				cin.clear(); 
				cin.ignore(99, '\n');
				cout << firstNumber << sign << secondNumber << "="; 
				cin >> answer; 
			}
			if (answer == result){ 
				cout << "Well done!";
				++score;
			}
			else{ 
				cout << "Wrong!"; 
			}
			cleanUP(); 
		}
		if (fwd == 1){
			sign = '-';
			result = firstNumber - secondNumber;
			cout << firstNumber << sign << secondNumber << "="; 
			cin.width(1); 
			cin >> answer; 
			cin.ignore(99, '\n');
			while (cin.fail()){ 
				cout << "Not an integer. Try again:\n"; 
				cin.clear();
				cin.ignore(99, '\n'); 
				cout << firstNumber << sign << secondNumber << "="; 
				cin >> answer; 
			}
			if (answer == result){ 
				cout << "Well done!"; 
				++score; 
			}
			else{ 
				cout << "Wrong!"; 
			}
			cleanUP(); 
		}
		if (fwd == 2){
			sign = '*';
			result = firstNumber * secondNumber;
			cout << firstNumber << sign << secondNumber << "="; 
			cin.width(1);
			cin >> answer; 
			cin.ignore(99, '\n');
			while (cin.fail()){ 
				cout << "Not an integer. Try again:\n"; 
				cin.clear();
				cin.ignore(99, '\n');
				cout << firstNumber << sign << secondNumber << "="; 
				cin >> answer; 
			}
			if (answer == result){ 
				cout << "Well done!"; 
				++score; 
			}
			else{ 
				cout << "Wrong!"; 
			}
			cleanUP();  
		}

	}

	cout << "Your score is: " << score << "/10"; 

	if (tutor == "A" || tutor == "a"){
		fileA << name << " " << score << "\n"; 
	}
	else if (tutor == "B" || tutor == "b"){
		fileB << name << " " << score << "\n"; 
	}
	else if (tutor == "C" || tutor == "c"){
		fileC << name << " " << score << "\n"; 
	}

	Sleep(2500);
	system("CLS");

	cout << "Do you want to see your tutor group scores? Y/N\n"; 

	string choice;
	//choice = _getch(); - not used
	cin >> choice; 

	if (choice == "Y" || choice == "y"){

		system("CLS"); 

		int j = 0, k = 0, temp;
		string nameInFile[100]; 
		string scoreInFile[100];
		string tempstr;
		int allScores[100]; 

		cout << "******Score Board******\n"; 
		cout << "You got " << score << "/10\n------------------\n"; 

		while (getline(fileAr, nameInFile[j], ' ') && getline(fileAr, scoreInFile[j])){
			allScores[j] = stoi(scoreInFile[j]); 
			++j;
		}
		
		for (int l = 0; l <= j-1; l++)
		{
			for (int n = l + 1; n <= j-1; n++) 
			{
				if (allScores[l] < allScores[n])
				{
					
					temp = allScores[n];
					allScores[n] = allScores[l];
					allScores[l] = temp;
					
					tempstr = nameInFile[n];
					nameInFile[n] = nameInFile[l];
					nameInFile[l] = tempstr;
				}
			}
			cout << nameInFile[l] << " " << allScores[l] << "/10\n"; 
		}
	
		cout << "\npress any key to exit..."; 
	}
	
	system("PAUSE >NULL"); 
	return 0; 
}

void cleanUP(){ 
	Sleep(1500); 
	system("CLS"); 
}
Last edited on Jan 9, 2016 at 11:31am
Jan 9, 2016 at 11:37am
How sure are you that the binary executable is being run in the directory you think it is? How do you know?
Jan 9, 2016 at 11:41am
Well thats where I had my files at the beginning(not in additional folder) and everything was working perfectly:

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3

However when I moved my files(plain .txt files) here:

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3\Scores

the magic stops working :D
Jan 9, 2016 at 11:43am
Point at which of those directories your executable is in.
Jan 9, 2016 at 11:44am
The first one:

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3
Jan 9, 2016 at 11:45am
Are you sure that the files are in the right folder? If you run the program inside VS "../" would refer to a folder outside the solution folder.

If you run the .exe from the Debug or Release folder "../" would refer to the Bin folder.

The see the current directory you can use this code:
1
2
3
4
5
  # include <windows.h>

  char buffer[MAX_PATH];
  ::GetCurrentDirectory(MAX_PATH, buffer);
  cout << "Current directory: " << buffer << endl;
Jan 9, 2016 at 11:47am
If your executable is in directory

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3\

then your relative path is wrong.

".." means "go back one", so "../Scores/A.txt" from your executable's directory is

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\Scores\A.txt

but you say the files are not in that directory, but in a different place:
C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3\Scores\

Try "./Scores/A.txt", or move your data files to
C:\Users\Nanyo\Desktop\A453 Quiz Task 3\Scores\
Last edited on Jan 9, 2016 at 11:51am
Jan 9, 2016 at 11:48am
oh my god the working directory is

C:\Users\Nanyo\Desktop\A453 Quiz Task 3

not

C:\Users\Nanyo\Desktop\A453 Quiz Task 3\A453 Quiz Task 3

Thank you so much!!!!!!!!!
Topic archived. No new replies allowed.