I'm trying to write a function that will print out function from 1 to 100 and a function that prints out the greatest digit in the integer that was input by the user.
The assignment is:
Program prompts user to input a positive integer x greater than 1000. If the inputted value is invalid, program continuously prompts user to input a valid number. Program then finds the biggest digit of x by using biggestDigit function (c). And prints all the prime numbers from 0 to 100 using isPrime function (b).
and this is my code so far:
#include<iostream>
#include<cmath>
using namespace std;
int biggestDigit(int a){
void is prime(){
for(int a=1;a<=100;a++){
int main(){
int n;
cout<<"Enter an integer greater than 1000: ";
cin>>n;
while(n<=1000){
cout<<"Try again: ";
cin>>n;
}
cout<<biggestDigit(n);
cout<<isPrime();
cout<<endl;
return 0;
}
Here I have an example for a function which read-in an integer. Its control flow is somewhat ugly, but that's almost the case if it shall being able handling various input errors.
/*
* Reads an integer. Range: [min : max].
*/
int read_integer( constint min = 0, constint max = 1000, std::istream& istr = std::cin, std::ostream& ostr = cout)
{
int number;
while (true) {
if (istr >> number) {
if ( min <= number && number <= max) {
return number; // here the solely return point
}
ostr << "The number must be in a range of " << min << " and "<< max<< ".\n";
continue;
}
ostr << "The input was no number. Try again.\n";
// reset the failbit
istr.clear();
// consume all wrong input
char c;
do {
c = istr.get();
} while (c != ' ' && c != '\n');
}
}
int biggestDigit( int number)
{
char max = 0;
std::string num_str = std::to_string( number);
for(char c : num_str) {
if ( c >= '0' && c <= '9' && c > max) {
max = c;
}
}
// makes the ASCII character to an int
returnstatic_cast<int>(max - '0');
}