Xcode issue with C++ - xcode

So I've recently started using XCode and have an issue with trying to compile C++ code: Even a basic program like
#include <iostream>
int main()
{
int x = 0;
std::cout << "Enter a value for X: " << std::endl;
std::cin >> x;
std::cout << "X is " + x << std::endl;
return 0;
}
Gives me no output (under "All Output" I get):
Enter a value for X:
5

Related

Why conditional breakpoints don't work in VScode with the hit count condition?

I put a conditional breakpoint in this code on the line std::cout << i << ", "; :
(I use gdb)
#include <iostream>
int main() {
for (int i = 0; i < 10; ++i) {
std::cout << i << ", ";
}
std::cout << std::endl;
}
I set the hit count on 4. But I see VScode break on the breakpoint the very first time it gets to the breakpoint.
Anyone knows how to fix this ?
Oddly enough, if I enter the expression i == 4 it works tho.
Thx!

How to use OpenMP to deal with two for loops with

I am new to OpenMP... Please help me with this dumb question. Thank you :)
Basically, I want to use OpenMP to speed up two for loops. But I do not know why it keeps saying: invalid controlling predicate for the for loop.
By the way, my GCC version is gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and OS I am using is Ubuntu 16.10.
Basically, I generate a toy data that has a typical Key-Value style, like this:
Data = {
"0": ["100","99","98","97",..."1"];
"1": ["100","99","98","97",..."1"];
...
"999":["100","99","98","97",..."1"];
}
Then, for each key, I want to compare its value with the rest of the keys. Here, I sum them up through "user1_list.size()+user2_list.size();". As for each key, the sum-up process is totally independent of other keys, which means this works for parallelism.
Here is my toy example code.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include "omp.h"
using namespace std;
int main(){
// Create Data
map<string, vector<string>> data;
for(int i=0; i != 1000; i++){
vector<string> list;
for (int j=100; j!=0; j--){
list.push_back(to_string(j));
}
data[to_string(i)]=list;
}
cout << "Data Total size: " << data.size() << endl;
int count = 1;
#pragma omp parallel for private(count)
for (auto it=data.begin(); it!=data.end(); it++){
//cout << "Evoke Thread: " << omp_get_thread_num();
cout << " count: " << count << " / " << data.size() << endl;
count ++;
string user1 = it->first;
vector<string> user1_list = it->second;
for (auto it2=data.begin(); it2!=data.end(); it2++){
string user2 = it2->first;
vector<string> user2_list = it2->second;
cout << "u1:" << user1 << " u2:" << user2;
int total_size = user1_list.size()+user2_list.size();
cout << " total size: " << total_size << endl;
}
}
return 0;
}

C ++ , My for loop doesn't work when I run it on the terminal. Any ideas?

When I run it on the terminal it works fine but the loop. The for loop just doesn't do anything at all. I'm learning C++, so I don't know much.
#include <iostream>
#include <cstring>
using namespace std;
int main( int argc, char *argv[] ) {
if (argc == 2) {
cout << "The first argument is " << argv[0] << endl;
cout << "The second argument is " << argv[1] << endl;
} else if (argc > 2) {
cout << "Too many arguments" << endl;
exit(0);
} else {
cout << "Only one argument" << endl;
cout << "The argument is " << argv[0] << endl;
exit(0);
}
if (atoi(argv[1]) < 0) {
cout << "Error negative number" << endl;
exit(0);
}
// this loop does not work, everything else does.
for (int i = 1; i >= atoi(argv[1]); i++){
int count = atoi(argv[1]--);
cout << count << endl;
int sum = sum + i;
}
cout << "The sum is: " << endl;
return(0);}
I think that could be the if statements what are messing around with the loop.
I think you made mistake in the for loop.
You show use "<=" instead of ">=" in the for loop.
Hope this might helps you.
I guess your code is not reaching the for loop as you have exit() conditions on each and every condition of if. Your code only reaches the loop if you are passing 2 arguments in the terminal while you are running your code

Strange behaviour of for_each and push_back()

I was doing some testing with for_each and the use of lambda functions and I'm stuck on this (compiled with g++ -std=c++11, gcc version 5.3.1)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vi = {1,1,1,1};
int end =0;
cout << "vi contains: ";
for_each(vi.begin(), vi.end(),[](int i){
cout << i << " ";
});
cout << endl;
for_each(vi.begin(),vi.end(),[&](int i){
cout << "i="<<i<<" ";
if(i==1){
vi.push_back(1);
end++;
}
});
cout << endl;
cout << "end=" << end << endl;
cout << "now vi contains: ";
for_each(vi.begin(), vi.end(),[](int i){
cout << i << " ";
});
cout << endl;
return 0;
}
and this is the output of this code
vi contains: 1 1 1 1
i=1 **i=0** i=1 i=1
end=3
now vi contains: 1 1 1 1 1 1 1
why is, at the first iteration of the loop, i equal to 0?

