-5

I've tried compiling the following program in a Linux Mint Cinnamon VM and get many errors, yet it runs perfectly fine on Windows.The command I'm using is gcc Main.cpp -o Main

#include "Main.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>


using namespace std;
int Main::count = 0;
string festival = "";
std::vector<string> v;

Main::Main()
{
}


Main::~Main()
{
}


int main() {
Main main;
cout << "Please enter the festival name.";
getline(cin, festival);
main.nameFile();
main.readFile();
system("PAUSE");
return 0;
}

void Main::nameFile() {
ifstream inFile;
inFile.open("names.txt");
if (inFile.fail()) {
            cerr << "Error Opening \"names.txt\"" << endl;
            exit(1);
        }
string line;
while (getline(inFile, line)) {
    v.push_back(line);
    ofstream outFile(line + ".txt");
}
}



void Main::readFile()
{
while (v.size() != 0) {
    string name;
    name = v.at(v.size() - 1);
    v.pop_back();
    std::ofstream out(name + ".txt");
    ifstream file;
    file.open("letter.txt");

    string line, nameHolder = "@name@", festivalHolder = "@festival@";
    while (std::getline(file, line))
    {
        std::size_t n_i = line.find(nameHolder);
        if (n_i != std::string::npos)
            line.replace(n_i, nameHolder.size(), name);

        std::size_t f_i = line.find(festivalHolder);
        if (f_i != std::string::npos)
            line.replace(f_i, festivalHolder.size(), festival);

        out << line << '\n';
    }
}
}

 #pragma once
 class Main
{
private: 
static int count;
//static string festival;
public:
Main();
~Main();
void readFile();
void nameFile();
};

Here are the errors I get on Linux:

Main.cpp: In function ‘int main()’:
Main.cpp:30:16: error: ‘system’ was not declared in this scope system("PAUSE");

Main.cpp: In member function ‘void Main::nameFile()’:
Main.cpp:39:11: error: ‘exit’ was not declared in this scope exit(1);

Main.cpp:44:33: error: no matching function for call to ‘std::basic_ofstream::basic_ofstream(std::basic_string)’
ofstream outFile(line + ".txt");

Main.cpp:44:33: note: candidates are:
In file included from Main.cpp:2:0: /usr/include/c++/4.8/fstream:640:7: note: std::basic_ofstream<_CharT, _ Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode] basic_ofstream(const char* __s,

/usr/include/c++/4.8/fstream:640:7: note: no known conversion for argument 1 from ‘std::basic_string’ to ‘const char*’ /usr/include/c++/4.8/fstream:625:7: note: std::basic_ofstream<_CharT, _ Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits] basic_ofstream(): __ostream_type(), _M_filebuf()

/usr/include/c++/4.8/fstream:625:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8/fstream:599:11: note: std::basic_ofstream::basic_ofstream(const std::basic_ofstream&) class basic_ofstream : public basic_ostream<_CharT,_Traits>

/usr/include/c++/4.8/fstream:599:11: note: no known conversion for argument 1 from ‘std::basic_string’ to ‘const std::basic_ofstream&’
Main.cpp: In member function ‘void Main::readFile()’:

Main.cpp:56:34: error: no matching function for call to ‘ std::basic_ofstream::basic_ofstream(std::basic_string)’ std::ofstream out(name + ".txt");

Main.cpp:56:34: note: candidates are:
In file included from Main.cpp:2:0: /usr/include/c++/4.8/fstream:640:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode] basic_ofstream(const char* __s,

/usr/include/c++/4.8/fstream:640:7: note: no known conversion for argument 1 from ‘std::basic_string’ to ‘const char*’
/usr/include/c++/4.8/fstream:625:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits] basic_ofstream(): __ostream_type(), _M_filebuf()

/usr/include/c++/4.8/fstream:625:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8/fstream:599:11: note: std::basic_ofstream::basic_ofstream(const std::basic_ofstream&) class basic_ofstream : public basic_ostream<_CharT,_Traits>

/usr/include/c++/4.8/fstream:599:11: note: no known conversion for argument 1 from ‘std::basic_string’ to ‘const std::basic_ofstream&’

And here's the output I get if I compile + run on Windows:

names.txt:

John Boehner

Barack Obama

John Doe

Jim Shoe

Bill Ding

letter.txt:

Jane Doe

Room 213-A

Generic Old Building

School of Information Technology

Programming State University

New York NY 12345-0987

USA

To: @name@

Subject: Season's greetings: @festival@

Dear @name@,

A very @festival@ to you and your family!

Your sincerely,

Jane

Tas
  • 7,023
  • 3
  • 36
  • 51
freedom666
  • 23
  • 4
  • 5
    Do you think providing the errors you're receiving would be appropriate, or do you instead think that anyone that might be willing to help you should have to discover what they are on their own? Providing the names.txt and letter.txt you hope to use at run time is quite useless if the help you seek is in getting your program to compile. – mah Apr 29 '16 at 01:18
  • Need to see the errors... – Dialecticus Apr 29 '16 at 01:20
  • 2
    I think, there's no `PAUSE` command on linuxes. – krOoze Apr 29 '16 at 01:23

1 Answers1

1

Without seeing the errors that you get, I'm just guessing that it's the problem that jumps out at me. The system function just hands your command to the operating system. Linux doesn't recognize the "PAUSE" command.

Don't use system for cross-platform code, because different operating systems recognize different commands.

C++: Equivalent of system("pause"); from Windows in Linux

Edit: Looking at your errors, I would guess that you need to include some extra headers in Linux. (The error: ‘exit’ was not declared in this scope and errors like that usually indicate a missing header.) In my experience, some of the Microsoft header files will include about half of the standard library headers, which can make maintaining cross-platform code annoying when developers try to clean up un-used headers without checking the other operating system.

If you look up the not declared functions, you should be able to find out which headers need to be included.

Kyle A
  • 928
  • 7
  • 17
  • ok, thank you, thats a start – freedom666 Apr 29 '16 at 01:27
  • 2
    Small correction: [don't use `system`](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong), ever – Tas Apr 29 '16 at 01:32
  • @Tas, I agree, but `system` can be useful for small test programs that you know you will never release, which is why I don't take a hard-line on it. If you ever want to give the code to anyone else, then yes, never use `system`. – Kyle A Apr 29 '16 at 01:36