Solved

Pages: 12
Hello Windsen, you are more likely to get help if you show what efforts you have made so far in the assignment.

And the more specific your questions, the better.
Last edited on
I wish I was learning C++ at school ):
Last edited on
You can still learn C++ online, there are two tutorials I recommend.

The one here a CPlusPlus (though it isn't up to date with C++14/17/20):
http://www.cplusplus.com/doc/tutorial/

Learn C++ (constantly being updated, though not a complete tutorial on ALL of C++ features):
https://www.learncpp.com/

Lots of examples at both tutorials. I prefer Learn C++ over the tutorial here.

NOT a tutorial site, it is for reference:
https://en.cppreference.com/w/

It is as up-to-date for both C and C++ as possible, with examples for much of the language features.

It is NOT for beginners, period. Many of the examples on language features use other language features without explanation. And the write-ups on language features are heavy on tech jargon, as befits a reference.
Last edited on
@Windsen - what problem are you having? What don't you understand? What have you attempted so far? Post your current code. NB There's plenty of previous posts on this forum and elsewhere on how to calculate/obtain prime numbers.
The purpose of homework is that you learn by doing. Nobody else can do that for you.

Should we help by saying: You can do it! or would We will break bones of lazy maggots! inspire you more?

We can explain concrete problems, once you have some. For example, why comma is in wrong place.
show what you tried.
a tip for the first part:
a positive number > 1000 has 4 or more digits. 1000 itself is the only 4 digit illegal value, you can compare "1000" to check the special case.
if you read it as a string, and every character isdigit(), and the length() >3, its ok, right?
if you read it as a string, you can either iterate it [] or sort() and peel the largest digit.
can you do that much (validate and find the largest digit)? remember that '9' and 9 are not directly related, but it may not matter since '0' < '1' ... < '9' so comparison still works correctly. Just be careful not to confuse integer values and letters as this can be a silent bug due to letters actually BEING a type of integer.

the first part is harder to do if you read the input into an integer. you can still do it, its just a bit of trouble. %10 gets you the least digit (ones place) and /10 removes the least digit, repeat until number is zero in a loop...
Last edited on
Please edit your post and add code formatting with [ code] { code here } [ /code] (no spaces).

You can't define functions within main.

It needs to be more like:

1
2
3
4
5
6
7
8
9
bool isPrime(int n)
{
    // ...
}

int main()
{

}


or

1
2
3
4
5
6
7
8
9
10
11
12

bool isPrime(int n);

int main()(
{

}

bool isPrime(int n)
{
    // ...
}
Last edited on
Windsen1:
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

M'ok, you've shown your assignment, you posted what code you've written so far. Now, what is the problem?

Having trouble with giving the user up to 5 tries to enter input? Think loops. for loop, do-while loop or while loop.
oh, he did show code, I didn't scroll around enough.

the first part, then, you would do like this if you insist on doing it via integers (sigh).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
unsigned int input{};
unsigned int tmp{};
int attmpts{5};
bool good{};
do
{
cout << blah blah;
cin >> input;
///if needed, handle junk string inputs and so on here. if not, ignore that idea. 
else //else for the junk handler. 
if(input > 1000) good = true;
{ //ok, its good.
    tmp = input;
    int digit = 0;
    while(tmp)
    {
        digit = tmp%10;  //not all these variables don't exist in my pseudo-code. 
        if(digit > biggest) biggest = digit;
        tmp /= 10;
    }
   if(!good)
   cout << blah blah << attempts << and stuff;
}
while (--attempts && !good);
Last edited on
As a starter, perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <algorithm>
#include <iostream>
#include <string>

int getInt() {
	constexpr size_t maxTries {5};
	int num {};
	size_t cnt {};

	do {
		std::cout << "Enter a positive number > 1000: ";
		std::cin >> num;
		std::cin.clear();
	} while (std::cin.ignore(1000, '\n' ) && (num <= 1000) && (std::cout << "Invalid number. " << maxTries - ++cnt << " left\n") && cnt < maxTries);

	return cnt < maxTries ? num : -1;
}

int biggestDigit(int x) {
	const auto sx {std::to_string(x)};

	return *std::max_element(sx.begin(), sx.end()) - '0';
}

int main() {
	const auto no {getInt()};

	if (no > 0)
		std::cout << "Max digit is " << biggestDigit(no) << '\n';
}

learned what? If you see something you don't know, ask specifically.
schools still have not picked up on {}.
int x{};
is the same as
int x = 0;
as far as you are concerned.

as for do-while, all loops are the same, the difference is just syntax elegance. do-while sometimes avoids having extra code outside the loop, but that is minor.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
// #include <cmath> // NO NEED FOR THIS

using namespace std;

bool isPrime (int num);
int biggestDigit(int x); // MISSED  A ';'

int main ()
{
    int a_number = 0;
    cout << "Enter a number: ";
    cin >> a_number;
    
    int answer = biggestDigit(a_number);
    cout << "Biggest digit is " << answer << endl;
    
    return 0;
}

// FUNCTIONS GO HERE NOT INSIDE main()
int biggestDigit(int x)
{
    int biggest = -1; // EVERY DIGIT WILL BE BIGGER THAN THIS
    int leftover = 0;
    int digit = 0;
    
    do
    {
        digit = x % 10;
        leftover = x / 10;
        
        if(digit > biggest)
            biggest = digit;
        
        x = x/10;
        
    } while( leftover != 0 );
    
    return biggest;
}



Enter a number: 35082
Biggest digit is 8
Program ended with exit code: 0
code tags...
and, that won't compile. Just reading it in a drive by I see at least 3 reasons why it will not.
Last edited on
Windsen1, I remind you:
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

You've been asked politely to use code tags more than once, by several people.

READ THE LINKS and USE CODE TAGS!
A limit of 5 guesses? Use a loop. A for loop, while loop or do-while loop are part of the C++ toolbox.

Don't know about loops? Look here:
https://www.learncpp.com/cpp-tutorial/intro-to-loops-and-while-statements/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>

using namespace std;

bool isPrime (int);
int biggestDigit(int);

int main ()
{
    // PART 1
    for(int i = 0; i < 101; i++)
    {
        if( isPrime(i) )
            cout << i << ' ';
    }
    cout << '\n';
    
    // PART 2
    int no_attempts{1};
    int number{0};
    while(
          cout << "Enter a number: " &&
          cin >> number &&
          number < 1000 &&
          no_attempts < 5
          )
    {
        cout << "Number must be greater than 1000\n";
        no_attempts++;
    }
    
    if(no_attempts < 5)
    {
        int answer = biggestDigit(number);
        cout << "Biggest digit is " << answer << endl;
    }
    else
        cout << "Too many tries\n";
    
    return 0;
}

bool isPrime (int x)
{
    if(x < 2)
        return false;
    
    for (int i = 2; i * i <= x; i++)
    {
        if( x % i == 0)
            return false;
    }
    
    return true;
}

int biggestDigit(int x)
{
    int biggest = -1;
    int leftover = 0;
    int digit = 0;
    
    do
    {
        digit = x % 10;
        leftover = x / 10;
        
        if(digit > biggest)
            biggest = digit;
        
        x = x/10;
        
    } while( leftover != 0 );
    
    return biggest;
}


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
Enter a number: 34
Number must be greater than 1000
Enter a number: 7089
Biggest digit is 9
Program ended with exit code: 0
Windsen1 wrote:
need it to show how many tries I have left
againtry wrote:
1
2
3
4
5
6
7
8
9
10
11
    while(
          cout << "Enter a number: " &&
          cin >> number &&
          number < 1000 &&
          no_attempts < 5
          )
    {
        cout << "Number must be greater than 1000\n";
        no_attempts++;
        // this is the point where you can compute "tries left" from no_attempts and show it
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>

using namespace std;

bool isPrime (int);
int biggestDigit(int);

int main ()
{
    // PART 1
    for(int i = 0; i < 101; i++)
    {
        if( isPrime(i) )
            cout << i << ' ';
    }
    cout << '\n';
    
    // PART 2
    int no_attempts{1};
    int number{0};
    while(
          cout << "Enter a number: " &&
          cin >> number &&
          number < 1000 &&
          no_attempts < 5
          )
    {
        cout << "Number must be greater than 1000\n";
        no_attempts++;
        
        switch(no_attempts)
        {
            case 1:
                cout << "5 tries left\n";
                break;
            case 2:
                cout << "4 tries left\n";
                break;
            case 3:
                cout << "3 tries left\n";
                break;
            case 4:
                cout << "2 tries left\n";
                break;
            case 5:
                cout << "1 try left\n";
                break;
            default:
                cout << "Impossible\n";
                break;
        }
    }
    
    if(no_attempts < 5)
    {
        int answer = biggestDigit(number);
        cout << "Biggest digit is " << answer << endl;
    }
    else
        cout << "Too many tries\n";
    
    return 0;
}

bool isPrime (int x)
{
    if(x < 2)
        return false;
    
    for (int i = 2; i * i <= x; i++)
    {
        if( x % i == 0)
            return false;
    }
    
    return true;
}

int biggestDigit(int x)
{
    int biggest = -1;
    int leftover = 0;
    int digit = 0;
    
    do
    {
        digit = x % 10;
        leftover = x / 10;
        
        if(digit > biggest)
            biggest = digit;
        
        x = x/10;
        
    } while( leftover != 0 );
    
    return biggest;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>

using namespace std;

bool isPrime (int);
int biggestDigit(int);
string howManyLeft(int);

int main ()
{
    // PART 1
    for(int i = 0; i < 101; i++)
    {
        if( isPrime(i) )
            cout << i << ' ';
    }
    cout << '\n';
    
    // PART 2
    int no_attempts{1};
    int number{0};
    while(
          cout << "Enter a number: " &&
          cin >> number &&
          number < 1000 &&
          no_attempts < 5
          )
    {
        cout << "Number must be greater than 1000\n";
        cout << howManyLeft(no_attempts);
        no_attempts++;
    }
    
    if(no_attempts < 5)
    {
        int answer = biggestDigit(number);
        cout << "Biggest digit is " << answer << endl;
    }
    else
        cout << "Too many tries\n";
    
    return 0;
}

bool isPrime (int x)
{
    if(x < 2)
        return false;
    
    for (int i = 2; i * i <= x; i++)
    {
        if( x % i == 0)
            return false;
    }
    
    return true;
}

int biggestDigit(int x)
{
    int biggest = -1;
    int leftover = 0;
    int digit = 0;
    
    do
    {
        digit = x % 10;
        leftover = x / 10;
        
        if(digit > biggest)
            biggest = digit;
        
        x = x/10;
        
    } while( leftover != 0 );
    
    return biggest;
}

string howManyLeft(int x)
{
    string howMany[]
    {
        "5 tries left\n", "4 tries left\n", "3 tries left\n",
        "2 tries left\n", "1 tries left\n"
    };
    
    return howMany[x];
}

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
Enter a number: 1
Number must be greater than 1000
4 tries left
Enter a number: 1023
Biggest digit is 3
Program ended with exit code: 0
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>

using namespace std;

bool isPrime(int);
int biggestDigit(int);

int main() {
	for (int i = 0; i < 101; ++i)
		if (isPrime(i))
			cout << i << ' ';

	cout << '\n';

	int no_attempts {1};
	int number {};

	while (
		cout << "Enter a number greater than 1000: " &&
		cin >> number &&
		number < 1000 &&
		no_attempts < 5
		)
	{
		cout << "Number must be greater than 1000\n";

		if (++no_attempts <= 5)
			cout << 6 - no_attempts << " tries left\n";
	}

	if (no_attempts < 5) {
		cout << "Biggest digit is " << biggestDigit(number) << '\n';
		cout << boolalpha << isPrime(number) << '\n';
	} else
		cout << "Too many tries\n";
}

bool isPrime(int number) {
	if (number != 2) {
		if (number < 2 || number % 2 == 0)
			return false;

		for (int i = 3; (i * i) <= number; i += 2)
			if (number % i == 0)
				return false;
	}

	return true;
}

int biggestDigit(int x)
{
	int biggest {-1};

	do {
		const int digit {x % 10};

		if (digit > biggest)
			biggest = digit;

	} while ((x /= 10) != 0);

	return biggest;
}


Pages: 12