myFile undeclared identifier - fstream

I get the following error when i try to solve my file:
myFile undeclared identifier
Help would be appreciated! :) I really dont see what is the problem is in this code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
int main()
{
ofstream myFile;
std::string name;
std::string password;
std::cout << "Enter Name" << std::endl;
std::cin >> name;
std::cout << "Enter password" << std::endl;
std::cin >> password;
std::string info = name + ":" + password;
myFile.open ("Database.txt");
myFile << (info) << std::endl << ;
myFile.close();
Sleep(10000);
}

You sould use : std::ofstream myFile to declare myFile
or you can add using namespace std; at the top of the programm (outside the main) and remove all std::.
If you want to add text to "Database.txt", you sould use :
myFile.open("Database.txt", ios::app);
Something is wrong :
myFile << (info) << std::endl << ;
You sould'nt keep the last <<
You also may want to return something in your main.

Related

Defining strict_real_policies for reals with a comma decimal character

I would like to create a custom policy derived from strict_real_policies that will parse reals, such as "3,14", i.e. with a comma decimal point as used e.g. in Germany.
That should be easy, right?
#include <iostream>
#include <string>
#include <boost/spirit/home/x3.hpp>
template <typename T>
struct decimal_comma_strict_real_policies:boost::spirit::x3::strict_real_policies<T>
{
template <typename Iterator>
static bool
parse_dot(Iterator& first, Iterator const& last)
{
if (first == last || *first != ',')
return false;
++first;
return true;
}
};
void parse(const std::string& input)
{
namespace x3=boost::spirit::x3;
std::cout << "Parsing '" << input << "'" << std::endl;
std::string::const_iterator iter=std::begin(input),end=std::end(input);
const auto parser = x3::real_parser<double, decimal_comma_strict_real_policies<double>>{};
double parsed_num;
bool result=x3::parse(iter,end,parser,parsed_num);
if(result && iter==end)
{
std::cout << "Parsed: " << parsed_num << std::endl;
}
else
{
std::cout << "Something failed." << std::endl;
}
}
int main()
{
parse("3,14");
parse("3.14");
}

counting length of string token using istringstream

I'm getting an error saying no matching function for call to 'getline' involving the getline(tokenizer, " "); Im not sure how to fix this, Ive tried including some other headers but I just keep coming up with more errors.
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <string>
using namespace std;
char encrypt(char character, int offset);
int main(int argc, char *argv[]) {
ifstream inputFile;
string str;
string token;
bool debug = true;
int lineLength = 0, offset;
inputFile.open(argv[1]);
if (inputFile.fail())
cout << "File failed to open. \n";
istringstream tokenizer(str);
getline(tokenizer, " ");
offset = token.length();
while (getline(inputFile, str)){
lineLength = str.length();
for (int i = 0; i < lineLength; i++)
str.at(i) = encrypt(str.at(i), offset);
cout << str << endl;
}
inputFile.close();
return(0);
}

Read data from fstream

I'm trying to read data from a fstream but this code doesn't work.
It puts 0 to the console.
Can you help me?
// g++ -Wall main.cpp -o main.exe
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::fstream file("main.txt", std::fstream::in | std::fstream::out | std::fstream::app);
file << "45634w6\n";
file << "dtusrjt\n";
while (!file.eof())
{
std::string line;
std::cout << std::getline(file, line) << "\n";
}
int a;
std::cin >> a;
}
Just try this code this code will help you
#include <fstream>
#include <iostream>
#include <string>
//#include <sstream>
using namespace std;
int main()
{
fstream file("main.txt");
file << "45634w6\n";
file << "dtusrjt\n";
file.close();
file.open("main.txt");
string line;
while (!file.fail())
{
getline(file, line);
cout <<line << "\n";
}
int a;
cin >> a;
}

SEGV on emitting null root (yaml-cpp)

I am trying yaml-cpp (r589:2c954b1ed301), but I have a trouble with following code.
#include <iostream>
#include <yaml-cpp/yaml.h>
int main()
{
YAML::Node doc;
std::cout << doc << std::endl; // SEGV
doc["sub"] = YAML::Node();
std::cout << doc << std::endl; // OK
doc = YAML::Load("");
std::cout << doc << std::endl; // OK
std::cout << YAML::Load("") << std::endl; // SEGV
std::cout << YAML::Load("a") << std::endl; // OK
YAML::Node doc2 = YAML::Load("");
std::cout << doc2 << std::endl; // SEGV
return 0;
}
The code is compiled with g++ 4.4.7 on Scientific Linux 6.4.
I am not sure that this error occurs only on my environment.
I would appreciate your comments and suggestions.
This was a bug, but it was fixed (see the bug report).

printing lines from files

I'm trying to print the first line from each file but I think its outputting the address instead.
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void FirstLineFromFile(ifstream files[], size_t count)
{
const int BUFSIZE = 511;
char buf[BUFSIZE];
ifstream *end, *start;
for (start = files, end = files + count; start < end; start++)
{
cout << start->getline(buf, sizeof(buf)) << '\n';
}
}
streams should not be passed by value. This code passes an array of streams by value. You can try to pass a vector instead and interate over them.
void FirstLineFromFile(vector<ifstream*> files) {
for (int i=0; i<files.size(); ++i) {
string s;
getline(*files[i], s);
cout << s << endl;
}
}
ifstream->getline does not return a string as its return value. You need to print out the buffer that it has filled in a separate line.
for (start = files, end = files + count; start < end; start++)
{
start->getline(buf, sizeof(buf));
cout << buf << '\n';
}