Icon Notification in Taskbar - c++

I'm writing a program in C++ that at some points, will need to throw an application notification in the Taskbar. I have added an image to show what I mean.
Previously I have done some search on this, but with no luck, I've only managed to find solutions where the program creates a dialogue box and throws that to the user.
I'd just like to ask, is something like this possible with C++? I've created a small program where I would need something like this to work:
#include <iostream>
#include <string>
using namespace std;
string input;
int main() {
while (1) {
cout << "Type \"test\" to throw a test prompt: ";
cin >> input;
if (input == "test") {
// throw prompt
}
else {
cout << "That wasn't \"test\"";
cout << '\n';
}
}
system("pause");
}

Call the FlashWindowEx function to flash a window's taskbar button.

Related

Movement system with functions for text RPG

I am a beginner student of C++ and I am currently working on a text RPG. I've been able to implement various functions that help the user check location, interact with items, check inventory, look around, and so on, but I cannot seem to get a good working movement system.
Now, I understand that OOP is obviously going to be MUCH more efficient and less frustrating than going the function route, but I am doing this for a class, and we haven't learned anything about classes/objects yet (We haven't even gone over vectors/arrays in our class).
Here is my code to try and get the movement working:
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
void movement(string action, int& currentRoom) {
if (action == "MOVE NORTH") {
if (currentRoom == 1) {
currentRoom = 2;
// This part is just to check if the loop happened and changed values.
cout << currentRoom << " " << "You are now in room two." << endl;
}
}
}
int main() {
int currentRoom;
string action;
cout << "Type 'move [direction]'" << endl;
currentRoom = 1;
getline(cin, action);
boost::to_upper(action);
// This is to check (for testing purposes for me) to see if the string
// converted to uppercase properly.
cout << action << endl;
getline(cin, action);
movement(action, currentRoom);
}
Now, this is not final code that I'm implementing into my game. I've just created a small file to try and work out the logic/syntax of this movement function. When I run this code, I am able to type in 'move north' and it successfully translates into MOVE NORTH, but the function doesn't seem to be calling or doing anything. What am I doing wrong here? Is there any way I can make this easier for myself, without fully leaning into OOP?
Like I said, I haven't been able to learn classes/objects aside from a bit of reading I've done online, and I feel like I would be taking on too much right now to try and learn/implement them properly in such a short time... but maybe it would be for the better if I did? I am not sure.
Any help and input is greatly appreciated.
int main() {
int currentRoom;
string action;
cout << "Type 'move [direction]'\n";
currentRoom = 1;
getline(cin, action);
boost::to_upper(action);
cout << action << '\n'; // here you verify
getline(cin, action); // but then you read again from stdin
movement(action, currentRoom); // and pass it on without changing it toupper.
}

Returning to main() in c++

