Why does the name of a source file affect compilation? - c++

I came across something really weird when I wrote a little lotto program in C++ called "lotto.cpp". Everything was fine until I wrote the write-to-file for my program. When I compiled, it showed me the following error:
ld: can't open output file for writing: lotto, errno=21 for architecture x86_64
collect2: ld returned 1 exit status
By coincidence, I changed the name of my program to "1.cpp", and all of a sudden it compiled without problems. It also worked when I changed the name to "test.cpp".
I am really curious as to why this happened. Any Ideas?
This happened on a MacBook Pro.
If you want the code as well, just let me know!
I know some people asked for the code. Here it is:
#include <iostream>
#include <fstream>
using namespace std;
const int NED = 10;
const int VIKING = 6;
const int NORMAL = 7;
const int MAX = 10;
void quickSort(int arr[], int left, int right);
int checkDuplicates(int arr[], int length);
int main (int argc, const char *argv[])
{
int i, j, k, ans;
char ans2;
int lottoNumbers[MAX];
ofstream out("Lotto.txt", ios::out | ios::app);
srand((unsigned)time(NULL));
do
{
do
{
cout << "\n\nDo you want to play Viking Lotto (press 6), or normal Lotto (press 7): ";
cin >> ans;
}while(ans != VIKING && ans != normal);
(ans == VIKING) ? cout << "\nViking Lotto:\n" : cout << "\n\nnormal Lotto:\n";
(ans == VIKING) ? out << "\nViking Lotto:\n" : out << "\n\nnormal Lotto:\n";
for (i = 0; i < NED; i++) //10 rows
{
for (j = 0; j < ans; j++) //6 or 7 columns
{
(ans == VIKING) ? lottoNumbers[j] = (rand() % 48) + 1 : lottoNumbers[j] = (rand() % 34) + 1;
}
if(checkDuplicates(lottoNumbers, ans) != -1)
{
for(k = 0; k < ans; k++)
{
while(checkDuplicates(lottoNumbers, ans) == lottoNumbers[k])
{
(ans == VIKING) ? lottoNumbers[k] = (rand() % 48) + 1 : lottoNumbers[k] = (rand() % 34) + 1;
}
}
}
quickSort(lottoNumbers, 0, ans - 1);
cout << '\n';
for(j = 0; j < ans; j++)
{
cout << lottoNumbers[j] << '\t';
out << lottoNumbers[j] << '\t';
}
out << '\n';
}
cout << "\n\n";
cout <<"Another lottery ticket (Y/N) ";
cin >> ans2;
}while(ans2 == 'j' || ans2 == 'J');
cout << "\n\nLOTTO NUMBERS WAS WRITTEN TO FILE...\n\n";
return 0;
}
void quickSort(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int mid = arr[(left + right) / 2];
while (i <= j)
{
while (arr[i] < mid) i++;
while (arr[j] > mid) j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
if (left < j) quickSort(arr, left, j);
if (i < right) quickSort(arr, i, right);
}
int checkDuplicates(int arr[], int length)
{
for(int i = 0; i < length; i++)
{
for(int j = i + 1; j < length; j++)
{
if(arr[i] == arr[j]) return arr[j];
}
}
return -1;
}

Error number 21 (on MacOS X 10.7.2) is EISDIR: Is a directory.
The name lotto seems to be a directory, not a file.

This is a linker error that states that we cannot write to the 'lotto' file on your computer while compiling. My guess is that either your program is still running, or you accidentally created a directory called 'lotto'. It's possible that your write-to-file function is keeping the application running, or itself tried to create a lotto directory.

Yeah I ran into this problem by copying some of my visual studio code to my mac. It seems Visual Studio likes to create folders inside your project with your executable name which causes this!

FWIW I got this error when I was trying to write my output file into a directory that hadn't been created yet, i.e. bin/myprogram.
Once I created the bin directory everything was fine; I didn't have to re-name anything. GCC seems to create the directory if it doesn't exist, whereas clang doesn't (at least thats as near as I can tell).

Related

Cannot find bounds of current function while debugging

Hello guys can anyone tell me how do i fix this problem?When i debug in codeblocks and press the next line button it says "Cannot find bounds of current function".How do i fix this?here in the while loop i wanted to test out the debugger.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define vint vector<int>
#define all(v) v.begin(), v.end()
int main()
{
int n, i, in, x, sum1 = 0, sum2, t = 0;
cin >> n;
vint a;
a.pb(0);
for (i = 0; i < n; i++) {
cin >> in;
a.pb(in);
sum1 = sum1 + a[i];
}
in = 1;
n = n + 1;
while (in != 5) { // i want to debug from here.
sum2 = sum1 + in;
for (i = 0; i < n + 1; i++) {
if (i == n + 1) {
i = -1;
continue;
}
sum2--;
if (sum2 == 0) {
break;
}
}
if (i != 0) {
t++;
}
in++;
}
cout << t << endl;
return 0;
}
That usually means that there is no debug info found. Do you try to debug a release project maybe? For code/symbols to be present during debugging, you need to use a debug build.

why my code is not giving the right fibonecci number in nth value

#include <iostream>
using namespace std;
int fibo(int);
int main()
{
int num;
cout << "Enter the nth position you want to find the fibonecci number\t ";
cin >> num;
cout << "The " << num << "th fibonecci number is " << fibo(num);
}
int fibo(int n)
{
int j = 0;
int arr[25];
for (int i = 0; i <= n; i++)
{
arr[i] = 0;
}
arr[1] = 0;
arr[2] = 1;
if (n == 1)
{
return arr[1];
}
else if (n == 2)
{
return arr[2];
}
else
{
for (j = 3; j <= n; j++)
{
arr[j] = arr[j - 1] + arr[j - 2];
cout << arr[j];
}
return arr[j];
}
}
when i am compiling the code its giving garbage value but i dont understand why please if you can try to help , here i am using fibonecci using memorizaion .
here i am taking an arrayarr[] for storing the value as a memory.
The garbage value that you are getting is because of the following reason:
You initialize elements a[0] to a[n] with 0, so elements from a[n+1] and afterwards are uninitialized (and hence might contain garbage value).
When you exit the for loop, the value of j is equal to n+1 and not n. So your function returns the garbage value of a[n+1].
You can easily fix this bug by just returning arr[--j] instead of a[j] after the for loop.
Also, you can now comment off the cout inside the for loop, otherwise it will further spoil your output.

asterisk rectangle using while loop

I have to make an i by j rectangle using while loops....
so far this is as far I got.
#include <iostream>
using namespace std;
void stars(int, int);
int main()
{
int i, j;
cin >> i >> j;
stars(i, j);
return 0;
}
void stars(int i, int j)
{
while (j >= 0)
{
while (i >= 1)
{
cout << "*";
i = i - 1;
}
j = j - 1;
}
}
it shoots out one row of 'i' asterisks.
I (j-1) more rows....
There are 2 things wrong with your stars code:
You need to somehow go to the new line after your row of asterisks is complete
If you decrement i and never restore it to its original value after the 1st row no more asterisks will be printed
You could try something like this:
void stars(int i, int j)
{
while (j-- > 0)
{
int k = i;
while (k-- > 0)
cout << "*";
cout << endl;
}
}

Non-working while loop & Messed up Binary Search

For this program I have to fill an array with 20 random numbers (1-100), sort the array (descending) and then search for a random key value and output the position of that value if it is in the array. I am having 2 problems. First the while loop I have to exit the program is not working and I can not figure out why. Second my binary search is not returning a position value and I don't know why. This code will compile.
#include <iostream>
#include <ctime>
using namespace std;
int printArray(int *arr, int arraySize);
int fillArrayWithRandomNumbers(int *arr, int arraySize);
int bubbleSortDesc(int *arr, int arraySize);
int binarySearch(int *arr, int arraySize, int key);
int main()
{
int const arraySize = 20;
int arr[arraySize];
cout << "CMPSC 201-Extra Credit\n" << "This program fills an array, and then searches for a random key value." << endl <<endl;
char stopTheProgram = 'n';
do {
int key, result;
cout << "Unordered array:" << endl;
fillArrayWithRandomNumbers(arr, arraySize);
printArray(arr, arraySize);
cout << endl << "Array after a bubble sort : " <<endl;
bubbleSortDesc(arr, arraySize);
printArray(arr, arraySize);
key = rand()%100;
cout << endl <<"Searching for " << key << endl;
result = binarySearch(arr, key, arraySize);
if (result == -1)
{
cout << "Key " << key << " not found in the array" << endl;
}
else
{
cout << "Key " << key << " found at position " << result << endl;
}
cout << "Stop the program? (y/n) ";
cin >> stopTheProgram;
cout << endl;
} while (!(stopTheProgram == 'Y' || stopTheProgram == 'y'));
return 0;
}
int fillArrayWithRandomNumbers(int *arr, int arraySize)
{
srand((unsigned)time(NULL));
for (int i = 0; i<arraySize; i++)
{
arr[i] = (rand() % 100) + 1;
}
return *arr;
}
int printArray(int *arr, int arraySize)
{
for (int i = 0; i<arraySize; i++)
{
cout << arr[i] << " ";
}
return *arr;
}
int bubbleSortDesc(int *arr, int arraySize){
int i, j;
int temp = 0;
for (i = 0; i < arraySize; i++)
{
for (j = 0; j < arraySize - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return *arr;
}
int binarySearch(int arr[], int key, int arraySize)
{
int i, j;
int temp = 0;
for (i = 0; i < arraySize; i++)
{
for (j = 0; j < arraySize - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
int position, lowerBound = 0;
position = (lowerBound + arraySize) / 2;
while ((arr[position] != key) && (lowerBound <= arraySize))
{
if (arr[position] > key)
{
arraySize = position - 1;
}
else
{
lowerBound = position + 1;
}
position = (lowerBound + arraySize) / 2;
}
if (lowerBound <= arraySize)
{
position = 19 - position;
return position;
}
else {
return -1;
}
}
Solved: I now have fixed my problems with my binary search, and exiting my while loop. I am going to leave this here just in case anyone has my prof after me. So just to recap, this program fills an array with 20 random numbers (ranging 1-100), sorts the array in descending order and then creates a random key value (between 1-100). It then bubble sorts the array so it is in ascending order, uses a binary search to find the key value in the array and finally output the position of that key value if it is in the array.
I can see two issues with your code. First, you are shadowing the stopTheProgram variable. You define it once just before the do/while loop and initialize it to 'n'; Then once inside the do/while you define another stopTheProgram. This is a problem because of scoping. Inside the do/while loop the input from the user is assigned to the local stopTheProgram (defined in the do/while) but that ceases to exits outside the loop. So the while loop expression is always evaluated using the globally scoped stopTheProgram, which is set to 'n'. So remove the second definition. Second is an issue with the expression that controls the while loop. It always evaluates true. Draw a truth table if you can't visualize it. If stopTheProgram = 'Y' then the stopTheProgram != "Y" || stopTheProgram != 'y' is 0 || 1 which is always true. If stopTheProgram = 'y' then stopTheProgram != "Y" || stopTheProgram != 'y' is 1 || 0 which is always true. This works: while(!(stopTheProgram == 'Y' || stopTheProgram == 'y'))

How do I make my program stop if it's taking too long to finish?

I'm trying to make a insertion/merge sort program, does both, and it needs to accept inputs up to 10 million series long arrays. For merge sort thats fine it takes a few seconds to sort it, but it should take over 6 hours for insertion according to my friends. I'd like to put a timer in to the program to make it just stop after 30 minutes of working and not getting done with the sorting somehow but I'm not sure how, or where I would put it.
Heres my code for my main method and insertion sort, since its the only one that needs a timer. Anyone have any idea what to do or where to start?
void insertionSort(int arr[], int length) {
int i, j, tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}
int main()
{
srand (time(0));
long x;
cout << "how long will the array be?\n" <<
"10, 100, 1000, 10000, 100000, 1000000, or 10000000?" << endl;
cin >> x;
switch(x){
case 10:
x = 10;
break;
case 100:
x = 100;
break;
case 1000:
x = 1000;
break;
case 10000:
x = 10000;
break;
case 100000:
x = 100000;
break;
case 1000000:
x = 1000000;
break;
case 10000000:
x = 10000000;
break;
default:
cout << "Error, incorrect number entered, please try again!" << endl;
}
static int ar[10000000];
for(int i = 0; i < x; i++){
ar[i] = rand() % 100000001;
}
int c= 0;
cout << "which sorting method would you like to use?\n" <<
"Insertion(1), merge(2), or quick(3)? \nPlease enter the number beside the one you want to use" << endl;
cin >> c;
if(c == 1){
insertionSort(ar, x);
}
else if(c==2){
for (int i = 1; i < x; i *= 2) {
for (int j = 0; j < x- i; j += 2*i) {
int iEnd2 = (2*i < x - j) ? 2*i : x - j;
Merge(&(ar[j]), i, iEnd2);
}
}
} else if(c==3){
quickSort(ar,0,x-1);
} else{
cout << "You did not enter a correct number, please try again" << endl;
}
for(int i = 0; i < x; i++){
cout << ar[i] << endl;
}
return 0;
}
Depending on your OS system you can set up special environments for your program to run in such that if it takes too long (or other conditions) the program will terminate. Alternatively you can spawn a thread that just counts down as long as you want. If it finishes and the main thread is still running, you can kill the program from there.
You could do it with the ctime module easily I guess:
#include <ctime>
#define HALF_HOUR 60 * 30
using namespace std;
...
clock_t start = clock();
clock_t now;
int i, j, tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
now = clock();
if ((now - start) / CLOCKS_PER_SEC) >= HALF_HOUR)
{
cout << "Insert sort timed out." << endl;
return;
}
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
The easiest way is to implement a timed callback function. That function will be called after specified time interval and you can exit the program inside that function.
More information about how to implement it, can be found in the following link
c++ Implementing Timed Callback function