My program won't run in Dev C++.

This program runs very well in Visual C++ and I also want to run this program in Dev C++. How can I run this program using DEV C++?

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
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int main()
{
	string FileName;
	char UserInput[512];
	string ReadBuffer;
	
	cout<<"Please enter the name of the file to write to.\n";
	cout<<"File Name";
	cin>>FileName;
	cin.get();
	cout<<"Please enter a line of text to save to the file.\n";
	cout<<"Text: ";
	cin.getline(UserInput, 512);
	
	ofstream WriteToFile;
	WriteToFile.open(FileName);
	WriteToFile<<UserInput;
	WriteToFile.close();
	
	system("CLS");
	cout<<"Opening the file: "<< FileName <<", the contents of the file are:\n\n";
	
	ifstream ReadFromFile(FileName);
	if(ReadFromFile.is_open())
	{
		getline(ReadFromFile, ReadBuffer);
		cout<< ReadBuffer <<endl;
	}
	else
		cout<<"Failed to open the file!\n";
		
	cin.get();
	return 0;
}
ERRORS
D:\sample.cpp	In function 'int main()':
21	27	D:\sample.cpp	[Error] no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'
21	27	D:\sample.cpp	[Note] candidate is:
2	0	D:\sample.cpp	In file included from D:\Cs3 act10\sample.cpp
702	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
702	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'
25	14	D:\sample.cpp	[Error] 'system' was not declared in this scope
28	32	D:\sample.cpp	[Error] no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'
28	32	D:\sample.cpp	[Note] candidates are:
2	0	D:\sample.cpp	In file included from D:\Cs3 act10\sample.cpp
460	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
460	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'
446	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]
446	7	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] candidate expects 0 arguments, 1 provided
420	11	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)
420	11	c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\fstream	[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const std::basic_ifstream<char>&'

Last edited on
You have to enable C++11.
C++11? How can I enable it sir?
Last edited on
I also want it to run in Cygwin.
You have to pass the flag -std=c++11 to the compiler. I'm not sure exactly how to do that in Dev-C++. There might be a checkbox somewhere, or maybe you can add the compiler flag manually somehow.
Another option, since the compiler you're using may not support C++11, would be to use the c_str() function to convert the string to a C-string.
 
	ifstream ReadFromFile(FileName.c_str());


Topic archived. No new replies allowed.