| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Program to calculate sum of numbers from m to n.
Use for loop or while to solve it.
I make a mistake here, because of "=" and "==".
Program to print Fibonacci series up to 100.
Integer Overflow:
If two number too large, the sum of them maybe to a negative number. It means the result affects the symbolic representation of the first bit.
Program to input a number and then calculate sum of its digits.
#include isdigit()
The functions determine whether the character is an integer.
Using mod to get a last number of a integer
sum = sum + a % 10;
a = a / 10;
static_cast
int num = static_cast(c - '0');
The function will cast character c to a ASCII value like 5 is "53".
Program to find whether given number is a prime number or not.
Program to display sum of series 1 + 1/2 + 1/3 + ... + 1/n.
Program to display series and find sum of 1 + 3 + 5 + ... + n.
Program to use switch statement. Display Monday to Sunday.
Cin.clear and Cin.ignore
if our cin input can's cast correctly like
int day;
cin>>day; //day is "cscac"
it means our cin will into a fail state.
We need to clear the state and remind user to enter correctly, and then clear the values we don't want.
cin.clear();
cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); //ignore all values until to the "\n"
Special case:
If we have a day input which is integer, and we enter a float value(3.2), the cin will get the input 3 first and leave the .2 into the string buffer. So it means in the next cin input, we are going to handle the input ".2"
if (!(cin >> day)) {
//if the input is 2.3, it will enter the correct state first and then enter the fail state, 2 will cast correctly, .3 will enter the false state
cout << day << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "It's fail state" << endl;
}
else {
cout << day << endl;
cout << "It's correct state" << endl;
}
if (!(cin >> day0)) {
//the string buffer have a .2, so it will into the fail state.
cout << day0 << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "It's fail state" << endl;
}
else {
cout << day0 << endl;
cout << "It's correct state" << endl;
}Program to display arithmetic operator using switch case
When we use cin to get input from keyboard, we will get input by type. Like cin>>char op, if we enter asasdad, we will get a first and leave the sasdad to the next cin.
| Back | FazBrowse Home | New Git URL |