| |

VerySource

 Forgot password?
 Register
Search
Author: iskiller

A program, you can only enter numbers, you cannot enter other characters ~?

[Copy link]

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-14 00:15:01
| Show all posts
isdigit()
Reply

Use magic Report

1

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-15 21:45:01
| Show all posts
#include <iostream.h>
#include <conio.h>


int main () {
//declare and initialise variables
int year1=0;
int year2=0;
int countyear=0;
//Display the "introduction"
cout<<"\n ---------Count Leap Year Program---------";
The
//obtain and input of the start year and end year from the user
while(1)
{
cout<<"\n Please enter the start year: ";
cin>>year1;
if(!cin)
{
cout<<"your input is incorrect!!"<<endl;
cin.clear();
cin.ignore(1024,'\n');
continue;
}
break;
The
}
while(1)
{
cout<<"\n Please enter the end year: ";
cin>>year2;
if(!cin)
{
cout<<"your input is incorrect!!"<<endl;
cin.clear();
cin.ignore(1024,'\n');
continue;
}
break;
The
}
The
The
//loop through the leap years from start year and end year
for (int i = year1; i <=year2; i++) {
//year can be divide by 4 left 0, and year can be divide by 100 not equal to 0 or year can be divide by 400 is a leap year. otherwise will not display the years.
if ((i%4==0)&&(i%100!=0)||(i%400==0)){
//display the leap years that user has input
cout <<"\n"<< i;
The
// add the current input to the total
++countyear<<i;
}
}
cout <<"\n The number of Leap year is: "<<countyear<<" of leap years";
cout<<"\n\n Program finished........";
The
The
getch();
The
return 0;
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-7-7 01:30:01
| Show all posts
Thank you so much upstairs!
Can you help explain the meaning of this sentence?

if(!cin)
{
cout<<"your input is incorrect!!"<<endl;
cin.clear();
cin.ignore(1024,'\n');
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-8 18:45:01
| Show all posts
Ha ha, just call the function of identifying numbers directly
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 22:15:01
| Show all posts
On the 5th floor, comment out the "exit(0);" line and it is ok.
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 22:30:01
| Show all posts
Well, yes, there are still errors.
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 22:45:01
| Show all posts
There are still bugs on the 12th floor
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-8-26 23:00:01
| Show all posts
[code=C/C++]
INT8U key, lastkey;
INT8U err;
INT32U root,a,b,n;
char str[33];
INT32U sum=0;
int times=0;
for (;;) {
        if (PC_GetKey(&key)==TRUE){
if(key==0x1B)PC_DOSReturn();
if(key>47&&key<58&×<11||key==8){
if(times==0){
PC_DispStr(27,5," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr(26,6," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
}
if(key!=8){
++times;
PC_DispChar(27+times,5,key,DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=sum*10+key-48;
lastkey=key;
}
if(key==8&×>0){
PC_DispChar(27+times,5,'',DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=(sum-lastkey+48)/10;
times-=1;
}
}
}
if(key==13){
PC_DispStr(5,6,"Result Of The Number:",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
if(times==0){
PC_DispStr(26,6," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr(27,5," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
}
a=3;n=2;
for(;a<=sum;a+=2){
OSSemPend(FuncSem,0,&err);
root=(INT32U)sqrt((double)a);
OSSemPost(FuncSem);
for(b=3;;){
if(a%b==0)break;
b+=2;
if(b>root){++n;break;}
}
}
if(sum==1||sum==2)n-=1;
if(sum==0)n=0;
OSSemPend(FuncSem,0,&err);
ultoa(n, str, 10);
OSSemPost(FuncSem);
PC_DispStr(29,6,str,DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=0;times=0;
}
}
[/code]
Take the example written in my junior year: prime number calculation in uC/OS-II environment. You don't need to understand it in detail. The effect is that you enter a non-number and do not respond. The PC_GetKey() function directly stores the key into the actual parameter.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list