I just got into c++ and I'm just experimenting. I want to make a simple program which takes users input and calls one of 2 functions, then the function will print a line and ask the user if they want to go again. The issue is c++, for some reason, does not allow me to call main by simple saying main();
Is there any way to call the main function from another function? I am looking for the simplest solution there is, but I can't find anything :/
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int do_math() {
cout << "Math" << endl;
string user;
cout << "would you like to go again? (y or n): " << endl;
cin >> user;
if (user == "y") {
main();
}
else if (user == "n") {
cout << "Okay, bye!";
exit(0);
}
return 0;
}
int do_eng(){
cout << "Eng";
string user;
cout << "would you like to go again? (y or n): " << endl;
cin >> user;
if (user == "y") {
main();
}
else if (user == "n") {
cout << "Okay, bye!";
exit(0);
}
return 0;
}
int main() {
string user;
cout << "Would you like to do math or end?:";
cin >> user;
if (user == "math") {
do_math();
}
else if (user == "end") {
do_eng();
}
return 0;
}
The issue is c++, for some reason, does not allow me to call main by
simple saying main(); Is there any way to call the main function from
another function? I am looking for the simplest solution there is, but
I can't find anything :/
No, you don't want to call main from any of your code.
Not that you should want to do this ... but the simplest solution is to provide a callable function, and get into it in the simplest way from main.
Perhaps:
int myMain()
{
string user;
cout << "Would you like to do math or end?:";
cin >> user;
if (user == "math") {
do_math();
}
else if (user == "end") {
do_eng();
}
return 0;
}
int main(int, const char**)
{
return myMain();
}
Lesson 1 - try to add another level of indirection (i.e. myMain()) does not have the restrictions of main()
Lesson 2 - learn something about recursion ... it seems you probably want to avoid it, here. (i.e. if you always invoke myMain(), how does your program ever terminate?
Lesson 3 - On my system, if the program terminates, I can up-arrow and launch it trivially. Terminal shells do this stuff for you. Perhaps this would be a better approach ... to always terminate unless the user selects one of the action choices (math, burp, etc.)
Lesson 4 - research other programs and how their user interface works. Find a model you like.
Note - I suppose, for your code to call myMain() again, you will need to 'forward declare' the function.
No, the standard specifically disallows calling main() from program code. What you want is to have a loop in main :
int main()
{
bool bContinue;
do
{
/* do something */
std::cout << "Do you want to go again?";
cin >> bContinue;
} while(bContinue);
}

Black screen in c++ disappears to fast [duplicate]

This question already has answers here:
C++ - Hold the console window open?
(11 answers)
Closed 6 years ago.
I have a problem with the c++ code below. The problem is that after the user has typed in the input, the black screen disappears very fast. I would like the black screen to stay up until I have pressed the enter button. I have tried using the cin.get(), but I am new to c++, and I don't understand what is wrong. (I don't get an error code, it's just that I would like the black screen to stay). I am using Visual Studio.
#include <iostream>
#include<string>
using namespace std;
int main() {
string password = "Hello";
cout << "Enter password" << flush;
cin.get();
string input;
cin >> input;
if (input == password) {
cout << "The password is correct" << endl;
cin.get();
}
if (input != password) {
cout << "Access denied" << endl;
cin.get();
}
return 0;
}
It closes because it reaches the end of the main(), which means there is nothing more to do, and the function returns.
A simple solution would be to use getChar() function before the return statement, this will leave the window open until you type a character (any character) on the keyboard.

Visual Studio 2015 - Local Windows Debugger closes out immediately after run?

Hi I'm a highschool student in need of help. I am interested in C++ as I wish to become a programmer one day. I started using Visual Studio for C++ and I'm running a few simple lines of code. But everytime I press (Ctrl + F5) or Local Windows Debugger it shows my line of code but it closes out immediately after it runs, making it near impossible to read. The code is the classic "Hello World!" code. Maybe it has to do something with return 0;?
EDIT: Here's my code.
#include <iostream>
using namespace std;
int main()
{
int mark = 90;
if (mark < 50) {
cout << "HES OLD" << endl;
}
else {
cout << "Hes not old!" << endl;
}
}
When making console applications, I use the following lines at the end of my main function:
std::cout << "\nPaused. Press Enter to continue.\n";
std::cin.ignore(100000, '\n');
The idea is to display a prompt and then wait for the Enter key to be pressed.

C++ code to automatically close console

I am currently learning C++ for a programming course. I noticed that my professor's program automatically closes at the end of their program. It usually prompts the user for an input, and then upon entering an input, the program closes. How would I code that? I only know using return 0 gets me "Press Any Key to Continue"
Note: this is a .exe file
If your program doesn't wait for any input, it runs and finally exits from the program. On exiting, the console automatically closes. I assume, to run the program you're clicking on the .exe, as opposed to runnng the program from cmd.exe, or you run the program from visual studio itself without debugging.
You could just put the following line before return 0;:
std::cin.get();
It will wait for some input and then proceed.
use getch(); before return; statement
Return 0 to give "press any jey to continue" is debugger-specific behavior. Running your compiled exe outside the debugger usually wont show that.
The simple code below does a little more than you're asking for (it repeats what you typed in) but still gives the general idea.
#include <iostream>
using namespace std;
int main() {
cout << "enter something" << endl;
string stuff;
cin >> stuff;
cout << "You entered " << stuff << " you insensitive clod" << endl;
return 0;
}
it is easy, at the end of your main() function put this:
int x;
cin >> x;
this define a new variable and tries to fill it with user-input and then the program will not be terminated till the user gives it input. This is how the program reaches the Press any key to continue, finally you exit the program with a 0 argument and the console window will be destroyed automatically since it is the main window of the process.
I recommend to use:
std::cin.clear();
std::cin.sync();
std::cin.get();
cause there may be times when you need to write something and you will need to press ENTER which will make
std::cin.get();
useles. As it will remeber the first time you pressed ENTER and close the window.
Sample:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Your name: ";
std::cin >> name; \\ <--Place where you press ENTER <--------------
std::cout << "Hi, " << name << ".";
std::cin.get();
return 0;
}