Hi there, im new to the site and am hoping someone out there can help me with my question, im pretty newbie in the world of c++ and programming in general. Im making a program with a menu, you can choose between entering a number yourself to be tested as a palindrome, or the second option opens a menu where you can enter a filename, amount of numbers, and then it generates the specified number of 5 digit random numbers into the specified file, then reads them back but in the process of reading them back it sorts them palindromes and regular numbers.

so like:

(menu whatever here)

Palindromes:
12321
11111
16361
11211

regular numbers:
12345
76543
18496

option 3 is just an exit.

Here is what I have so far.
(it does everything I want, except in option 2 when it hits the 2 loops i used to try and sort the dang numbers, they wont even sort or anything, i threw in a debug to see if it reads em and it does, but when it compares to the reversed numbers in the if statement, it must think they are different for some reason. i dont really know.)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

void menuitem1();
void menuitem2();

int main()
{
    system("cls");
    
    int choice = 0;
    
    cout << "Palindrome Checker Menu" << endl
         << "-------------------------------" << endl
         << "1. Enter a number" << endl
         << "2. Generate a list" << endl
         << "3. Exit" << endl
         << "-------------------------------" << endl;
    
    cin >> choice;
         
    if(choice == 1)
    {
              menuitem1();
    }
    else if(choice == 2)
    {
              menuitem2();
    }
    else if(choice == 3)
    {
         cout << "\n<EXITING PROGRAM>" << endl;
         system("pause");
         return 0;
    }
}

void menuitem1()
{
     system("cls");
     int entry = 0;
     int entrys = 0;
     int rev = 0;
     
     cout << "Palindrome Checker Menu" << endl
          << "-------------------------------" << endl
          << "Please enter a whole number." << endl
          << "Examples: 1, 2, 3... 200, 300..." << endl
          << "86452, 86278, 1111..." << endl
          << "-------------------------------" << endl
          << "Entry:      ";
         
     cin >> entry;
     
     entrys = entry;
     
     while(entrys>0)
     {
              rev *= 10;
              rev += entrys%10;
              entrys /= 10;
     }
     
     
     cout << "Reverse:    " << rev;
    
     if(entry == rev)
     {
            cout << endl << endl 
                 << "That means you have a palindrome." << endl
                 << "The number is the same backwards as it is forward." 
                 << endl << endl;
     }
     else if(entry != rev)
     {
            cout << endl << endl 
                 << "That means you do not have a palindrome." << endl
                 << "The number is not the same backwards as it is forward." 
                 << endl << endl;
     }
     system("pause");
     main();
}

void menuitem2()
{    
     system("cls");
     
     int rev = 0;
     int dromes = 0;
     int regs = 0;
     int amount;
     int amounts;
     int rnumber;
     unsigned seed = time(0);
     char filename[50];
     int filetext;
     int filetexts;
     srand(seed);
     ofstream rfile;
     ifstream wfile;
     
     cout << "Palindrome Checker Menu" << endl
          << "-------------------------------" << endl
          << "This is going to generate" << endl
          << "a list of random numbers." << endl
          << "What would you like to name the file?" << endl
          << "(20 character limit.)" << endl
          << "(The .txt will be added automatically.)" << endl
          << "-------------------------------" << endl;
          
     cin >> filename;
     
     system("cls");
     
     cout << "Palindrome Checker Menu" << endl
          << "-------------------------------" << endl
          << "This is going to generate" << endl
          << "a list of random numbers." << endl
          << "How many numbers would you like to generate?" << endl
          << "(Whole numbers only please)" << endl
          << "(All numbers will be 5 digits)" << endl
          << "-------------------------------" << endl;
     
     cin >> amount;
     
     amounts = amount;
     
     strcat(filename, ".txt");
     
     rfile.open(filename);
     
     if(amount <= 0)
     {
          cout << endl << endl
               << "You seem to have entered an invalid number, returning to Main Menu..." << endl;
          system("pause");
          main();
     }
     
     do
     {    
          rnumber = 1 + rand() % 100000;
          
          if(rnumber > 9999)
          {
                     rfile << rnumber << "\n";
                     amount -= 1;
          }
     }while(amount > 0);
     
     rfile.close();
     
     system("cls");
     
     wfile.open(filename);
     
     cout << "Palindrome Checker Menu" << endl
          << "-------------------------------" << endl
          << "Here is your generated list." << endl
          << "Filename: " << filename << endl
          << "Amount of numbers: " << amounts << endl
          << "-------------------------------" << endl
          << "Palindromes: " << endl;
          
     
     wfile >> filetext;
     while(wfile.good())
     {
             wfile >> filetext;
             
             dromes++;
             cout << "Debug - " << dromes << ". " << filetext << endl;
             
             filetexts = filetext;
             
             while(filetexts > 0)
             {
                    rev *= 10;
                    rev += filetexts%10;
                    filetexts /= 10;
             }
             
             if(filetext == rev)
             {
                         dromes++;
                         cout << dromes << ". " << filetext << endl;
             }
             
     }
     
     wfile.close();
     
     cout << endl << "Regular Numbers: " << endl;
     
     wfile.open(filename);
     
     wfile >> filetext;
     while(wfile.good())
     {
             wfile >> filetext;
             
             regs++;
             cout << "Debug - " << regs << ". " << filetext << endl;
             
             filetexts += filetext;
             
             while(filetexts > 0)
             {
                    rev *= 10;
                    rev += filetexts%10;
                    filetexts /= 10;
             }
             
             if(filetext != rev)
             {
                         regs++;
                         cout << regs << ". " << filetext << endl;
             }
             
     }
     
     wfile.close();
     
     system("pause");
     main();
}

Apologies in advance for my more than likely redundant and disorganized coding... D:

Recommended Answers

All 2 Replies

Never -- NEVER -- NEVER call main() to return from a function!!! Never!!

Modularize your program. Create subroutines that do a single part of the task and call them, like
1) Generate the list of numbers
2) Read the list
3) Test for palindrome
etc.

Then you can work in short sections at a time.
As for your sort problem, I see nothing that sorts at all. Nor can I see a reason to sort.

Also, for the sanity of your user, get rid of all the system("cls"); commands...
And the system("pause"); commands -- see this

Thank you muchly for your help :).

Found the solution:
I was using wfile twice (the thing to open the file for reading), and a couple other minor tweaks.

P.S.
Normally I would revamp till I get it without all the cls and main() calls, but im short on time an its for a class, and she only wants function, doesnt care about size or efficiency or portability. Definite thank you for the links and tips. ill keep them in mind next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.