Controlling cout in a for loop - c++11

Here is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream os; // Initialize stringstream "os"
string mValue = "month"; // Initialize mValue "month"
int iValue = 1; // Initialize iValue "1"
for(int iValue = 1; iValue < 13; ++iValue) // Iteration 1: 1 < 12 so execute the following:
{
os << mValue << "" << iValue; // Glue mValue and iValue together
cout << os.str() << endl; // Print glued mValue and iValue
}
return 0;
}
This results in the following output:
month1
month2month2
month3month3month3
month4month4month4month4
month5month5month5month5month5
month6month6month6month6month6month6
month7month7month7month7month7month7month7
month8month8month8month8month8month8month8month8
month9month9month9month9month9month9month9month9month9
month10month10month10month10month10month10month10month10month10
month11month11month11month11month11month11month11month11month11month11
month12month12month12month12month12month12month12month12month12month12month12
The desired output is:
month1
month2
month3
month4
month5
month6
month7
month8
month9
month10
month11
month12
Being a noob at coding, I understand why this is happening but I don't know how to fix it. I tried to place cout outside of the for loop but that results in
month1month2month3month4month5month6month7month8month9month10month11month12
I'm out of ideas and I hope you can tell me how to get this right.

you need to add next line escape sequence to your os value in for loop after placing the cout outside the loop.
os << mValue << "" << iValue << "\n";

Related

I initialize char *str1 = nullptr and output to the screen... But when i want output to other variable i receive empty screen.. Why is it happening?

#include <iostream>
#include <cstring>
int main()
{
const char *str = "Hello world";
char *str1 = nullptr;
std::cout << str << std::endl; // when i output to screan i see hello world
std::cout << str1 << std::endl; // when i output to scream i sen nothing
str1 = new char[strlen(str) + 1];
strcpy(str1, str);
std::cout << str << std::endl; // when i output to scream again i sen nothing
std::cout << str1 << std::endl; // The same
}

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

OpenCV perspectiveTransform broken function

Im trying to use perspectiveTransform but I keep getting error. I tried to follow the solution from this thread http://answers.opencv.org/question/18252/opencv-assertion-failed-for-perspective-transform/
_players[i].getCoordinates() is of type Point
_homography_matrix is a 3 x 3 Mat
Mat temp_Mat = Mat::zeros(2, 1, CV_32FC2);
for (int i = 0; i < _players.size(); i++)
{
cout << Mat(_players[i].get_Coordinates()) << endl;
perspectiveTransform(Mat(_players[i].get_Coordinates()), temp_Mat, _homography_matrix);
}
Also, how do I convert temp_Mat into type Point ?
OpenCV Error: Assertion failed (scn + 1 == m.cols) in cv::perspectiveTransform
Basically you just need to correct from
Mat(_players[i].get_Coordinates()) ...
to
Mat2f(_players[i].get_Coordinates()) ...
In the first case you are creating a 2x1, 1 channel float matrix, in the second case (correct) you create a 1x1, 2 channel float matrix.
You also don't need to initialize temp_Mat.
You can also use template Mat_ to better control the types of your Mats. E.g. creating a Mat of type CV_32FC2 is equivalent to create a Mat2f.
This sample code will show you also how to convert back and forth between Mat and Point:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
// Some random points
vector<Point2f> pts = {Point2f(1,2), Point2f(5,10)};
// Some random transform matrix
Mat1f m(3,3, float(0.1));
for (int i = 0; i < pts.size(); ++i)
{
cout << "Point: " << pts[i] << endl;
Mat2f dst;
perspectiveTransform(Mat2f(pts[i]), dst, m);
cout << "Dst mat: " << dst << endl;
Point2f p(dst(0));
cout << "Dst point: " << p << endl;
}
return 0;
}

searching a string for particular words

i want to create like a very basic AI for a console game. i need a function that will recognize 2 or 3 words within a string created via cin. any help would be greatly appreciated as iv been at this for a while now and cant seem to find a solution.
#include <string>
#include <iostream>
using namespace std;
//-------------------------------------------globals
string sentence;
int main()
{
getline(cin, sentence);
string search;
size_t pos;
search = "hi";
pos = sentence.find(search);
if (pos != string::npos)
cout << "hello sir " << endl;
else
search = "cya";
pos = sentence.find("cya");
if (pos != std::string::npos)
std::cout << "goodbye sir " << endl;
else
search = "goodbye";
pos = sentence.find("goodbye");
if (pos != std::string::npos)
std::cout << "shuting down sir " << endl;
else
return 0;
}
what i would like is to have the initial search look for other words such as hello hey, and so forth, and the similer thing with the others

Resources