fatal error with #include "stdafx.h"

Hi I wrote some code but it's giving me error and I have no idea how to fix it. Can you guys help me?


when trying to compile it gives off ||=== Build: Debug in menu (compiler: GNU GCC Compiler) ===|
C:\Users\happyboy9505\Desktop\menu\main.cpp|1|fatal error: stdafx.h: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

My code is following.


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void show()
{
string file_nm;
cout << "Enter file name : ";
cin >> file_nm;
ifstream inFile;
inFile.open (file_nm);

char inString[1000];

if (inFile.good())
{
while(!inFile.eof())
{
inFile.getline(inString,256);
cout << inString;
cout << "\n";
}
inFile.close();
cout << "\n";
}
else
{
cout<< "File does not exit...!";
cout<< "\n\n";
inFile.close();
}
}

void add()
{
string txt, file_nm;
cout << "Enter file name : ";
cin>> file_nm;
cout << "Enter text to be added : ";
cin>> txt;
std::ofstream outfile;
outfile.open(file_nm, std::ios_base::app);
outfile << txt << endl;
}

//
//===================================================================================
//

//int _tmain(int argc, _TCHAR* argv[])
int _tmain(int argc, char* argv[])
{
int iOption = 0;

while(1)
{
cout << "Main menu\n";
cout << " 1) Show a list\n";
cout << " 2) Add a name\n";
cout << " 3) exit\n";

cin >> iOption;

switch(iOption)
{
case 1: show(); continue;
case 2: add(); continue;
case 3: exit(1);
default: cout << "Invalid option...!\n\n";
}
}

return 0;
}




thanks!



"stdafx.h" is a Microsoft VC++ non-standard header, just delete that line for GNU GCC.

(The definition of your main function is also non-standard, in case your compiler complains about that next.)
Last edited on
Use int main() as you don't care about its arguments
so I deleted "stdafx.h" and its giving off another error.

C:\Users\happyboy9505\Desktop\menu\main.cpp|14|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'|

now how can i solve this error??

@ne555

where should I use int main() ?

thanks yall
Use C++11 (or better, C++14) if you want the i/ofstream construction or "open" function to be able to use std::strings (compiler option -std=c++11)
Otherwise, you must change
inFile.open (file_nm);
to
inFile.open (file_nm.c_str());
and all other places where you open a file.

where should I use int main() ?

replace int _tmain(int argc, char* argv[])
with
int main()
Last edited on
Topic archived. No new replies allowed.