reading pairs of data from a file - c++

I am trying to read pairs of data from a file using the following code.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
//**** Opening data file ****
ifstream infile;
infile.open("reg_data_log.inp");
if (infile.is_open())
{
cout << "File successfully open" << endl;
}
else
{
cout << "Error opening file";
}
//**** Reading data ***
vector<pair<double, double> > proteins;
pair<double, double> input;
while (infile >> input.first >> input.second)
{
proteins.push_back(input);
}
//**** Show data in screen ****
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
}
when compiling I have the following message
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
I cannot get my head around the problem. Can anybody help?

Look further at your loop
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
You are trying to access the property first of the integer value i. integers do not have that property it is a property of a pair object.
I think you are getting confused between iterating using iterator and indices. The simple fix is to use a range based for loop, as already suggested in comments.
for(auto d: proteins)
{
cout << d.first << ", " << d.second << endl;
}
Now what you have as is the element from the vector and not an integer. You can now access first and second.
If you cannot use range based for loop and auto, then you can use the old iterator for loop way.
for(vector<pair<double, double> >::iterator it = proteins.begin();
it != proteins.end();
++it)
{
cout << it->first << ", " << it->second << endl;
}
and someone else has already posted the how it can be done using indices, so I won't repeat that here.

As John Mopp mentions in his comment, indeed you arev referencing an int and not a pair-type. The following loop will most likely fix your issues:
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << proteins[i].first << ", " << proteins[i].second << endl;
}
I did not test this yet, but I am pretty convinced this solves your problem.

Related

c++ error: expected primary-expression before '&' token

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.

make some crc check code, loop for multiple file (c++)

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;
}

Data doesn't load into c++ vector correctly

I am trying to load a text file and import the contents into a vector of structs.
Here are my definitions
typedef struct
{
string pcName, pcUsername, pcPassword, pcMessage, pcAdvertisement; //I know that
//this is incorrect convention. It was originally a char*
}
ENTRY;
vector<ENTRY> entries;
fstream data;
Here is my display data function
void DisplayData()
{
std::cout << (int)(entries.size() / 5) <<" entries" << endl;
for(int i = 1; i <=(int)entries.size()/5; i++)
{
cout << endl << "Entry " << i << ":" << endl
<< "Name: " << entries[i].pcName << endl
<< "Username: " << entries[i].pcUsername << endl
<< "Password: " << entries[i].pcPassword << endl
<< "Message: " << entries[i].pcMessage << endl
<< "Advertisement: " << entries[i].pcAdvertisement << endl;
}
}
and here is my Load Data function
bool LoadData(const char* filepath)
{
std::string lineData ;
int linenumber = 1 ;
data.open(filepath, ios::in);
ENTRY entry_temp;
if(!data.is_open())
{
cerr << "Error loading file" << endl;
return false;
}
while(getline(data, lineData))
{
if(linenumber==1) {entry_temp.pcName = lineData;}
else if(linenumber==2) {entry_temp.pcUsername = lineData;}
else if(linenumber==3) {entry_temp.pcPassword = lineData;}
else if(linenumber==4) {entry_temp.pcMessage = lineData;}
else if(linenumber==5) {entry_temp.pcAdvertisement = lineData;}
entries.push_back(entry_temp);
if(linenumber == 5)
{
linenumber = 0;
}
linenumber++;
}
data.close();
puts("Database Loaded");
return true;
}
Here is the text file I am loading:
Name1
Username1
Password1
Message1
Ad1
And here is the result of the display data function after calling load data:
1 entries
Entry 1:
Name: Name1
Username Username1
Password:
Message:
Advertisement:
As you can see, the first two load but the last three don't. When I did this with an array instead of a vector, it worked fine so I don't know what I'm doing wrong. Thanks.
I suggest that you read each line directly into the data field where it goes:
getline(data, entry_temp.pcName);
getline(data, entry_temp.pcUsername);
getline(data, entry_temp.pcPassword);
getline(data, entry_temp.pcMessage);
getline(data, entry_temp.pcAdvertisement);
entries.push_back(entry_temp);
This makes your intent much clearer than your current while loop. It also creates a single entry for all 4 input lines rather than one for each input line (with the other three blank). Now you can read several "entries" by using a while loop that checks if you have reached the end of the file.
Doing this will also make printing out the data much easier since the vector will have exactly the number of entries rather than five times as many as you expect (which also eats up a lot more memory than you need to).
Your DisplayData function is a little weird, and so is your LoadData.
Your LoadData pushes back a new copy of the current ENTRIES entry with every line. Your DisplayData starts at 1 (which is not the beginning of any vector or array), and iterates only up to the 1/5th entry of the entire vector.
This needs a heavy rework.
First, the size() member of any standard container returns the number of elements that it contains, and will not take the number of fields in a contained struct into account.
For future reference, you'll want to post your question in a complete, standalone example that we can immediately compile to help. (see http://sscce.org/)
Try this modified data, which runs correctly, and see if you can tell what is being done differently:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
typedef struct
{
string pcName, pcUsername, pcPassword, pcMessage, pcAdvertisement;
}
ENTRY;
vector<ENTRY> entries;
fstream data;
bool LoadData(const char* filepath)
{
std::string lineData ;
int linenumber = 1 ;
data.open(filepath, ios::in);
ENTRY entry_temp;
if(!data.is_open())
{
cerr << "Error loading file" << endl;
return false;
}
while(getline(data, lineData))
{
if(linenumber==1) {entry_temp.pcName = lineData;}
else if(linenumber==2) {entry_temp.pcUsername = lineData;}
else if(linenumber==3) {entry_temp.pcPassword = lineData;}
else if(linenumber==4) {entry_temp.pcMessage = lineData;}
else if(linenumber==5) {entry_temp.pcAdvertisement = lineData;}
if(linenumber == 5)
{
entries.push_back(entry_temp);
linenumber = 0;
}
linenumber++;
}
data.close();
puts("Database Loaded");
return true;
}
void DisplayData()
{
std::cout << entries.size() <<" entries" << endl;
for(int i = 0; i < entries.size(); i++)
{
cout << endl << "Entry " << i << ":" << endl
<< "Name: " << entries[i].pcName << endl
<< "Username: " << entries[i].pcUsername << endl
<< "Password: " << entries[i].pcPassword << endl
<< "Message: " << entries[i].pcMessage << endl
<< "Advertisement: " << entries[i].pcAdvertisement << endl;
}
}
int main()
{
LoadData("/tmp/testdata");
DisplayData();
return (0);
}
While I think #code-guru has the right idea, I'd take the same idea just a little further, and make your code work a little more closely with the standard library. I'd do that by reading a data item with a stream extractor, and displaying it with stream inserter. So, the extractor would look something like this:
std::istream &operator>>(std::istream &is, ENTRY &e) {
getline(is, e.pcName);
getline(is, e.pcUsername);
getline(is, e.pcPassword);
getline(is, e.pcMessage);
getline(is, e.pcAdvertisement);
return is;
}
..and the inserter would look something like this:
std::ostream &operator<<(std::ostream &os, ENTRY const &e) {
os << e.pcName << "\n";
os << e.pcUsername << "\n";
os << e.pcPassword << "\n";
os << e.pcMessage << "\n";
os << e.pcAdvertisement << "\n";
return os;
}
With those in place, loading and displaying the data becomes fairly straightforward.
Load the data:
std::ifstream in("yourfile.txt");
std::vector<ENTRY> data((std::istream_iterator<ENTRY>(in)),
std::istream_iterator<ENTRY>());
Display the data:
for (auto const & e: data)
std::cout << e << "\n";
For the moment, I haven't tried to duplicate the format you were using to display the data -- presumably the modifications for that should be fairly obvious.

