3 Questions

Hi I have made a simple code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <limits>

using namespace 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;

3.
This can be done with the system() method, but it is not recommended see http://www.cplusplus.com/articles/j3wTURfi .
Maybe this post will help you: http://www.cplusplus.com/forum/lounge/17684/
Last edited on
First, when you use using namespace 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Basic calculator design:

#include <iostream>

using namespace 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.
Thanks both.

I have 1 question more. How can i find something in string ?
In lua we have use

string.find(theString, "somethingHere")
closed account (zb0S216C)
Blaz Rezar wrote:
"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.

Wazzak
Last edited on
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";
}
Last edited on
Thanks Peter.

I have one last question. How can I clear Console ?
Topic archived. No new replies allowed.