9

In C++ with Visual studio 2017,

I copied some header files into my project folder, then added them under the "solution explorer" in c++. Now when I write

#include "name.h"

it prints an error under include, and says "cannot open source file".

Why, and what can I potentially do to fix it?

I only just downloaded VS and am learning c++ for the first time.

Jaood
  • 295
  • 1
  • 2
  • 10

5 Answers5

14

If you're using Visual studio, right click on the project and then on Properties, Under Configuration Properties click on C\C++ and then add the directory to your header files under the Additional Include Directories section.

xflowXen
  • 326
  • 3
  • 8
2

There is more information here on how to deal with this problem: Where does Visual Studio look for C++ header files?

For me, I followed xflowXen's answer and then at "Include Directories" typed in the specific pathname where my header file was located followed by a semicolon, something like: C:\Users\name\source\repos\p2-A\p2-A; then applied the changes and the issue went away.

jskattt797
  • 191
  • 1
  • 7
1

For anyone still scratching their heads, you're not "supposed to" #include your own headerfiles with triangle quotes (<>), You're supposed to use "Quotation marks". It is a common mistake.

0

Visual Studio (or rather the compiler) needs to know where to look for the included file. Check out your include path in your VS project.

doron
  • 26,460
  • 11
  • 62
  • 99
-15
#include<iostream.h>
#include<conio.h> 
#include<stdlib.h> 
using namespace std; 

int divide(int num, int den) 
{
   if(den==0) 
   { 
      return -1; 
   } 
   if((num%den)==0) 
   { 
      return 1; 
   } 
   else 
   {    
      return 0; 
   } 
} 

int divide(int a) 
{ 
   int j = a/2, flag = 1, i; 

   for(i=2; (i<=j) && (flag); i++) 
   { 
      if(a%i == 0) 
      { 
         flag = 0; 
      } 
   } 
   return flag; 
} 

void main() 
{ 
   clrscr(); 
   int choice, res, a, b; 

   do 
   { 
      cout<<"1.Check for divisibility\n"; 
      cout<<"2.Check for Prime\n"; 
      cout<<"3.Exit\n"; 
      cout<<"Enter your choice(1-3): "; 
      cin>>choice; cout<<"\n"; 
      switch(choice) 
      { 
         case 1: 
            cout<<"Enter numerator and denominator: "; 
            cin>>a>>b; 
            res = divide(a, b); 
            if(res == -1) 
            { 
               cout<<"Divide by zero error..!!\n"; break; 
            } 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 2: 
            cout<<"Enter the number: "; 
            cin>>a; 
            res = 0; 
            res = divide(a); 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 3: 
            cout<<"Exiting...press any key..."; 
            getch(); 
            exit(1); 
         default:
            cout<<"Wrong choice..!!"; 
      } 
      cout<<"\n"; 
   }while(choice>0 && choice<=3); 
   getch(); 
}
MLeblanc
  • 1,733
  • 11
  • 20