#include <iostream>
#include <limits>
usingnamespace std;
int main ()
{
std::cout << "Please choose the program you want to open:\n\n";
std::cout << "(1.) Calculator\n";
std::cout << "(2.) Notepad ++\n";
std::cout << "(3.) Close\n";
std::cin.get();
return 0;
}
1. How can I disable if player clicks ENTER then the console doesn't close?
2. How can I detect what did player wrote in console ?
3. How can I open a program ?
1. Do another read: std::cin.get();
2.
std::cin.get() returns the value that has been entered by the user.
You can either choose to store it in a char, integer, string and more.
e.g. string input = std::cin.get();
std::cout << input << std::endl;
First, when you use usingnamespace std;, you're basically removing the need to type std:: in your programs. So you could simply write cout << "something"; for example.
Second, if you want to get information from a user (in your case, a number) then you need to use variable and probably a while() loop. See:
// Basic calculator design:
#include <iostream>
usingnamespace std;
int main()
{
bool IsRunning = true;
int Choice;
while(IsRunning)
{
cout << "\n\nChoose an option:\n";
cout << "1. Add numbers\n";
cout << "2. Subtract numbers\n";
cout << "3. End program\n- ";
cin >> Choice;
switch(Choice)
{
case 1: AddNumbers(); break; // you'd have to write an AddNumbers() function for this to work.
case 2: SubNumbers(); break; // function call again here for this option.
case 3: IsRunning = false; break; // quit out of your while loop.
default: cout << "\nInvalid input. Try again."; break; // any other input produces this.
}
}
return 0;
}
Third, opening up other programs/applications is a pretty advanced thing for a beginner, it's probably best to create your own programs for now.
"1. How can I disable if player clicks ENTER then the console doesn't close?
2. How can I detect what did player wrote in console ?
3. How can I open a program ?"
1) You can't. By the time the new-line is detected, the input would've been fed into std::istream. You have to process it. If you want to ignore it completely, you'll have to use the OS's API.
2) Use either std::cin.operator >>(), or place the input into a buffer, then parse the buffer's contents.
3) You mean execute? The safest way is to invoke CreateProcess() (Windows). Alternatively, you can use Qt, but linking an entire library to execute a program is a little extreme. Any suggestions to system() should be ignored, if you know what's good for you.
I have 1 question more. How can i find something in string ?
1 2 3 4 5 6 7 8 9 10
std::string theString("hello somethingHere bye");
std::size_t pos = theString.find("somethingHere");
if (pos != std::string::npos)
{
std::cout << "The string was found at position " << pos << ".\n";
}
else
{
std::cout << "The string was not found.\n";
}