Visual Studio Code: Error include external header in C - c

New to programming, actually following an open classroom tutorial on C and I'm finding myself stuck on that including external header part. I use an IDE, I'm on Visual Studio Code, I could switch to an IDE but it should be possible and not to difficult to do it in a simple text editor so I want to understand how.
The error code when running the program:
main.c:6:10: fatal error: 'level.h' file not found
#include "level.h"
^~~~~~~~~
1 error generated.
Here's my main.c code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include "level.h"
#include "menu.h"
int level();
int menu();
int main()
{
int nombreMystere, guess, round = 10;
char answer;
bool exit = true;
while (exit)
{
menu();
{
while(guess != nombreMystere)
{
printf("%d" , nombreMystere);
printf("\nIl vous restes %d round.\n", round );
printf("Trouver le nombre magique: " );
scanf ("%d" , &guess );
if(guess < nombreMystere) printf("Trop bas\n");
else if(guess > nombreMystere) printf("Trop Haut\n");
round --;
if (round == 0)
{
printf("You Lose.. Dumbass");
break;
}
if (guess == nombreMystere) printf("\nGood job !\n");
}
printf("Play again?\n (yes/no): ");
scanf("%s", &answer);
if (answer == 110 || answer == 78) {exit=0;}
}
}
return 0;
}
Here's level.c:
static int selec;
int level(int x)
{
int max, difficulty = selec;
if (difficulty==1){max = 100 ;}
else if (difficulty==2){max = 1000 ;}
else if (difficulty==3){max = 10000 ;}
return max;
}
level.h:
int level(int x);
menu.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "menu.h"
int level();
int menu()
{
srand(time(NULL));
int max, nombreMystere, selec;
const int MIN = 1;
while (selec < 1 || selec > 3)
{
printf("\n\nSelect Difficulty level:\n");
printf("1: Level 1\n");
printf("2: Level 2\n");
printf("3: Level 3\n");
scanf("%d", &selec);
level(selec);
printf("\n\n%d\n\n", max);
//if (difficulty==1){max = 100 ;}
//else if (difficulty==2){max = 1000 ;}
//else if (difficulty==3){max = 10000 ;}
//else {printf("Wrong selection, please try again.");}
nombreMystere = (rand() % (max - MIN + 1)) + MIN;
}
return nombreMystere;
}
menu.h:
int menu();
I've been searching the whole day on the internet a way to fix this issue but I don't understand half of what I'm seeing and every topic was talking only about C++. I've seen somewhere that it could come from the tasks.json file in VSCode but it was on a C++ topic.
I'm using macOS.
Thanks for your time.
EDIT: Both .h and .c files are in separate folders and both folders are in the same directory.

The level.h file needs to be in the SAME folder as main.c.
Also the menu.h file should be in the same folder. So make sure you have all the files in the same folder.
Additionally these two lines shouldn't be in your main.c:
int level();
int menu();
Since this is essentially what including the headers does for you. On that note I also can't see you calling the function level() in your main.c file, thus you do not even need to include the level.h file at all. However you do call the function menu() so you do need to #include "menu.h" like you are doing.
Also in the menu.c file you don't need this line #include "menu.h", you should change it to #include "level.h" since you are calling the level() function in your code and then also remove the line after it, that is; the line int level();, since that's essentially what including the level header will do for you.
The level.c file looks fine to me.
If you're still getting an error with all the files in the same folder, make sure you are compiling it correctly.

In include "x" the value of x represents the path to your header file (relative using the directories . and .. in your current directory or absolute paths, the latter is a BAD PRACTICE don't do it, but you can, side note: the ~ is not supported) if all your files are in the same directory you shouldn't have such an error, you don't have to use the relative path it assumes the current directory by default, but if they aren't consider including them the right way. see sveinnth's answer for more notes about your coding style.
one more thing, you should include your compilation options in your question.

Related

Can't create ppm File from an executable (C)

I am trying to create a ppm file from inside a C program, but somehow it doesn't work.
It works fine when I am inside the IDE and run the program there. The program is executed and the file created inside the file's folder.
But once I build it and open the executable with a double click, the program runs in the terminal but does not create the file.
I am working on a mac and this is the relevant code, thanks in advance you all!
// from generalSettings.h
#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];
//from the main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"
int main()
{
triangle firstParentTriangle;
// these two functions "draw" sierpinsky triangles in the pictureArray
drawFirstParentTriangle(&firstParentTriangle);
drawChildTriangles(firstParentTriangle, numberOfRecursions);
create_ppm();
return 0;
}
void create_ppm()
{
unsigned char color_black[] = "000 000 000\n";
unsigned char color_white[] = "255 255 255\n";
FILE *p_file = fopen(MYFILENAME, "w");
if (NULL != p_file)
{
fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (1 == pictureArray[i][j])
fprintf(p_file, color_black);
else
fprintf(p_file, color_white);
fclose(p_file);
}
}
Here, I have replaced the foreground and background variables with local ones.
Generally, the program inserts 1 to the pictureArray[][] on certain elements and leaves others with a 0.
For my problem, this should be the relevant code for a reproducable example.
EDIT: Problem solver. File was created in the user folder due to missing path.

"Invalid Operands to Binary" error when trying to calculate square of elements in array

this code is supposed to read a text file that contains integers, which then finds the squares of these numbers after putting them into an array.
After this its supposed to print the results onto a new text file "result.txt", but I keep getting the error "invalid operands to binary*(have 'int*' and 'int*')" on this line: square[x] = square[x] * square[x];
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int square[100][2];
int x;
int i;
FILE* input=fopen("in.csv","r");
FILE * f=fopen("result.txt","wb");
system("cls");
for(x=0;x<100;x++)
{
fscanf(input,"%d",&square[x][0]);
}
fclose(input);
for(x=0;x<100;x++)
{
square[x] = square[x] * square[x]; //this line produces the error
}
for(i=0;i<100;i++)
{
fprintf(f,"%d -> %d || ",square[i][0],square[i][1]);
}
fclose(f);
getchar();
}
I am using Eclipse IDE and MinGW-w64
I have tried finding solutions online but am stuck, any help or replies would be appreciated, thanks!
square[x] yields an array with two elements. You should reference the [0] and [1] elements like you do in the rest of your code.
square[x][1] = square[x][0] * square[x][0];