How can I add something to the middle of a vector without losing data?

I'm trying to add a string to the middle of a vector, but I don't want to lose the data that is being replaced. I want everything below that element to shift down one. Is that possible?
Here is what I have so far
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("Rich");
cout << v.back() << endl;
v.push_back("Debbie");
cout << v.back() << endl;
v.push_back("Robin");
cout << v.back() << endl;
v.push_back("Dustin");
cout << v.back() << endl;
v.push_back("Philip");
cout << v.back() << endl;
v.push_back("Jane");
cout << v.back() << endl;
v.push_back("Joseph");
cout << v.back() << endl;
cout << "Removing Joseph from the vector"<<endl;
v.pop_back();
cout << "Adding my name to the vector" << endl;
vector<string>::iterator pos = v.find(v.begin(),v.end(), "Robin");
if (pos != v.end())
{
++pos;
}
v.insert(pos, "Jimmy");
cout << "The vector now contains the names:";
for (unsigned i=0; i<v.size(); i++)
cout << " " << "\n" << v.at(i);
cout << "\n";
return 0;
}
I'm also getting an error on this find function. Any help would be appreciated.
Error 1 error C2039: 'find' : is not a member of 'std::vector<_Ty>' d:\pf3\lab3b\lab3b\3b.cpp 28
2 IntelliSense: class "std::vector<std::string, std::allocator<std::string>>" has no member "find" d:\pf3\lab3b\lab3b\3b.cpp 28
Like this:
#include <vector> // for std::vector
#include <algorithm> // for std::find
v.insert(std::find(v.begin(), v.end(), "Robin"), "Jimmy");
std::vector has no find function, use std::find instead:
vector<string>::iterator pos = std::find(v.begin(),v.end(), "Robin");
this operation is O(N) in vector. if you will use it frequently please use linked list.

JsonCpp: Undefined reference to `Json::Value::operator[](int) const'

i'm trying to use JsonCpp with this sample code
string json_example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
// Let's parse it
Json::Value root;
Json::Reader reader;
//Parsing of something. In example do parsing of json_example string.
bool parsedSuccess = reader.parse(json_example, root, false);
if (!parsedSuccess) {
// report to the user the failure and their locations in the document.
cout << "Failed to parse JSON" << endl << reader.getFormatedErrorMessages() << endl;
return 1;
}
// Let's extract the array that is contained inside the root object
const Json::Value array = root["array"];
// And print its values
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
// Lets extract the not array element contained in the root object and print its value
cout << "Not an array: " << root["not an array"] << endl;
// If we want to print JSON is as easy as doing:
cout << "Json Example pretty print: " << endl << root.toStyledString() << endl;
return 0;
but i've received this error on this "..array[index].asString..."
Undefined reference to `Json::Value::operator[](int) const'
can anyone help me?!tnx so much
Tnx for your answer...
So the solution is changing this
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
with
for (unsigned int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
bye
I think you didn't included jsoncpp.cpp file, which describes your json.h library file. U have to generate it using Pythot with command python amalgamate.py and then include it into your project