I've been working on a program (nothing big, or anything) and there are some things I'd like to work (That has nothing to do with the fact that there supposed to work :)

Problems:
1 - Screen won't clear (This seems to be the most frequent question about c/c++ "ms-dos" programs, yet even when I follow all directions, it still fails me)

2 - I wan't it to be so that if a user enters invalid numbers, they recieve a message telling them of their mistake, but for some reason it ALWAYS reports an error...

[I]//Successfully compiled in Dev-Cpp (Bloodshed)[/I]
[I]//I cannot seem to remove the exponential notation :([/I]
[I]//The cls command also fails me :([/I]
[I]//I had a perfect number finder included, but it has no true use[/I]
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
[B]using[/B] [B]namespace[/B] std;
 
[B]void[/B] Menu();[B]void[/B] GetFactors();[B]int[/B] Exit();[B]void[/B] Help();[B]void[/B] PrimeFinder();
 
[B]int[/B] main()
{
 Menu();
 [B]return[/B](0);
}
 
[B]void[/B] GetFactors()
{
 cout.unsetf(ios::scientific);
 cin.unsetf(ios::scientific);
 [B]long[/B] [B]double[/B] ctr = 0; [B]long[/B] [B]double[/B] Facts = 0; [B]long[/B] [B]double[/B] Number = 0; [B]long[/B] [B]double[/B] currnum = 1;
 cout << "Enter a number:  ";
 cin >> Number;
 cout << "\n\n\nFactors of " << Number << " include:\n";
 [B]if[/B] (Number == 1)
 {
  cout << "(1 , 1)";
 }
 [B]for[/B] (ctr=1;currnum != 0;ctr++)
 {
  [B]if[/B] ((Number/ctr)==(truncl(Number/ctr)))
  {
   currnum += (ctr-(currnum-1));
   [B]if[/B] (currnum >= (Number/ctr))
   {
    currnum = 0;
   }
   [B]if[/B] (currnum != 0)
   {
    cout << "(" << ctr << " , " << (Number/ctr) << ")\t";
    Facts++;
   }
  }
  [B]if[/B] (Facts >= 3)
  {
   cout << "\n";
   Facts = 0;
  }
 }
 cout << "\n\nPress Enter To Continue...";
 cin.ignore(1);
 cin.get();
 Menu();
}
 
[B]void[/B] Menu()
{
     system("CLS");
     [B]int[/B] Choice = 0;
     cout << "\nHello and welcome to Factor Finder\n\nEnter an option:\n\t1 - Find Factors\n\t2 - Find Prime Numbers\n\t3 - Help\n\t4 - Exit\n\tChoice:  ";
     cin >> Choice;
     [B]switch[/B] (Choice)
     {
            [B]case[/B] 1:
                 GetFactors();
                 [B]break[/B];
            [B]case[/B] 2:
                 PrimeFinder();
                 [B]break[/B];
            [B]case[/B] 3:
                 Help();
                 [B]break[/B];
            [B]case[/B] 4:
                 Exit();
                 [B]break[/B];
            [B]default[/B]:
                 cout << "\n\n\nChoice invalid!\tEnter a new choice\n\n";
                 Menu();
                 [B]break[/B];
     }
}
 
[I]/*[/I]
[I]void PerfectNumber()[/I]
[I]{[/I]
[I]    cout.unsetf(ios::scientific);[/I]
[I]    long /*double Number = 0;[/I]
[I]    system("CLS");[/I]
[I]    cout << "\nEnter how many numbers to check:  ";[/I]
[I]    cin >> Number;[/I]
[I]    cout << "\n";[/I]
[I]    IsPerfect(1,Number);[/I]
[I]    Menu();[/I]
[I]}[/I]
[I]*/[/I]
 
[B]void[/B] Help()
{
     system ("CLS");
     cout << "\nFind Factors:\n\tFinds the factors of a given number"
     << "\n\nFind prime numbers:\n\tFinds the prime numbers within a given range."
[I]//     << "\n\nCheck Prime:\n\tAllows the entry of only one number to check if it is prime."[/I]
     << "\n\nHelp:\n\tDisplays this message."
     << "\n\nExit:\n\tQuits the program."
     << "\n\nPress Enter To Continue...";
     cin.ignore();
     cin.get();
     Menu();
}
 
[B]int[/B] Exit()
{
     [B]return[/B](0);
}
 
