Is there any quick way to change a windows wallpaper in C++? I'm trying to animate a wallpaper and am going through a directory of images and setting the wallpaper to an image every few milliseconds, problem is, even when running on a thread, there is sometimes a time gap of a few seconds between wallpaper changes? How do I fix this? Am I doing something wrong? Here's my code:
#include <windows.h>
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
void thinggg()
{
cout << "Directory(0) or Files(1)?\n";
int choice;
cin >> choice;
int num = 0;
string* images;
if (choice == 1)
{
cout << "How many images?\n";
cin >> num;
cin.ignore();
images = new string[num];
for (int i = 0; i < num; i++)
{
string loc;
cout << i + 1 << ": ";
getline(cin, loc);
images[i] = loc;
}
}
else
{
cin.ignore();
string loc;
cout << "Enter folder location: ";
getline(cin, loc);
// there is probably a much better to do this
for (const auto& entry : fs::directory_iterator(loc))
{
num++;
}
images = new string[num];
num = 0;
for (const auto& entry : fs::directory_iterator(loc))
{
string path = entry.path().string();
if (path.find("png") != string::npos || path.find("jpg") != string::npos || path.find("bmp") != string::npos)
{
images[num] = path;
}
num++;
}
}
int intervals;
cout << "Enter FPS: ";
cin >> intervals;
cin.ignore();
intervals = 1000 / intervals;
int i = 0;
while (true)
{
wstring loc = wstring(images[i].begin(), images[i].end());
const wchar_t* a = loc.c_str();
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)a, SPIF_SENDCHANGE);
i++;
if (i == num) i = 0;
this_thread::sleep_for(chrono::milliseconds(intervals));
}
}
int main()
{
thread t(thinggg);
t.join();
cout << "quit?" << endl;
int a;
cin >> a;
if (a == 1)
{
t.native_handle();
return 0;
}
}
Related
For some reason my boolean is not checking false, can anyone help me figure out why? Also how would I go about making this program ignore spaces if it were a phrase (i.e. "never odd or even")
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string input;
int i = 0;
bool flag = true;
int size = input.size();
cin >> input;
for (i = 0; i < size; i++)
{
if (input.at(i) != input.at(size - i - 1))
{
flag = false;
}
}
if (flag = true)
{
cout << "palindrome: " << input << endl;
}
else
{
cout << "not a palindrome: " << input << endl;
}
return 0;
}
New answer
You are getting the size of the string before the user has entered the string.
Change
int size = input.size();
cin >> input;
to
cin >> input:
int size = input.size();
Previous answer
You are using a single = rather than two in the if statement.
Change
if (flag = true)
to
if (flag == true)
So I have built a small basic data encrypter (for learning purposes only). It is working perfectly fine but it reads only a single line of input. Is it my Editor problem or my code have some issues.
ps: I use CodeBlocks
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
std::string str;
char enc;
int word;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
cout << "Enter a Word: ";
getline(cin, str);
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(int i = 0; i < str.length(); i++){
int randomAdd[5] = {5,6,2,3,2};
int size = sizeof(randomAdd)/sizeof(randomAdd[0]);
// for(int j = 0; j < 5; j++){
word = str.at(i);
if(i%5 == 0){
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
return 0;
}
This works
Hello World
But I cannot enter this
Hello World
Have a nice day
because then the program exits command prompt without any error or message.
How can I read more than one line?
You can do as
#include <iostream>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
cout << str << endl;
}
return 0;
}
This code sample allows you to input multiple lines interactively from the command line/shell
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
char enc;
int word;
vector<string> myInput;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
while (str != "Enigma")
{
cout << "Enter a line (Write Enigma to exit input): ";
getline(cin, str);
myInput.push_back(str);
}
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(auto & myInputLine : myInput)
{
str = myInputLine;
for (size_t i = 0; i < str.length(); i++) {
int randomAdd[5] = { 5,6,2,3,2 };
int size = sizeof(randomAdd) / sizeof(randomAdd[0]);
word = str.at(i);
if (i % 5 == 0) {
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
}
return 0;
}
The input is finished if Enigma is written.
All input is stored in the vector container of the STL, see vector.
Afterwards, all the lines are encrypted by your algorithm.
Hope it helps?
I am relatively new to C++ and have some experience in Java with classes and functions but now very much, so this program is giving me some issues. Below is the code I have, everything seems right now to me and even though I have set "num" to 0, it always prints out "-858993460".
Here are my header files:
#include <string>
using namespace std;
class romanType
{
public:
void setRoman(string n);
void romanToPositiveInteger();
void printPositiveInteger() const;
romanType();
romanType(string n);
void printNum();
private:
string romanString;
int num;
};
Here is my implementation file:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int value(char num) {
if (num == 'I')
return 1;
if (num == 'V')
return 5;
if (num == 'X')
return 10;
if (num == 'L')
return 50;
if (num == 'C')
return 100;
if (num == 'D')
return 500;
if (num == 'M')
return 1000;
return -1;
}
void romanType::setRoman(string n) {
romanString = n;
}
void romanType::romanToPositiveInteger() {
num = 0;
for (int i = 0; i < romanString.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(romanString[i]);
if (i + 1 < romanString.length())
{
// Getting value of symbol s[i+1]
int s2 = value(romanString[i + 1]);
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
num = num + s1;
}
else
{
num = num + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
num = num + s1;
i++;
}
}
}
void romanType::printPositiveInteger() const {
cout << num << endl;
}
romanType::romanType(string n) {
romanString = n;
}
romanType::romanType() {
}
void romanType::printNum() {
cout << num << endl;
}
And here is my main file:
#include "stdafx.h"
//Main program
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int main()
{
romanType roman;
string romanString;
while (romanString != "EXIT") {
cout << "Enter a roman number: ";
cin >> romanString;
roman.printNum();
roman.setRoman(romanString);
cout << "The equivalent of the Roman numeral "
<< romanString << " is ";
roman.printPositiveInteger();
cout << endl;
cout << endl;
}
//Pause the program
std::cout << "\n\n---------------------------------\n";
system("pause");
//Exit the program
return EXIT_SUCCESS;
}
As I said previously, I am currently held up on the output part, but since I am new and this code is most likely horrid, I am accepting any critique on it. I will be a pretty busy today with work and wont be able to implement any suggestions until the next day, but I will get back to anyone that has a solution as soon as I am able to! Thanks in advance for any help :)
You need to call roman.romanToPositiveInteger() at some point between roman.setRoman(romanString); and roman.printPositiveInteger();
how do I cast a void pointer vector array into a vector array?
vector<string>* vArray = new vector<string[numThreads];
p_thread_create(&my_thread[1], NULL, &function, (void)* vArray[1]);
void* function (vector<string> vArray[])
{
//?? casting?
}
when passed in as argument (void)* vArray[1] it says invalid cast...
And can pthread create take in more arguments, say 2, if the function takes 2 parameters? Thanks
Here's my complete code
#include <iostream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <pthread.h>
#include <queue>
using namespace std;
int array[5] = { 0, 0, 0, 0, 0 };
void* readData(void* arg)
{
string fileName = "Wisconsin.txt";
cout << "HERE!"<< endl;
ifstream cities(fileName.c_str());
string line;
while(!cities.eof()){
getline(cities, line);
if(line != "") {
int commaPos = line.find(',');
int popul = atoi(line.substr(commaPos + 2).c_str());
int x = popul;
if (x >= 1 && x <= 999)
{
array[0]++;
} else
if (x >= 1000 && x <= 9999)
{
array[1]++;
} else
if (x >= 10000 && x <= 99999)
{
array[2]++;
} else
if (x >= 100000 && x <= 999999)
{
array[3]++;
} else
if (x >= 1000000)
{
array[4]++;
}
} // end of IF
} // end of while
}
int main()
{
int numThreads;
ifstream ifs("states.txt");
std::string str((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
stringstream ss(str);
int fileCount = 0;
string fileName;
while (ss >> fileName)
{
fileCount++;
}
cout << fileCount;
string* filesArr = new string[fileCount];
ss.clear();
ss.seekg(0, std::ios::beg);
for (int i = 0; i < fileCount; i++)
{
ss >> filesArr[i];
}
cout << "Enter number of threads: ";
cin >> numThreads;
vector<string>* vArray = new vector<string>[numThreads];
for (int i = 0; i < fileCount; i++)
{
vArray[i % numThreads].insert(vArray[i % numThreads].begin(), filesArr[i]);
}
//queue<string>* queueArr = new queue<string>[numThreads];
//for (int i = 0; i < fileCount; i++)
// queueArr[i % numThreads].push(filesArr[i]);
for(int i = 0; i < numThreads; i ++)
{
cout << endl;
cout << "FOR THREAD " << i + 1 << ":" << endl;
for (std::vector<string>::iterator it = vArray[i].begin(); it != vArray[i].end(); it++)
{
cout << *it << endl;
}
}
pthread_t my_thread[numThreads];
int ret = pthread_create(&my_thread[1], NULL, &readData, (void*)(vArray+1));
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
///////////// ERROR HERE!
pthread_join(my_thread[1], ret);
// for (int id = 0; id < numThreads; id++)
// {
//}
pthread_join(my_thread[1]);
cout << endl;
printf("%-20s", "1-999:");
cout << array[0] << endl;
printf("%-20s", "1,000 - 9,999:");
cout << array[1] << endl;
printf("%-20s", "10,000-99,999:");
cout << (array[2]) << endl;
printf("%-20s", "100,000-999,999:");
cout << (array[3]) << endl;
printf("%-20s", "1,000,000+:");
cout << (array[4]) << endl;
//int pos;
//string token;
//while ((pos = s.find(delimiter))
cin.get();
return 0;
}
pthread join doesn't seem to be working now.. compiling it return me: too few arguments to function 'int pthread_join(pthread_t, void**)
After cleaning up your code:
#include <iostream>
#include <vector>
using namespace std;
const int numThreads = 10;
void * thread_body (void * param)
{
// retrieve parameter
string& prm = *(string *)param;
// use it
cout << prm << endl;
return NULL;
}
int main() {
vector<string >threadPrm(numThreads);
vector<pthread_t>threadId (numThreads);
threadPrm[1] = "Hello";
pthread_create(&threadId[1], NULL, thread_body, &threadPrm[1]);
pthread_join (threadId[1], NULL);
return 0;
}
You don't seem to be comfortable with
vectors
pointers and references
variable naming
Also, trying to shut the compiler up with static casts is a sure way to produce silly code.
The point is usually to make the code run, not to compile it at all cost.
Lastly, you can answer your own question about the number of parameters a posix thread can take by reading the manual
Note:This problem I wrote is just for the people who know about ACM questions.
I have problem with this question. I wrote a good solution for this but every time I send it I get Wrong Answer. I don't know what's wrong here. I test this code for lots of test cases. Can you help me to fix my code?
Here is the link of the question: http://sharecode.ir/section/problemset/problem/1022.
Here is my code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
string page[1000] = { "" };
int cntr = 0;
page[cntr] = "http://www.acm.org/";
string page1;
while (1)
{
cin >> page1;
if (page1 == "QUIT")
break;
if (page1 == "VISIT")
{
cntr++;
cin >> page1;
page[cntr] = page1;
cout << page1 << endl;
}
if (page1 == "BACK")
{
cntr--;
if (cntr >= 0)
cout << page[cntr] << endl;
else
{
cout << "Ignored" << endl;
cntr = 0;
}
}
if (page1 == "FORWARD")
{
cntr++;
if (page[cntr] == "")
cout << "Ignored" << endl;
else
cout << page[cntr] << endl;
}
}
if (n) cout << endl;
}
}
You don't clear forwards after visiting a site. That's why you get wrong answer.
Here is the correct code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int n){
cin >> n;
while (n--){
string c;
vector <string> a(999,"\0");
a[0] = "http://www.acm.org/";
int i = 0;
while(cin>>c,c != "QUIT"){
if (c == "VISIT"){
i++;
string s;
cin >> s;
a[i] = s;
cout << a[i] << "\n";
int t = i+1;
while (a[t] != "\0") {
a[t] = "\0";
t++;
}
}
if (c == "BACK"){
i--;
if (i < 0) {cout << "Ignored\n"; i=0;} else cout << a[i] << "\n";
}
if (c == "FORWARD"){
i++;
if (a[i] == "\0") {cout << "Ignored\n"; i--;} else cout << a[i] << "\n";
}
}
if (n) cout << "\n";
}
}