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;
}
Related
Though this is a fairly simple question, I am having trouble opening the file through the inputFileStream(inputFilePath). Could someone just lead me in the correct direction (I'm not here to be given answers for a school assignment)?
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
const string ID_LINE = "James McMillan - CS 1336 050 - Assignment 26";
int main(int argc, char *argv[]) {
cout << ID_LINE << endl << endl;
// guard clause - invalid number of arguments
if (argc != 3) {
cout << "Usage: " << argv[0] << "<input file path> <output file path>" << endl;
return 1;
}
//extract arguments
string programPath = argv[0];
string inputFilePath = argv[1];
string outputFilePath = argv[2];
cout << "Program path: " << programPath << endl;
cout << "Input file path: " << inputFilePath << endl;
cout << "Output file path: " << outputFilePath << endl << endl;
cout << "Creating input file stream..." << endl;
ifstream inputFileStream;
cout << "Created input file stream." << endl;
cout << "Opening input file stream: " << inputFilePath << endl;
inputFileStream.open(inputFilePath);
if (!inputFileStream.is_open()) {
cout << "Unable to open input file stream: " << inputFilePath << endl;
return 1;
}
}
Your solution might come from checking if the file exists, try
//Add this at the top
#include <experimental/filesystem>
//somewhere in the body
const auto has_the_file = std::experimental::filesystem::exists(inputFilePath);
if (!has_the_file)
{
std::cout << "Oh No! Can't find" << inputFilePath << std::endl;
}
Also instead of parsing the arguments, I would manually set the paths to confirm your expectations on how to specify file paths.
Currently have this simple code written, trying to figure out why it's not counting specific words every time.
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int t = 0;
int a1 = 0;
string a[100];
ifstream food;
food.open("food.txt");
if (food.fail())
{
cout << "File can't open" << endl;
}
else
cout << "File successfully opened" << endl;
int i = 0;
while (!food.eof())
{
// Tomato
food >> a[i];
if (a[i] == "tomato")
{
t = t + 1;
}
i++;
// Apple
food >> a[i];
if (a[i] == "apple")
{
a1 = a1 + 1;
}
i++;
}
cout << "Amount of Tomatos: " << t << endl;
cout << "Amount of Apples: " << a1 << endl;
}
The text file I'm using:
apple
apple
tomato
apple
tomato
tomato
The output:
File successfully opened
Amount of Tomatoes: 2
Amount of Apples: 2
The purpose is to find the amount of each food found in the list. I'm currently only using two kinds of food but will have many more.
There are several problems with your code.
using eof() in a loop incorrectly. You can't check eof() before performing a read operation first.
using an array without bounds checking. For that matter, you don't need an array at all.
skipping words, which is why you are not counting everything you are expecting. Let's take the very first line, apple. Since it is not a "tomato", you skip it and read the next word in the file, which is "apple", so you count it. But you didn't count the 1st apple at all.
You need to do something more like this instead:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int tomatoes = 0;
int apples = 0;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s)
{
// Tomato
if (s == "tomato")
++tomatoes;
// Apple
else if (s == "apple")
++apples;
}
cout << "Amount of Tomatos: " << tomatoes << endl;
cout << "Amount of Apples: " << apples << endl;
return 0;
}
Alternatively, as #user463035818 mentioned in comments, you can use a std::map instead:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> foods;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s) {
foods[s]++;
}
for (map<string, int>::iterator iter = foods.begin(); iter != foods.end(); ++iter) {
cout << "Amount of " << iter->first << ": " << iter->second << endl;
}
/* or, if you are using C++11 or later...
for (auto &item : foods) {
cout << "Amount of " << item.first << ": " << item.second << endl;
}
*/
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 working on a function in a program to read an input from a user then, the function will check to see if that function exists and then runs it, but it is very buggy and the functions meant to be run, run twice.
//functions with a int and a string
std::map<std::string, std::function<void(int, string)>> functionsIS = {
{"printWordWithNumber", numberPlusWord},
};
//functions with no parameters
std::map<std::string, std::function<void()>> functionsNI = {
{"Help", userHelp},
};
void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
if (functionsIS[command]){
std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
std::cout << "Enter paramater one (integer) : ";
std::cin >> paramInt;
std::cout << std::endl<<"Enter paramater two (string)" << std::endl;
std::cin.ignore();
std::getline(std::cin,paramString);
std::cout << "running..." << std::endl;
functionsIS[command](paramInt,paramString);
}
}
for (int i = 0; i < functionsNI.size(); i = i++){
if (functionsNI[command]){
std::cout << "Accessed '" << command << "' running..." << std::endl;
functionsNI[command]();
}
}
}
Here is version for you to run:
In source:
#include <iostream>
#include <map>
#include <windows.h>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include "userFunctions.h"//header file for functions
using namespace std;
std::string input;
//functions with a int and a string
std::map<std::string, std::function<void(int, string)>> functionsIS = {
{ "printWordWithNumber", numberPlusWord },
};
//functions with no parameters
std::map<std::string, std::function<void()>> functionsNI = {
{ "Help", userHelp },
};
void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
if (functionsIS[command]){
std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
std::cout << "Enter paramater one (integer) : ";
std::cin >> paramInt;
std::cout << std::endl << "Enter paramater two (string)" << std::endl;
std::cin.ignore();
std::getline(std::cin, paramString);
std::cout << "running..." << std::endl;
functionsIS[command](paramInt, paramString);
}
}
for (int i = 0; i < functionsNI.size(); i = i++){
if (functionsNI[command]){
std::cout << "Accessed '" << command << "' running..." << std::endl;
functionsNI[command]();
}
}
}
int main(){
do{
std::cout << "Waiting For Command..." << std::endl;
cin >> input;
CommandCheck(input);
} while (input != "end");
return 0;
}
Create a header file called "functions" and paste this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void numberPlusWord(int number, std::string word){
std::cout << word << std::endl;
std::cout << number << std::endl;
}
void userHelp(){
std::cout << "I can help!" << std::endl;
}
There are a couple of problems with your code that would cause problems. The first is that you are iterationg through the function map and if the command exists you are calling it. The problem is that you are checking for the same command on every iteration so if the map contains more than one element the command will be called for each of them. You can resolve this by removing the for loop and using the find function of the map to determine if the command exists.
The second problem is that if the command does not exist an element will be created in the map for it. The if statement below will automatically insert an element using command as the key.
if(functionsIS[command]) { /*...*/}
The following update to your code will correct the problem.
void CommandCheck(std::string command)
{
int paramInt;
string paramString;
if (functionsIS.find(command) != functionsIS.end())
{
std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
std::cout << "Enter paramater one (integer) : ";
std::cin >> paramInt;
std::cout << std::endl << "Enter paramater two (string)" << std::endl;
std::cin.ignore();
std::getline(std::cin, paramString);
std::cout << "running..." << std::endl;
functionsIS[command](paramInt, paramString);
}
else if (functionsNI.find(command) != functionsNI.end())
{
std::cout << "Accessed '" << command << "' running..." << std::endl;
functionsNI[command]();
}
}
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"