I'm using the Armadillo library in a C++ project (with Xcode) and I would like to create a .mat file containing matrix A (as in the code hereafter).
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main ()
{
mat A;
A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
<< 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
<< 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
<< 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
<< 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
cout << A << endl;
bool status = A.save("test.mat", arma_ascii);
if(status == true)
{
cout << "Save OK !" << endl << endl;
}
else
{
cout << "Problem with save" << endl << endl;
}
mat B;
B.load("test.mat", arma_ascii);
cout << B;
return 0;
}
Everything works fine, as you can see in the results below, except the fact that I cannot find the file "test.mat" anywhere :( ! It is supposed to be in the project's folder, but unfortunately this is not the case! Anyone have any idea to solve this issue?
0.1653 0.4540 0.9958 0.1241 0.0471
0.6888 0.0365 0.5528 0.9377 0.8664
0.3487 0.4794 0.5062 0.1457 0.4915
0.1487 0.6823 0.5712 0.8747 0.4446
0.2457 0.5952 0.4093 0.3678 0.3857
Save OK !
0.1653 0.4540 0.9958 0.1241 0.0471
0.6888 0.0365 0.5528 0.9377 0.8664
0.3487 0.4794 0.5062 0.1457 0.4915
0.1487 0.6823 0.5712 0.8747 0.4446
0.2457 0.5952 0.4093 0.3678 0.3857
Thanks in advance!
P.S. I have problems with the function load() too. It always fail to read the file located in the project's folder.
Related
I'm learning c++ and I have the following code which gives an error in line 39 (fill_file() call). I've searched on the web for a solution but can't find why I get this error (expected primary-expression before '&' token).
#include <iostream>
#include <string>
#include <vector>
#include "../std_lib_facilities.h"
using namespace std;
struct Point {
double x;
double y;
};
void fill_file(vector<Point>& original_points) {
string outputfile="mydata.txt";
ofstream ost{outputfile};
if(!ost) error("Can't open outputfile ", outputfile);
for(int i=0;i<original_points.size();i++) {
ost << original_points[i].x << " " << original_points[i].y << endl;
}
}
int main() {
cout << "Please enter 3 points with a value: " << endl;
vector<Point> original_points;
Point p;
double x;
double y;
for(int i=0;i<3;i++) {
cin>>p.x;
cin>>p.y;
original_points.push_back(p);
}
cout << endl;
cout << endl << "Points: " << endl;
for(int i=0;i<original_points.size();i++) {
cout << original_points[i].x << " " << original_points[i].y << endl;
/* ost << original_points[i].x << " " << original_points[i].y << endl; */
}
cout << endl << endl;
fill_file(vector<Point>& original_points);
return 0;
}
What am I doing wrong? Thx for the help!!
You made a mistake when you called your fill_file function:
fill_file(vector<Point>& original_points);
must be called like this:
fill_file(original_points);
You made an error calling the function fill_file(). Currently you call it like this:
fill_file(vector<Point>& original_points);
This above, I presume is a copy paste error. What I thing you want to do is:
fill_file(original_points);
because original_points is the actual variable, not vector<Point>& original_points. As your error states:
expected primary-expression before '&' token
As seen above, you are putting a random l-value in the function call, and this is not allowed.
That's my first question here, so I would be glad to receive some support on the style I used to refer to my problem :). Here is the finished program, its main purpose is to split given words into halves and create words replacing the origin ones. Replaced words are build from its origins by spliting them into halves and taking even ones from the 1st half begining with the first letter of a word. Heres the complete code:
#include <iostream>
#include <string>
#include <cstdio>
#include <math.h>
using namespace std;
void obcinaczSlow(int);
int main(){
int ilosc;
cout << "Prosze o podanie ilosci prob: ";
cin>>ilosc;
cout << endl;
obcinaczSlow(ilosc);
cin.ignore();
cin.get();
return 0;
}
void obcinaczSlow(int ilosc_prob){
int i=0,j=0,dlugosc_slowa=0,dlugosc_polowy=0;
string *tablica_slow,budowane_slowo,aktualne_slowo,dodane;
tablica_slow = new string [ilosc_prob];
cout << "Prosze o podanie " << ilosc_prob << " slow" << endl;
cin.sync();
for(i=0;i<ilosc_prob;i++){
cout << "Prosze o podanie slowa numer: " << i+1 << endl;
cin>>aktualne_slowo;
tablica_slow[i] = aktualne_slowo;
}
for(i=0;i<ilosc_prob;i++){
aktualne_slowo = tablica_slow[i];
cout << "Aktualne slowo do przerobienia: " << aktualne_slowo << endl;
dlugosc_slowa = aktualne_slowo.length();
cout << "Dlugosc slowa do przerobienia: " << dlugosc_slowa << endl;
dlugosc_polowy = floor(dlugosc_slowa/2);
cout << "Dlugosc polowy slowa int: " << dlugosc_polowy << endl;
budowane_slowo.clear();
dodane.clear();
cout << "Budowane slowo to: " << budowane_slowo << endl;
for(j=0;j<=dlugosc_polowy;j=+2){
dodane = aktualne_slowo.at(j);
budowane_slowo.append(dodane);
}
tablica_slow[i] = budowane_slowo;
}
cout << "Slowa po transformacji wygladaja nastepujaco: " << endl;
for(i=0;i<ilosc_prob;i++){
cout << "Slowo o numerze " << i+1 << " : " << tablica_slow[i] << endl;
}
delete [] tablica_slow;
cin.sync();
}
The problem raises when program reaches the loop, that is supposed to append the letter pointed by the j-index using '.at' method from the string class. I can't find a solution even trying to debug it. Could You help me :)?
You have a typo here
for(j=0;j<=dlugosc_polowy;j=+2)
I assume you meant += instead of =+
for(j=0;j<=dlugosc_polowy;j+=2)
Otherwise you are just assigning 2 to j over and over again.
Your error is reversing two characters:
Change:
`j=+2` to `j+=2`
^^ ^^
(The way it is written j is assigned the value of 2, then, for the rest of its life, stays there.)
for(j=0;j<=dlugosc_polowy;j=+2){
dodane = aktualne_slowo.at(j);
budowane_slowo.append(dodane);
}
replace the j=+2 to j+=2
for(j=0;j<=dlugosc_polowy;j+=2){
dodane = aktualne_slowo.at(j);
budowane_slowo.append(dodane);
}
So I recently decided to pick up programming again and went with C++. Tried to make an adventurer class, but I seem to be running into some trouble. Here are my files:
Adventurer.h:
#ifndef __Adventurer_H_INCLUDED__ //if Adventurer.h hasn't been included yet...
#define __Adventurer_H_INCLUDED__ //#define this so the compiler knows it has been included
class Adventurer
{
private:
int hp, mp, str, agi, magic, armour;
public:
Adventurer(){}
void printStats();
}
#endif
Adventurer.cpp:
#include <iostream>
#include "Adventurer.h"
Adventurer::Adventurer()
{
hp = 50;
mp = 25;
str = 5;
agi = 5;
magic = 5;
armour = 5;
}
void Adventurer::printStats()
{
cout << "HP = " << hp << "\n\n";
cout << "MP = " << mp << "\n\n";
cout << "str = " << str << "\n\n";
cout << "agi = " << agi << "\n\n";
cout << "magic = " << magic << "\n\n";
cout << "armour = " << armour << "\n\n";
}
RPG_Game.cpp:
// my first program in C++
#include <iostream>
#include <string>
#include "Adventurer.h"
;using namespace std;
int main()
{
cout << "Hello Adventurer! What is your name? \n";
string advName;
cin >> advName;
cout << "\nYour name is " << advName << "!";
Adventurer *adv = new Adventurer();
cout << adv.printStats();
delete adv;
system(pause);
}
Let's look at the errors in your code
First, in your Adventurer.h, put a semicolon (;) after the class.
Next, in that same class, you have
Adventurer(){}
change this to
Adventurer();
Then, in your RPG_Game.cpp , change
cout << adv.printStats();
to
adv->printStats() ;
When using pointers, you need to use -> and not .
And lastly,
system(pause);
should be
system( "pause" );
Now, try running your code.
Also, you might find this helpful.
so i have this code for checking crc file named map.spak and compare the result with my specified crc result which stored in variable "compare"
int main(int iArg, char *sArg[])
{
char sSourceFile[MAX_PATH];
memset(sSourceFile, 0, sizeof(sSourceFile));
CCRC32 crc32;
crc32.Initialize(); //Only have to do this once.
unsigned int iCRC = 0;
strcpy(sSourceFile, "map.spak");
int compare = 399857339;
ifstream checkfile(sSourceFile);
if (checkfile){
cout << "Checking file " << sSourceFile << "..." << endl;
crc32.FileCRC(sSourceFile, &iCRC);
if(iCRC == compare){
cout << "File " << sSourceFile << " complete!\nCRC Result: " << iCRC << endl;
}else{
cout << "File " << sSourceFile << " incomplete!\nCRC Result: " << iCRC << endl;
}
}else{
cout << "File not found!" << endl;
}
system("pause");
return 0;
}
and now i want to make this code for multiple file
let's say the file name list stored in filelist.txt
the filelist.txt structure:
id|filename|specified crc
1|map.spak|399857339
2|monster.spak|274394072
how to make the crc check, loop for each file name
i'm not really good at c++ i only know some algorithm because i know PHP
c++ is too complicated
this is the full source included CRC source Source Code
or pastebin
TestApp.cpp link
I made several changes to your code. I removed guard headers since we use it only in header files. Old-fasioned memset has been replaced by operation on strings. I suspect that you need to pass char* to CCRC32 object hence sSourceFile is still const char*. I compiled code except parts with CCRC32.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "../CCRC32.H"
int main(int iArg, char *sArg[])
{
std::vector<std::string> filenames;
// TODO - populate filesnames (paths?)
CCRC32 crc32;
crc32.Initialize(); //Only have to do this once.
for (unsigned int i = 0; i < filenames.size(); i++) {
const char* sSourceFile = filenames[i].c_str();
unsigned int iCRC = 0;
int compare = 399857339; // TODO - you need to change this since you are checking several files
std::ifstream checkfile(sSourceFile);
if (checkfile) {
std::cout << "Checking file " << sSourceFile << "..." << std::endl;
crc32.FileCRC(sSourceFile, &iCRC);
if(iCRC == compare){
std::cout << "File " << sSourceFile << " complete!\nCRC Result: " << iCRC << std::endl;
} else {
std::cout << "File " << sSourceFile << " incomplete!\nCRC Result: " << iCRC << std::endl;
}
} else {
std::cout << "File tidak ditemukan!" << std::endl;
}
}
return 0;
}
Okay, I'm working on a project for school, and we need to have a linked list of a class within another class (a linked list of the class "task" inside a class called "objectives"), so for this i'm using the STL class . Now I almost have it set up, but in my display function, to display the contents of the tasks, I'm using an iterator. But I can't assign taskList.begin() to the iterator because it gives me an error.
The following is the code that I think is relevant.
objective.h
#ifndef OBJECTIVE_H
#define OBJECTIVE_H
#include <string>
#include <list>
#include "date.h"
#include "task.h"
using namespace std;
namespace team2
{
class objective
{
private:
string objective_name, objective_desc, resources[10];
int category, priority, res_used;
double time;
date start, end;
int status;
std::list<task> taskList;
public:
// CONSTRUCTORS
objective();
objective(string objN, string objD, int c, int p, date s, date e, double t, string res[], int resU, int stat, list<task>& tList);
...
// CONSTANT MEMBER FUNCTIONS
void display() const;
...
};
}
#endif
objective.cpp (This is where I get the error)
#include "objective.h"
#include "date.h"
#include <cstdlib>
#include <cassert>
#include <string>
#include <list>
#include "task.h"
using namespace std;
namespace team2
{
void objective::display() const // display() - Displays the complete contents of a single objective
{
int days, hours, minutes;
std::list<task>::iterator taskIterator;
days = floor(time/24.0); // Find the max number of days based off of the time (in hours)
hours = floor(time - days*24); // Find the max number of hours after deduction of days
minutes = floor((time - (days*24 + hours))*60); // Find the number or minutes after taking into account hours and days
cout << "\nObjective Name: " << objective_name << endl;
cout << "Objective Description: " << objective_desc << endl;
cout << "Category: Quad " << category << endl;
cout << "Priority: " << priority << endl;
cout << "Starting Date: " << start.getMonth() << "/" << start.getDay() << "/" << start.getYear() << endl;
cout << "Ending Date: " << end.getMonth() << "/" << end.getDay() << "/" << end.getYear() << endl;
cout << "Time Required: " << days << " Days " << hours << " Hours " << minutes << " Minutes " << endl;
cout << "Resources: " << endl;
if(res_used == 0)
cout << "\tNo Resources" << endl;
for(int i = 0; i < res_used; i++)
cout << "\t" << resources [i] << endl;
cout << "Current Status: ";
if(status == 1)
cout << "Completed" << endl;
else if(status == 0)
cout << "Incomplete" << endl;
cout << "Tasks: " << endl;
if(taskList.empty())
cout << "\tNo Resources" << endl;
for(taskIterator = taskList.begin(); taskIterator != taskList.end(); taskIterator++)
{
(*taskIterator).display();
cout << endl;
}
}
}
The tasks class is almost Identical to the objective class, with a few fields omitted. The error occurs in the for loop. for(taskIterator = taskList.begin();...) Anyone know the cause for the problem? I can also provide more code if necessary. Thank you in advance!
The method is const, taskList is a member, so you can't have a non-const iterator on it.
Making a member method const is a contract that that method will not change non-mutable class members nor call non-const member methods. By having a non-const iterator, you're breaking that contract.
Since display is const, you can use a const iterator:
std::list<task>::const_iterator taskIterator;