Apple Mach-O Linker (Id) Errors

Xcode gives me 3 "Apple Mach-O Linker (Id) Error" errors. But, when I click them it doesn't direct me to a line in my code, so I don't know what/where the problem is. I know others have asked this question, but all of the solutions that I could find were specific to each individual's code. I am learning C++, so these errors are coming as part of a beginner program I'm working on.
Apple Mach-O Linker (Id) Error
"SensorNode::SensorNode(char*, float, float, float, int, float)", referenced from:
Apple Mach-O Linker (Id) Error
"LOCATION::LOCATION()", referenced from:
Apple Mach-O Linker (Id) Error
Linker command failed with exit code 1 (use -v to see invocation)
If it helps, I'll put my code here:
here's my "sensor_node.h"
#ifndef SENSORNODE_H
#define SENSORNODE_H
#include <iostream>
class LOCATION {
float lat, longi, height;
public:
LOCATION ();
void setx(float xx);
void sety(float yy);
void setz(float zz);
void print();
};
class SensorNode {
char* NodeName;
int NodeID;
LOCATION Node1;
float batt;
int func;
public:
SensorNode(char *n, float x, float y, float z, int i, float ah);
void print();
void setOK(int o);
int getOK();
void setLOC(float longi, float lat, float h);
};
#endif /* defined(__Project_3__sensor_node__) */
here's my sensor_node.cpp:
#include "sensor_node.h"
//#include <iostream>
using namespace std;
void LOCATION::setx(float xx) {
lat = xx;
if (lat > 180.0 || lat < -180.0) {
cout << "Latitude is not in the -180 to 180 degree range";
lat = 0.0;
}
}
void LOCATION::sety(float yy) {
longi = yy;
if (longi > 180.0 || longi < -180.0) {
cout << "Latitude is not in the -180 to 180 degree range";
longi = 0.0;
}
}
void LOCATION::setz(float zz) {
height = zz;
}
void LOCATION::print() {
cout << "(LONGITUDE: " << longi << " ,LATITUDE: " << lat << " ,HEIGHT: " << height << " )";
}
and here's my main.cpp:
#include <iostream>
using namespace std;
#include "sensor_node.h"
int main() {
LOCATION a; SensorNode s1("Pulse",15.9,-30.1,0,157,2.0);
cout << "Beginning LOCATION tests.\n\n";
cout << " After initial construction: ";
a.print();
cout << "\n";
a.setx(-45.3);
a.sety(27.6);
a.setz(3.5);
cout << " After setting x/y/z to -45.3/27.6/3.5: ";
a.print();
cout << "\n";
cout << " After attempting to set longitude to 180.1: ";
a.setx(180.1);
a.print();
cout << "\n";
cout << " After attempting to set longitude to -180.1: ";
a.setx(-180.1);
a.print();
cout << "\n";
cout << " After attempting to set latitude to 180.1: ";
a.sety(180.1);
a.print();
cout << "\n";
cout << " After attempting to set latitude to -180.1: ";
a.sety(-180.1);
a.print();
cout << "\n";
/*
cout << "\n\n\n\nBeginning sensor node tests.\n\n";
cout << " After initial construction:";
s1.print();
cout << "\n Printing the value returned by getOK: " << s1.getOK();
cout << "\n After changing location to 20/30/40:";
s1.setLOC(20,30,40);
s1.print();
cout << "\n After trying to set location illegally:";
s1.setLOC(181, -181, 10);
s1.print();
cout << "\n Node fails, then try to change location:";
s1.setOK(0);
s1.setLOC(5,10,15);
s1.print();
cout << "\n Printing the value returned by getOK: " << s1.getOK();
cout << "\n\n\n End of tests.\n";
cout << "Enter an integer to quit: ";
cin >> hold;
*/
return 0;
}
You haven't written the constructor for the LOCATION class. You declare a LOCATION named a and a SensorNode which contains a LOCATION, but the linker can't figure out where the code for the LOCATION constructor is, so it can't link. Write a constructor for the LOCATION class and you should be good.
You seem to have forgotten to implement SensorNode. In SensorNode.h you declare a class, SensorNode which has data and public methods, but in SensorNode.cpp you are not providing an implementation of SensorNode (constructor), print etc. The linker is unable to find implementations of these since they haven't been implemented, hence the linker error.
Here's some boilerplate you can start out with:
SensorNode::SensorNode(char *n, float x, float y, float z, int i, float ah)
{
}
void SensorNode::print()
{
}
void SensorNode::setOK(int o)
{
}
int SensorNode::getOK()
{
}
void SensorNode::setLOC(float longi, float lat, float h)
{
}

Resources