1

I am trying to make a program that will read from one file and output to another file a different value than read in, based on a input integer. I am getting errors on opening files. No matching call function

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;

int main()
{

char option, m, c;
int amount, n, k;
string fname, dname, ename;

ofstream nstream;
ifstream istream;


cout << "Do you want to encrypt of decrypt? Enter D or E.";
cin >> option;
cout << endl;

while (option != 'D' && option != 'E')
{
    cout << "That is not acceptable" << endl;
    cout << "Do you want to encrypt of decrypt? Enter D or E.";
    cin >> option;
    cout << endl;
}


if (option == 'E')
{
    cout << "Enter the name of the file that you want to encrypt: ";
    cin >> fname;
    istream.open(fname);
    cout<<endl;
    cout << "Enter the name of the file that you want to output: ";
    cin >> ename;
    nstream.open(ename);
    cout<<endl;
}

if (option == 'D')
{
    cout << "Enter the name of the file that you want to decrypt: ";
    cin >> dname;
    istream.open(dname);
    cout<<endl;
    cout << "Enter the name of the file that you want to output: ";
    cin >> ename;
    nstream.open(ename);
    cout<<endl;

}
cout << "Enter the number of digits that you want to de/encrypt(integers): ";
cin >> k;

istream >> m;
while (m >= 0);
{
    if (m > 65 && m < 90);
    {
        c = m + k;
        while (c > 90)
        {
            c = c - 26;
        }

    }
    if (m > 97 && m < 122);
    {(c = m + k);
    while (c > 122)
    {
        c = c - 26;
    }
    }

     if(m >= 0)
    {
        c = m + k;
    }

}

nstream << c;
return 0;

}

error messages

In function ‘int main()’:
homework4.cpp:39:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
  istream.open(fname);
                    ^
homework4.cpp:39:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
      ^
/usr/include/c++/4.9/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:43:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
  nstream.open(ename);
                    ^
homework4.cpp:43:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: 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]
      open(const char* __s,
      ^
/usr/include/c++/4.9/fstream:716:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:51:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
  istream.open(dname);
                    ^
homework4.cpp:51:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
      ^
/usr/include/c++/4.9/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:55:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
  nstream.open(ename);
                    ^
homework4.cpp:55:21: note: candidate is:

In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: 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]
      open(const char* __s,
^ /usr/include/c++/4.9/fstream:716:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
Sal Rahman
  • 4,607
  • 2
  • 30
  • 43
Xorn
  • 13
  • 1
  • 5
  • Which line exactly in this code is causing the error? – BusyProgrammer Mar 29 '17 at 23:12
  • Where are you getting that error? I can compile your code just fine (except for needing to change `“`, and `”` to `"`). Also, your code is littered with warnings. For example, multiple instances of: _warning C4390: ';' : empty controlled statement found; is this the intent?_. – Algirdas Preidžius Mar 29 '17 at 23:13
  • I am getting this error for each istream.open and nstream.open code – Xorn Mar 29 '17 at 23:17
  • @Xorn, as I already mentioned. All of the errors were related to needing to change `“`, and `”` to `"` (or, on the second though - it would be more logical to remove them altogether.. Code still compiles just fine). There are no _no matching function_ errors in the code, that you presented. Maybe your compiler is misinterpreting something about those weird quotation marks. – Algirdas Preidžius Mar 29 '17 at 23:19
  • Post the __full and exact__ error message that the compiler produced. – BusyProgrammer Mar 29 '17 at 23:19
  • In function ‘int main()’: homework4.cpp:39:21: error: no matching function for call to ‘std::basic_ifstream::open(std::string&)’ istream.open(fname); ^ – Xorn Mar 29 '17 at 23:23
  • Okay this is everything with the quotations edited out. – Xorn Mar 29 '17 at 23:26
  • 1
    @Xorn Are you compiling with C++11 enabled? Quick glance at the documentation, would tell you, that [`std::ifstream::open`](http://en.cppreference.com/w/cpp/io/basic_ifstream/open) accepts `std::string` argument only since C++11. – Algirdas Preidžius Mar 29 '17 at 23:29
  • @Algirdas Preidzius I am compiling using G++. Sorry for a lack of knowledge – Xorn Mar 29 '17 at 23:36
  • @Xorn You still didn't answer my question, are you compiling with C++11 enabled, using your g++? – Algirdas Preidžius Mar 29 '17 at 23:41
  • I'm honestly not sure, we have to use the school's ubuntu machines and they don't specify the version of C++ – Xorn Mar 29 '17 at 23:49
  • @Xorn Then [this](http://stackoverflow.com/questions/10363646/compiling-c11-with-g) SO question (or the top answer, to be precise) would explain that to you. – Algirdas Preidžius Mar 29 '17 at 23:53

1 Answers1

1

In your code, fname, dname, and ename are variables of type std::string, by default. However, the fstream::open requires a const char * value passed to it for a filename.

Here is how the function should be used:

void open (const char* filename,
            ios_base::openmode mode = ios_base::in | ios_base::out); 

Obtained from C++ Reference: std::fstream::open

To do this, add the .c_str() function to the end of the filename variable:

  • istream.open(fname.c_str());
  • nstream.open(ename.c_str());
  • istream.open(dname.c_str());

This will remove that error.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • Wait what? Explanation is, precisely, backwards, while code snippet is correct? `fname`, `dname`, `ename` is of type `std::string` (not `const char*`), and pre-C++11 `fstream::open` accepts only `const char*`. – Algirdas Preidžius Mar 29 '17 at 23:43
  • Could I have declared these as a Char String? for Instance char ename[31] – Xorn Mar 29 '17 at 23:44
  • @AlgirdasPreidžius oops! Sorry, I completely messed that up. Thanks for warning me about that, at least before the downvotesstarted pouring in. I'll fix it. – BusyProgrammer Mar 29 '17 at 23:48
  • @Xorn The value passed to the `open()` function must be of type `const char *`. Just use `std::string`. – BusyProgrammer Mar 29 '17 at 23:49
  • @Xorn By the way, if the answer worked, please select the checkmark next to my answer. It should turn green when you do so. Thanks! – BusyProgrammer Mar 29 '17 at 23:49