c does not follow the program operation procedure

summary : system("clear"); isn't working well.
I'm using gcc, ubuntu 18.04 LTS version for c programming.
what I intended was "read each words and print from two text files. After finish read file, delay 3 seconds and erase terminal"
so I was make two text files, and using system("clear"); to erase terminal.
here is whole code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
I'm very sorry, files and variables names are changed because of policy. Also, file contents also can not upload.
I can't find which part makes break Procedure-oriented programming. I think that was compiler error, because using one file read and system(clear); works well.
I also make two point variables, such as 'FILE *f1; FILE *f2; f1=fopen(file1); f2=fopen(file2)...`, but same result occur.
Is it compiler error? If it is, what should I do for fix these problem? Thanks.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
fflush(stdout);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
fflush(stdout);// The answer.
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
Hint for
That's probably just buffering. Do fflush(stdout); before you sleep. – melpomene
Thanks.
You can try this solution for delay.
#include <time.h>
#include <stdio.h>
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(&current);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(3);
printf("...oh man, waiting for so long...\n");
return 0;
}
Following solution is pretty quite the same of previous one but with a clear terminal solution.
#include <time.h>
#include <stdio.h>
#ifdef _WIN32
#define CLEAR_SCREEN system ("cls");
#else
#define CLEAR_SCREEN puts("\x1b[H\x1b[2J");
#endif
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(&current);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(2); //seconds
printf("...oh man, waiting for so long...\n");
delay(1);
CLEAR_SCREEN
return 0;
}

error C2447: '{' : missing function header -- Can't resolve this error, what's wrong?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "bcio2.h"
int error, x;
char totalimpulse[80], averageimpulse[80];
void validate_number();
int main(void)
{
clrscr();
do{
printf("\nTotal Impulse delivered: ");
gets(totalimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
{ //error C2447
clrscr();
do{
printf("\nAverage Impulse delivered: ");
gets(averageimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
The brackets seen to match and there doesn't seem to be any unnecessary semicolons. I'm assuming this is the correct way to display input/validation. It works fine when run with just the do…while(); loop for totalimpulse but when I copy/paste the exact same between another pair of { } I get just that C2447 error.
The code that starts where the error is is not inside main, or any other function for that matter. If you remove the braces on the error line and the one preceding it, then your second loop will also beside of main. If you want that section to be a different function, you have to include the header for that function. What you put at the top for validate_number is just a promise that you will define that function somewhere (although if you mean for that section at the bottom to be validate_number, I'm pretty sure you don't want it to be recursive).
Right now you just have a block of code, outside any function.
I'm assuming from the rest of your code, that this block of code is supposed to be the definition of void validate_number();, like this:
void validate_number()
{
clrscr();
do{
// ...
return 0;
}
Do note that a void function cannot return a value, so your return 0 should be removed.

RUN FAILED exit value -1 073 741 819 - what's wrong with my code?

I'm trying to make a stopwatch program, which will write czas to file.txt. I started learning C today, so please be lenient for me, if it's a stupid question, but the compiler doesn't throw out any errors, and NetBeans also doesn't display any exclamation marks. There is my code:
#include <windows.h>
#define sleep(x) Sleep(1000 * x)
#include <stdio.h>
#include <stdlib.h>
int a = 0;
int czas = 0;
int main (void)
{
FILE *file;
while (a < 30) { /*repeats only 30 times*/
a = a + 1; /*increases the counter for while loop*/
file = fopen("file.txt","w"); /*opens file.txt for writing*/
fprintf(file,"%s", czas); /*writes czas to file.txt*/
fclose(file); /*closes file.txt to save*/
czas = czas + 1; /*increases czas for writing to file*/
}
return 0;
}
Could somebody help me?
%s needs a char* as referring parameter.
You want to write out an int, which expects %d.
For details on this please see man fprintf.
In case you like to have every new value of czas on a new line you can specify this in the fprintf() statement like so:
fprintf(file,"%d\n", czas);

Resources