⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 C++ Source code from a tutorial
💻 CPP
字号:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include <sstream>

using namespace std;

int StringToNumber(string MyString) {
    istringstream converter(MyString);
    int result;
    converter >> result;
    return result;
}

string EnterOnlyNumbers() {
    string numAsString = "";
    char ch = getch();
    while (ch != '\r') {  // \r is the enter key
        if (ch >= '0' && ch <= '9') {
            cout << ch;
            numAsString += ch;
        }
        ch = getch();
    }
    return numAsString;
}

string EnterPassword() {
    string numAsString = "";
    char ch = getch();
    while (ch != '\r') {  // \r is the enter key
        cout << '*';
        numAsString += ch;
        ch = getch();
    }
    return numAsString;
}

int main(int argc, char *argv[])
{
    // Just a basic name-entering
    string name;
    cout << "What is your name? ";
    cin >> name;    
    cout << "Hello " << name << endl;

    // Now you are asked to enter a number,
    // but the computer allows you to enter anything!    
    int x;
    cout << endl;
    cout << "Enter a number, any number! ";
    cin >> x;
    cout << "You chose " << x << endl;

    // This time you can only enter a number.
    cout << endl;
    cout << "This time you'll only be able to enter a number!" << endl;    
    cout << "Enter a number, any number! ";
    string entered = EnterOnlyNumbers();
    int num = StringToNumber(entered);
    cout << endl << "You entered " << num << endl;
    
    // Now enter a password!
    cout << endl;
    cout << "Enter your password! ";
    string password = EnterPassword();
    cout << endl << "Shhhh, it's " << password << endl;

    system("PAUSE");	
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -