prime factorization

I have a question for prime factorization since I am being asked to validate the input to exclude anything that is a string, character, or a negative number using a do loop

#include <iostream>
using namespace std;

int main()
{
int a;
int n;
do
{
n % 2==0;


Something like this, maybe:

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
#include <string>
#include <string_view>
#include <cstdlib>
#include <iostream>

bool parse_number(std::string_view s, long long& n)
{
  char*       parse_end = nullptr;
  char const* s_begin   = s.data();
  char const* s_end     = s.data() + s.size();

  n = std::strtoll(s_begin, &parse_end, 10);

  return (parse_end == s_end) && (s.size());
}

int main()
{
  std::string s; 
  long long n = 0;

  std::cin.exceptions(~std::ios_base::goodbit);
  do std::cin >> s; while (!(parse_number(s, n) && n >= 0));
  
  std::cout << n << '\n';
}

Save parse_number, you'll use it again.
The problem states "Write a program that asks the user for an integer and then prints out all its prime factors. For example, when the user 84 the program should print
2
2
3
7
Validate the input to make sure that it is not a character or a string or a negative number using a do loop
The program I use is Dev C++ 6.3
Last edited on
You're not entitled to my help.
read it in as a string, see if it is a number .. for positive values, its just isdigit for all of them since no decimal, minus sign, or other junk would be allowed. then factor it if valid.

Topic archived. No new replies allowed.