Hi all I'm having compilation error when I'm using vector iterator in a function itself.
i've tried executing the example from http://ideone.com/tTDYU5 and it works perfectly fine. However when I try to put it in a function things gets ugly why is this so?
vector <PointTwoD> topfive;
void MissionPlan::topfives()
{
topfive.assign( point1.begin(), point1.end() );
sort(topfive.begin(), topfive.end(), sortByCiv);
}
void MissionPlan::DisplayTopFiveResult()
{
missionplan.topfives();
vector<PointTwoD>::iterator it = topfive.begin();
for (int i = 0; i < 5 && it != topfive.end(); i++) {
cout << "X axis: " << pointtwoD.xcord << " " << "Y axis: " << pointtwoD.ycord << " " << "CIV Index: " << pointtwoD.civIndex << *it;
++it;
}
}
Since "the value are all in the class PointTwoD "
Use it to print those :
vector<PointTwoD>::iterator it = topfive.begin();
for (int i = 0; i < 5 && it != topfive.end(); i++) {
cout << "X axis: " << it->xcord << " "
<< "Y axis: " << it->ycord << " "
<< "CIV Index: " << it->civIndex << std::endl;
++it;
}
If still this throws compilation error, you need to provide more info.
Related
I'm a little confused as to how to go around this. I have some array variables with some information, and I want to print them out after some calculations. If the value is 0, then I want to print a " " instead. There are 3 arrays that need to get checked however, how would I change the output statement to cater for all 3 checks and print an empty string instead of the value?
for(int start = 1; start < 13; start++)
{
if(check[start] == 1)
{
cout << checkMonth(start) << ": " << setprecision(1) << fixed << averagespeed[start] << "(" << setprecision(1) << fixed << sdSpeed[start] << ")," << setprecision(1) << fixed << averagetemp[start] << "(" << setprecision(1) << fixed << sdTemp[start] << ")," << setprecision(1) << fixed << Solar[start] << '\n';
}
/*if(sumTemp[start] == 0 || sumTemp[start] == 0 || sumSpeed[start] == 0){
}*/
}
Example Output looks like this:
January,5.5(1.2),25.5(12.2),196.4
For example if Sum of Speed is 0, that means all values of speed were 0 or null. So it should change to this:
January,,25.5(12.2),196.4
A single line to std::cout doesn't need to be done in one statement. For example:
std::cout << "First";
std::cout << ", second"
std::cout << ", third\n"
Prints the following line:
First, second, third
Now we can use an if to conditionally print the middle part of the string:
std::cout << a;
if (b != 0) {
std::cout << ", " << b;
}
std::cout << ", " << c << '\n';
I am trying to display struct members of a list in OMNeT++, all members are displayed correctly unless the member which is of type const char*. I am confused because after three push_back in the list, when I display. All the members of the last pushed element is displayed correctly even the one of type const char*. But for the two first pushed elements, the member of type cont char* display nothing, garbage or "DETAIL (Ipv4)Drones.host[3].ipv4.ip".
Ipv4Address srcAddress = recMtlsd->getSrcAddress();
Ipv4Address dstAddress = recMtlsd->getDstAddress();
const char* position = recMtlsd->getPosition();
simtime_t time = recMtlsd->getTime();
int srcID = recMtlsd->getId();
EV_DEBUG << "Source : " << srcAddress << endl;
EV_DEBUG << "Destination : " << dstAddress << endl;
EV_DEBUG << "Position : " << position << endl;
EV_DEBUG << "Time : " << time << endl;
EV_DEBUG << "Source ID: " << srcID << endl;
// All precedent displays are working correctly
/*typedef struct Mtlsd{
Ipv4Address originatorAddr, destinationAddr;
const char *position;
int originatorId;
simtime_t time;
}MTLSD;*/
MTLSD recitem;
recitem.originatorAddr = srcAddress;
recitem.originatorId = srcID;
recitem.destinationAddr = dstAddress;
recitem.position = position;
recitem.time = time;
EV_DEBUG << "Source : " << recitem.originatorAddr << endl;
EV_DEBUG << "Destination : " << recitem.dstinationAddr << endl;
EV_DEBUG << "Position : " << recitem.position << endl;
EV_DEBUG << "Time : " << recitem.time << endl;
EV_DEBUG << "Source ID: " << recitem.srcID << endl;
// All precedent displays are working correctly
/*typedef struct Mtlsd_data{
list<MTLSD> q;
int ID;
}MTLSD_DATA;*/
list<MTLSD_DATA> mtlsd_file;
auto node = find_if(mtlsd_file.begin(), mtlsd_file.end(), [=] (MTLSD_DATA const& i){return (i.ID == srcID);});
bool found = (node != mtlsd_file.end());
if (!found)
{
MTLSD_DATA recdata;
recdata.ID = srcID;
recdata.q.push_back(recitem);
mtlsd_file.push_back(recdata);
EV_DEBUG << "For node " << srcID ;
for(auto claim=mtlsd_file.back().q.begin(); claim!=mtlsd_file.back().q.end();++claim)
{
EV_DEBUG << "(" << string(claim->position) << ", " << claim->time << ");" << endl;
}
// The precedent display works correctly
}
else
{
EV_DEBUG << "I already have data about the node " << node->ID << endl;
if (node->q.size() == 3)
{
EV_DEBUG << "I already have three time-location claim in the queue" << endl;
EV_DEBUG << "Here they are: ";
EV_DEBUG << "For node " << (*node).ID << endl;
for(auto fileclaim=(*node).q.begin(); fileclaim!=(*node).q.end();++fileclaim)
EV_DEBUG << "(" << string((*fileclaim).position) << ", " << (*fileclaim).time << ");" << endl;
EV_DEBUG << "I will delete the old one (" << node->q.front().position << ", " << node->q.front().time << ")" << endl;
node->q.pop_front();
}
node->q.push_back(recitem);
EV_DEBUG << "I have pushed this new one : (" << string(node->q.back().position) << ", " << node->q.back().time << ")" << endl;
}
EV_DEBUG << "Here they are all time-location claims in the queue : ";
for(auto fileclaims=node->q.begin(); fileclaims!=node->q.end();++fileclaims)
{
EV_DEBUG << "(" << string(fileclaims->position) << ", " << fileclaims->time << ");" << endl;
}
// The last element is displayed correctly, but those before not.
.
.
.
I have just more or less finished my first C++ Project, it is a Hangman Game and so far everything works fine. The only Problem is that i need to have spaces between the underlines (_) that represent the hidden word. If anyone could help me on this i would really appreciate it.
// UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS
#include "tests.h"
#include <iostream>
#include <string>
#include "hangman.h"
int main(){
using namespace std;
// display the hidden word
std::string word_to_guess = chooseWord();
int misses = 0;
std::string displayed_word = word_to_guess;
for(int i=0; i< displayed_word.length(); i++)
displayed_word[i] = '_';
int attempts = 6;
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for correct letter
while(1){
std::cout << "Your guess";
std::cout << ":";
char guess;
std::cin >> guess;
bool Correct = false;
for(int i=0; i< word_to_guess.length(); i++)
if (guess == word_to_guess[i]) {
displayed_word[i] = word_to_guess[i];
Correct = true;
}
if (!Correct)
attempts--;
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for win or lose
if (attempts==0)
std::cout << "The word was: " << word_to_guess << std::endl << "You lost!";
if (attempts==0)
return 0;
if (!word_to_guess.find(displayed_word))
std::cout << "You won!";
if (!word_to_guess.find(displayed_word))
return 0;
}
}
First, you can simplify this
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
Now, about your question, I think a best solution is replacing
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
if(i == 0 || displayed_word[i] == '_')
std::cout << " ";
std::cout << displayed_word[i];
if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
std::cout << " ";
}
std::cout << "]" << std::endl;
Explaination:
We put spaces at the beginning and the end, and also around underscores, but we make sure to put only one space between two underscores.
main.cpp
void torsoOPCB(Fl_Widget *w, void* p) {
for(std::size_t i=0; i < torso.size(); i++) {
cout << "Name: " << torso[i].GetName() << endl;
cout << "Part Number: " << torso[i].GetPartNumber() << endl << endl;
} // This loop is to check if the inputs are in the vector torso
dialog = new Fl_Window(340, 300, "Robot Part");
Fl_Multiline_Output* output = new Fl_Multiline_Output(100, 10, 400, 200, "Torso list:");
for(std::size_t i = 0; i < torso.size(); i++) {
output->value(torso[i].print().c_str());
}
dialog->end();
dialog->set_non_modal();
dialog->show();
}
I'm learning how to use FLTK in C++ and I'm not sure why it keeps showing me the latest user's inputs. For example, if I entered the inputs for torso[0] and torso[1], the output will only show torso[1] which is the latest input. The inputs are stored correctly I think but I'm not sure why it won't show both torso[0] and torso[1].
Here is my print fucntion
Torso.cpp
std::string Torso::print()
{
ostringstream of;
of << "Part name: " << GetName()
<< endl << "Part #: " << GetPartNumber()
<< endl << "Weight: " << GetWeight()
<< endl << "Cost: " << GetCost()
<< endl << "Battery Comp: " << GetDescription()
<< endl << "Description: " << GetBatteryCompartmentSize() << endl;
return of.str();
}
I'm using FLTK version 1.3.4. Thank you in advanced
You are only showing the very last item in
for(std::size_t i = 0; i < torso.size(); i++) {
output->value(torso[i].print().c_str());
}
The output value is always being overwritten. If you wish to show all the output, gather it up first before putting it into the widget
std::ostringstream oss;
for(std::size_t i = 0; i < torso.size(); i++) {
oss << torso[i].print() << "\n";
}
output->value(oss.str().c_str());
If you don't mind multiple outputs on the same line, change the "\n to ". "
I'm actually trying to implement the floyd's warshall algorithm but I met a hard-point. I got a segmentation fault while i'm trying to find the shortest-way in my matrix of sequence. I follow this tutorial:
floyd's warshall algorithm
Everything work well except at the line 124:
chemin.insert(it,sequenceTest[v1-1][temp-1]);
where i get the segfault (You can skip the first part except if you want to know how I implement the algorithm butt the problem is in the second part):
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int i,j,k;
int sequenceTest [4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int tempSequenceTest [4][4];
int distanceTest [4][4] = {{0,2,4,100000},{2,0,1,5},{4,1,0,3},{100000,5,3,0}};
int tempDistanceTest[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<sequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << distanceTest[i][j] << " ";
}
cout<< endl;
}
cout<< endl;
for(k=0;k<3;k++)
{
cout <<endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << "i: " << i << " j: " << j << endl;
if(i == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
if(j == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
cout<< endl;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
//cout<< "i: " << i << " j: " << j << " k:" << k << endl;
if(i!=k && j!=k)
{
if(distanceTest[i][j]> distanceTest[i][k] + distanceTest[k][j])
{
tempDistanceTest[i][j] = distanceTest[i][k] + distanceTest[k][j];
cout << distanceTest[i][j] << " > " << distanceTest[i][k] << " + " << distanceTest[k][j] << " = " << tempDistanceTest[i][j] << " " << i << j << endl;
tempSequenceTest[i][j] = k+1;
}
else
{
tempDistanceTest[i][j] = distanceTest[i][j];
tempSequenceTest[i][j] = sequenceTest[i][j];
}
}
}
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
distanceTest[i][j] = tempDistanceTest[i][j];
sequenceTest[i][j] = tempSequenceTest[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<tempSequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << tempDistanceTest[i][j] << " ";
}
cout<< endl;
}
}
int v1, v2;
v1 = 1;
v2 = 4;
vector <int> chemin;
vector <int>::iterator it;
chemin.push_back(v1);
chemin.push_back(v2);
int temp = v2;
cout << sequenceTest[0][2];
while(sequenceTest[v1-1][temp-1] != v2)
{
it = chemin.begin() + 1;
cout << "V1: " << v1 << " V2: " << v2 << " Temp: " << temp << endl;
chemin.insert(it,sequenceTest[v1-1][temp-1]);
for(i=0;i<chemin.size();i++)
{
cout << chemin[i];
}
cout << endl;
temp = sequenceTest[v1-1][temp-1];
}
}
Thanks for your attention and for taking the time to help me!
chemin.insert(it,sequenceTest[v1-1][temp-1]);
This invalidates the iterator it, but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value.
it = chemin.insert(it,sequenceTest[v1-1][temp-1]);