c++ press a character or number to terminate the input

 1, press a character to terminate the input

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
    char c;//声明变量c
    c=getch();//输入变量c
    cout<<c;//输出变量c
    return 0;
}
    

input: c

output: c

2, press an input to terminate the input

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
    int c;//声明变量c
    c=getch()//输入变量c
    c=c-48//此时c存储的是输入字符的ascll码现在将它转换为数字
    
Example 1 Example 2
Input: 2 (ascll: 50) Input: A (ascll:65)
Output: 2 Output: 17

Guess you like

Origin blog.csdn.net/m0_68045532/article/details/128672615