gpt4 book ai didi

c++ - 不按回车进入下一条指令

转载 作者:行者123 更新时间:2023-11-30 04:36:29 25 4
gpt4 key购买 nike

我的问题是在不按回车键的情况下尝试转到下一条指令。这是我的代码

cout<<"Enter Date Of Birth: ";
cin>>day;
cout<<"/";
cin>>month;
cout<<"/";
cin>>year;

通过只为一天提供 2 位数字,我希望在不按 enter 键的情况下打印下一条指令,然后转到其余的月份和年份。由于年份是最后一年,我可以在那之后按回车键。

最佳答案

你可以这样写一个函数:

#include <iostream>
#include <sstream>
#include <conio.h>
#include <vector>
#include <math.h>

int getInput(int count)
{
int i = 0;
std::stringstream ss;

while (count)
{
char c = _getch();
std::cout << c;
ss << c;
count--;
}

ss >> i;
return i;
}

int getInput_(int count)
{
int num = 0;

std::vector <int> v;

while (count)
{
char c = _getch();
std::cout << c;
v.push_back(atoi(&c));
count--;
}

for (size_t i = 0; i < v.size(); i++)
{
num += (int)(v[i] * (pow((float)10, (float)(v.size()-1)-i)));
}

return num;
}

int main()
{
int day = 0, month = 0, year = 0;

day = getInput_(2);

std::cout << "/";

month = getInput_(2);

std::cout << "/";

year = getInput_(4);

std::cout << std::endl << day << "/" << month << "/" << year;
}

关于c++ - 不按回车进入下一条指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4524033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com