[B]void[/B] PrimeFinder()
{
 [B]bool[/B] Error = 0;
 [B]bool[/B] PNum = 0;
 [B]bool[/B] Pressed = 0;
 [B]bool[/B] RangeHasPrime = 0;
 [B]int[/B] Lines = 0;
 [B]long[/B] [B]double[/B] PrimeCnt = 0;
 [B]long[/B] [B]double[/B] Current = 0;
 [B]long[/B] [B]double[/B] ctr = 0;
 [B]long[/B] [B]double[/B] Facts = 0;
 [B]long[/B] [B]double[/B] NumberMin = 0;
 [B]long[/B] [B]double[/B] NumberMax = 0;
 [B]long[/B] [B]double[/B] currnum = 1;
 cout << "What number would you like to begin checking at:  ";
 cin >> NumberMin;
 cout << "\nAnd what number should I search to:  ";
 cin >> NumberMax;
 
//Below is where it should check for errors, but seems to fail miserably
  
[I]// if (NumberMax < NumberMin)[/I]
[I]// {[/I]
[I]//  Error = 1;[/I]
[I]//  cout << "\nError! You cant have the number you start from be less than the one you end at.\n";[/I]
[I]// }[/I]
[I]// if ((NumberMax != truncl(NumberMax))||(NumberMin != truncl(NumberMin)))[/I]
[I]// {[/I]
[I]//  Error = 1;[/I]
[I]//  cout << "\nError! Your number cannot include decimals\n";[/I]
[I]// }[/I]
[I]// if (Error != 1)[/I]
[I]// {[/I]
  cout << "\n\n\nPrime factors from " << NumberMin << " to " << NumberMax << " include:\n"; 
  [B]for[/B] (Current = NumberMin;Current <= NumberMax; Current++)
  {
   currnum = 1;
   PrimeCnt = 0;
   PNum = 0;
   ctr = 1;
   [B]for[/B] (ctr=1;currnum < (Current/ctr);ctr++)
   {
[I]//    if (Current <= 1)[/I]
[I]//    {[/I]
[I]//     PrimeCnt = 3;[/I]
[I]//     Current = 2;[/I]
[I]//    }[/I]
    currnum = ctr;
    [B]if[/B] ((Current/ctr)==(truncl(Current/ctr)))[I]//Cant use modulus on long double[/I]
    {
     PrimeCnt += 2;[I]//Counts factors ([/I]
     currnum = ctr;
     [B]if[/B] (PrimeCnt > 2)
     {
      currnum = 0;
     }
     [B]if[/B] (currnum >= (Current/ctr))
     {
      currnum = 0;
     }
    }
   }
   currnum = 1;
   [B]if[/B] (PrimeCnt == 2)
   {
    cout.unsetf(ios::scientific);
    cout << Current << " , ";
    RangeHasPrime = 1;
    Facts++;
    PrimeCnt = 0;
   }
   PrimeCnt = 0;
   [B]if[/B] (Facts >= 8)
   {
    cout << "\n";
    Facts = 0;
    Lines += 1;
   }
   [B]if[/B] (Lines >= 10)
   {
    cout << "\nDue to the large amount of factors the program has paused to let you view them\nPress enter to continue...\n";
    [B]if[/B] (Pressed == 0)
    {
     cin.ignore();
     Pressed = 1;
    }
    cin.get();
    Lines = 0;
   }
  }
 [I]//}[/I]
 [B]if[/B] (RangeHasPrime == 0)
 {
  cout << "\n\nThe range you entered contains no prime numbers.";
 }
 cout << "\n\nPress Enter To Continue...";
 system ("CLS");
 [B]if[/B] (Pressed == 0)
 {
  cin.ignore();
  Pressed = 1;
 }
 cin.get();
 Menu();
}

If for some reason you want the code opened in a seperate window, go <a href="http://brentsite.bravehost.com/FactorFinder.html">here</a>

It was compiled with the bloodshed freeware compiler, but I assume that my code ought to work with "all" compilers, but I have no others to test it on so...

Thanks in advance
Brent

P.S.
This is probably something I should no, but what does "parse" mean...

>Screen won't clear
I'm not entirely sure of the details, but it probably has something to do with the system call. Try using the WinAPI method described at the end of this article.
http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385

>I wan't it to be so that if a user enters invalid numbers, they recieve a message telling them of their mistake
You'll want to use getline() instead for your input. Use string::find_first_not_of member function to check if the string contains nonvalid numbers, and then finally convert the string back into a number by using string streams. (If that made any sense at all)

Thank you... The clear_screen worked great, and best of all it worked with my compiler (normally things seem to only work with windows visual c++ :( )

The program is pretty dense but I like it. I know the program works but I've modified the GetFactors() function a little to so you another possible way to do it. You can take it or leave it:

void GetFactors()
{
 cout.unsetf(ios::scientific);
 cin.unsetf(ios::scientific);
 
 int ctr = 0
 long double Facts = 0;
 long double Number = 0;
 long double currnum = 1;
 
 cout << "Enter a number:  ";
 cin >> Number;
 cout << "\n\n\nFactors of "<< Number << " include:\n";
 
 if (Number == 1 || isPrime((int)Number)))
  cout << "(1, " << Number << ")" << endl;;
 else {
  for(ctr=2; ctr < Number;ctr++) {
   if(!((int)Number % ctr)) {
    cout << "(" << ctr <<" , " << (Number/ctr) << ")\t";
    Facts++;
   }
  }
  if (Facts >= 3) {
   cout << "\n";
   Facts = 0;
  }
 }
}
 cout << "\n\nPress Enter To Continue...";
 cin.ignore(1);
 cin.get();
 Menu();
}
bool isPrime(int num) {
   for (int i=2;i<num;i++) 
       if (!(num % i)) return false;
   return true;
}

LamaBot

I forgor to ask this one thing... why even though I use the unsetf() function, it still outputs large numbers in exponential notation.

This article has been dead for over six months. Start a new discussion instead.