I'm fairly new to C++ and I'm getting the following error 'fw' is not a class or namespace in the main file when I try to call the create_event_file() function.
Here is my code.
#ifndef FILE_WRITER_H
#define FILE_WRITER_H
#include <string>
using namespace std;
class File_Writer{
private:
int test;
public:
File_Writer() { }
void create_event_file(void);
void write(const string file_name, string data);
};
#endif // FILE_WRITER_H
The cpp file
#include "file_writer.h"
#include <iostream>
#include <fstream>
using namespace std;
File_Writer::File_Writer(void){
cout << "Object of File_Writer is created" << endl;
}
void File_Writer::create_event_file(void){
ofstream outputFile;
outputFile.open("event.txt");
string data;
cout << "Enter event title : " << endl;
getline(cin,data);
outputFile << textToSave;
cout << "Enter event date : " << endl;
getline(cin,data);
outputFile << textToSave;
cout << "Enter event start time : " << endl;
getline(cin,data);
outputFile << textToSave;
outputFile.close();
}
void File_Writer::write(const string file_name, string data){
ofstream outputFile;
outputFile.open("all.txt");
outputFile << data;
outputFile.close();
}
And the main file
#include <iostream>
#include "file_writer.h"
using namespace std;
int main(){
string input;
File_Writer fw;
cout << "Welcome to the event creation program!\n" << endl;
cout << "---------------------------" << endl;
cout << "| event - To create event file |" << endl;
cout << "| entrants - To create entrants file |" << endl;
cout << "| coursess - To create courses file |" << endl;
cout << "---------------------------\n" << endl;
getline(cin,input);
if(input == "event")
fw::create_event_file();
}
Thanks in advance
Replace this, which implies that fw is the name of a class or namespace:
fw::create_event_file();
// ^^ This is a "scope opearator"
With this, which implies that fw is a variable:
fw.create_event_file();
// ^ This is a "member access opearator"
Related
I want to search argv[2] value in JSON but i couldn't do this. I looked the other threads but they couldn't hep either. Because i am a newbie. So ho can I do this?
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <json/value.h>
using namespace std;
// struct instead of class
struct Element {
int atomNumber;
string atomName;
string atomSymbol;
string atomType;
string atomSerie;
// void funciton instead of Book(){}
void printinfo() {
cout << "Atom Number: " << atomNumber << "\n";
cout << "Atom name: " << atomName << "\n";
cout << "Atom Symbol: " << atomSymbol << "\n";
cout << "Atom Type: " << atomType << "\n";
cout << "Atom Serie: " << atomSerie << "\n";
}
};
// Arg Count Args
// V V
int main(int argc, char* argv[]) {
Json::Value ptable;
std::ifstream ptable_file("ptable.json", std::ifstream::binary);
ptable_file >> ptable;
if (strcmp(argv[1], "-p") == 0){
};
return 0;
}
I have a problem with this code. Could you please help me to find out why it does not save new total donation values (total) to the file (donation_total.txt)? It should be noted that the default value saved in the file is zero, but it keeps it for each iteration. I need the new value is saved at the last line each time.
Thanks
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
fstream records;
records.open("donation_total.txt", ios_base::in | ios_base::out | ios_base::app);
if (records.is_open())
{
cout << "The record file is open!" << endl;
string line;
while (!records.eof())
{
getline(records, line);
}
int total = stoi(line);
cout << "Total donation is: "<<total << endl;
cout << "Is there any new record?" << endl;
string str,newname;
stringstream field;
string name;
int donation;
getline(cin , str);
while (str=="Yes" | str=="yes")
{
cout << "Enter name and donation value: ";
getline(cin, newname);
field.str(newname);
field >> name >> donation;
total += donation;
field.clear();
cout << name << " donates " << donation << "$" << endl;
cout << "Total donation is: " << total << endl;
records << endl << total;
cout << "Is there any new record?" << endl;
getline(cin, str);
}
}
else
{
cerr << "Could not find the file!" << endl;
}
records.close();
return 0;
}
Your program has many logical incorrectness. When a name is entered with no donation amount, it takes the previously donated value.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main()
{
fstream records;
records.open("donation_total.txt", ios_base::in | ios_base::out | ios_base::app);
if (records.is_open())
{
cout << "The record file is open!" << endl;
string line;
while (getline(cin, line))
{
int total = stoi(line);
cout << "Total donation is: "<<total << endl;
cout << "Is there any new record?" << endl;
string str,newname;
stringstream field;
string name;
int donation;
getline(cin , str);
while (str=="Yes" || str=="yes")
{
cout << "Enter name and donation value: ";
getline(cin, newname);
field.str(newname);
field >> name >> donation;
total += donation;
field.clear();
cout << name << " donates " << donation << "$" << endl;
cout << "Total donation is: " << total << endl;
records << endl << total;
cout << "Is there any new record? ";
getline(cin, str);
}
break;
}
}
else
{
cerr << "Could not find the file!" << endl;
}
records.close();
return 0;
}
This is my .h, header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
namespace westeros {
class Kingdom {
public:
char m_name[32];
int m_population;
};
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom);
}
#endif
This is my .cpp, source file
#include <iostream>
#include "kingdom.h"
using namespace std;
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom){
cout << "------------------------------" << endl;
for (int i = 0; i < kingdomElement; i++) {
**if(pKingdom[i].m_name == nameOfKingdom){** //it's giving me error right here, visual studio underlining red line below == sign saying operand types are incompatible
cout << "Searching for kingdom " << pKingdom[i].m_name << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << pKingdom[i].m_name << ", population " << pKingdom[i].m_population << endl;
}
else {
cout << "------------------------------" << endl;
cout << "Searching for kingdom " << nameOfKingdom << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << nameOfKingdom << " is not part of Westeros." << endl;
cout << "------------------------------" << endl;
}
}
}
}
and this is my main file trying to call it
#include <iostream>
#include "kingdom.h"
using namespace std;
using namespace westeros;
int main(void)
{
int count = 0; // the number of kingdoms in the array
Kingdom* pKingdoms = nullptr;
//allocating dynamic memory
pKingdoms = new Kingdom[count];
display(pKingdoms, count, "Mordor");
cout << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
Can anyone find what could be the problem?
Your problem is that pKingdom[i].m_name is a char[32], and the type of nameOfKingdom is char. You cannot compare a character array with a character.
which type would I use then?
std::string
I am trying to search a specific ID/ registration from a .txt and display the corresponding info accordingly. In this case I want to display the pricing according to the corresponding registration number which should be entered.
Reading and writing files is not the issue for me. There is a lot of info on the web in regards with reading and writing, but not much on searching and displaying according to the ID/ registration.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string line;
double cost;
string reg;
ifstream in_stream;
ofstream out_stream;
char registration[10];
//Open file
in_stream.open("Fines.dat");
//Error if opening fails
if (in_stream.fail())
{
cout << "Input file could not open. " << endl;
exit(1);
}
//Open out stream file
out_stream.open("OutStandingFines.dat");
//Error if opening fails
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
//Display original .dat file
cout <<"Original .dat File" << endl;
if(in_stream.is_open())
{
while(in_stream >> reg >> cost)
{
cout << reg <<" " << cost << '\n';
}
in_stream.close();
}
else
{
cout <<"File is not open: " << endl;
}
/////////////My problem is from here//////////////////////////
//Enter the registration number you wish to search
cout << "Please enter registration number: " << endl;
cin >>registration;
//I must display all cost values that have the same registration number???????? I need help with this
/*
for (int i = 0; i < 10; i++)
{
if( reg == registration)
{
cout << fixed << setw(2)<< setprecision(2) <<"R " << cost << '\n';
out_stream << fixed << setw(2)<< setprecision(2) << "R "<< cost << endl; //send back to .dat
}
}
*/
in_stream.close();
out_stream.close();
system("pause");
return(0);
}
Ok, I managed to get the output. The problem I have now is to display all the values with the same registration/ ID.
For some reason only the last row in the fstream .txt gets displayed when I enter "ABC123".
The input .txt contains the following info.
ABC123 400
DEC234 340
ABC123 500
GED345 600
ABC123 240
GEE600 120
GED345 230
GEE600 470
ABC123 120
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string line;
double cost;
string reg;
ifstream in_stream;
ofstream out_stream;
string registration;
in_stream.open("Fines.dat");
if (in_stream.fail())
{
cout << "Input file could not open. " << endl;
exit(1);
}
out_stream.open("OutStandingFines.dat");
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
//Display original .dat file
cout <<"Original .dat File" << endl;
if(in_stream.is_open())
{
while(in_stream >> reg >> cost)
{
cout << reg <<" " << cost << '\n';
}
}
else
{
cout <<"File is not open: " << endl;
}
//Enter Registration here
cout << "Please enter registration number: " << endl;
cin >>registration;
//compare and display all registration numbers that match
cout <<"Fines: " << endl;
if(out_stream.is_open())
{
while(reg == registration)
{
cout << fixed << setw(2)<< setprecision(2) <<"R " << cost << '\n';
out_stream << fixed << setw(2)<< setprecision(2) << "R "<< cost << endl; //send back to .dat
exit(1);
}
}
else
{
cout <<"File is not open: " << endl;
}
in_stream.close();
out_stream.close();
system("pause");
return(0);
}
I'm trying to include experiencecalculator from a class but I get this Error: a nonstatic member reference must be relative to a specific object.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include "ExpCalc.h"
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
int main() {
cout << "Pick which calculator you would like to use by typing the correct "
"number.\n";
cout << "1. Experience Calculator" << endl;
// cout << "" Insert other calculators and there number here.
// cout << ""
int choice;
cin >> choice;
if (choice == 1) {
ExpCalc::ExperienceCalculator;
}
}
The class I am taking it from is:
ExpCalc.h
class ExpCalc
{
public:
ExpCalc();
int ExperienceCalculator;
};
ExpCalc.cpp
#include "stdafx.h"
#include "ExpCalc.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
ExpCalc::ExpCalc() {}
int ExperienceCalculator() {
double timetotal;
double timeperinv;
double xptotal;
double xpitem;
double amount;
double perinv;
double totalinv;
double costper;
double costtotal;
SetConsoleTitle(TEXT("Runescape Skill Calculator"));
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
"Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
cout << endl;
cout << "How much experience do you want to get?" << endl;
cin >> xptotal;
cout << endl;
cout << "How much does it cost per item?" << endl;
cin >> costper;
cout << endl;
cout << "How much experience do you get per item?" << endl;
cin >> xpitem;
cout << endl;
cout << "How many items can you process in one inventory?" << endl;
cin >> perinv;
cout << endl;
cout << "How long does it take to process one inventory of items?" << endl;
cin >> timeperinv;
system("CLS");
amount = xptotal / xpitem;
totalinv = amount / perinv;
timetotal = totalinv * timeperinv;
costtotal = amount * costper;
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
"Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
cout << endl;
std::cout << std::setprecision(1) << fixed;
cout << "The amount of items that you will need to process is: \n" << amount
<< endl;
cout << endl;
cout << "The total amount of inventories to process all items is: \n"
<< totalinv << endl;
cout << endl;
cout << "The total time it will take to complete processing all items is:\n"
<< timetotal << endl;
cout << endl;
cout << "The total cost of the items will be: \n" << totalinv << endl;
cout << endl;
cout << "The total amount of inventories to process is: \n" << totalinv
<< endl;
cout << endl;
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+==+=+==+=+==+=+==+=+==+=+=+=+=+=+=+="
"+=+=+=+=+=+=+=" << endl;
system("PAUSE");
return 0;
};
Any help will be much appreciated!
Your H file describes ExperienceCalculator as int field. Your CPP file describes ExperienceCalculator as a free function (even not a method of ExpCalc). So I suspect that you have to do the following amends:
H file:
int ExperienceCalculator(); // parenthesis to be added
CPP file:
int ExpCalc::ExperienceCalculator() { // class name ExpCalc to be added
main file:
if (choice == 1) {
ExpCalc exp_calc; // instantiate the class
exp_calc.ExperienceCalculator(); // make a call to non-static method
}
Alternatively, you can make the method as static but let make one step at a time. Happy coding!