| |

VerySource

 Forgot password?
 Register
Search
View: 2232|Reply: 17

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

[Copy link]

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

Post time: 2020-2-24 17:30:02
| Show all posts |Read mode
A program, you can only enter numbers, you cannot enter other characters ~? If you enter other characters, an error message appears

How to change? It is best to add a FUNCTION function.


#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 ---------";

// obtain and input of the start year and end year from the user
cout << "\n Please enter the start year:";
cin >> year1;
cout << "\n Please enter the End Year:";
cin >> year2;
Ranch


// 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;
Ranch
// 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 ........";


getch ();

return 0;
}
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-5-10 22:00:01
| Show all posts
C language approach
Use getchar, ungetc
Just scanf
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-5-14 21:30:02
| Show all posts
Alas, use BCB to make a small interface, the code is simpler.
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-5-17 13:45:01
| Show all posts
Consult the library function isdigit () to determine whether a character is a number
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-5-20 16:15:01
| Show all posts
cout << "\n Please enter the start year:";

while (true) {
if (kbhit ()) {
char ch = getch ();
The
if (ch == 13)
break;

if (isdigit (ch)) {
cin.putback (ch);
cout << ch;
}
else {
cout << "not number" << endl;
exit (0);
}
}
}

cin >> year1;
cin.clear ();

Joining this section, I tested it, and there are still many problems, I will explore it slowly.
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-5-24 19:00:01
| Show all posts
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-5-24 19:15:01
| Show all posts
int d12;

while (! (cin >> d12))
{
     cout << "Except for Arabs, no one recognizes it, lose again!" << endl;
cin.clear ();
cin.ignore (1000, '\n');
The
}

cout << d12 << endl;
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-5-24 22:30:01
| Show all posts
Main idea
while (getline (cin, s))
{
for (int i = 0; i <s.size (); ++ i)
{
    if (! isdigit (s [i]))
    {
       cout << "Except for Arabs, no one recognizes it, lose again!" << endl;
       ...
    }
}
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-6-11 21:30:01
| Show all posts
I don’t understand. I’m a beginner in C++. I haven’t read many commands. Can I insert them into my program? Thank you
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-6-13 19:45:02
| Show all posts
char k;
while(cin>>k&&k<='9'&&k>='0')
cout<<k<<endl;
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