So i have pretty much figured out how to do this code through a series of trial and error and professor assistance. But im stuck on this last part. For some reason when i take the contents of my array of characters, after it has been uppercased through toupper, my loop isnt wanting to isolate just the alphabet and numbers. Can someone please give me at least a hint in the right direction???
#include <iostream>
#include <string>
#include <iomanip> // for set precision
usingnamespace std;
void center(string display); // Functions
void showMenu();
void palindrome();
void stringinput(char*);
int main() //main
{
char choice;
//Constants for the menu choice
cout << fixed << showpoint << setprecision (2); //Set decimal point to two figures (1/100th)
do
{
fflush(stdin);
//Display the menu and get the user's choice.
showMenu();
while (! (cin >> choice) || !(choice == '1' || choice == '2' || choice == '3')) // Validate the menu selection.
//Loop , and the the user input of what choice to do
{
center( "Are you drunk?? thats invalid order.");
cout<< endl;
cin.clear();
fflush(stdin); // Clear Input buffer.
center ("Re-Enter your choice : ");
}
switch (choice) //Switch menu choices
{
case'1': // Box choice
palindrome();
case'2': // Rainfall statistics choice
fflush(stdin);
cout << endl;
center("Rainfall Statistics : ");
break;
case'3': // end program choice
center ("Programmer: Andrew Holder");
cout << endl;
center ("BYE BYE!!! Press <Enter> key to END the program...");
fflush(stdin);
cin.get();
break;
}
system("CLS"); //Clears screen and then reloops for the entire program
}
while (choice!= '3'); // THis is part of the do while loop that keeps continuing until you pick end program.
cout << endl << endl;
fflush(stdin);
return 0;
}
void palindrome()
{
char word[80];
cout << "Please input a word and press return" << endl;
fflush(stdin);
stringinput(word);
}
void stringinput(char* word)
{
bool palindrome=true;
char temp[80];
int i =0;
cin.getline(word, 80);
for( int i=0 ; i < strlen(word); i++ )
word[i] = toupper( word[i] );
for (unsigned i = 0; i < strlen(word); i++) //unsigned is positive integer
{
if ((word[i]>= 48 && word[i] <=57) || (word[i]>=65 && word[i]<=90)) // only takes numbers and alphabets and puts
// and puts them into temp array.
{
temp[i]=word[i];
}
}
for(int i = 0; i< 80; i++)
{
cout << temp[i];
}
char *front = temp,*rear = (temp + strlen(temp)) - 1;
for(int i =0; front<=rear; front++,rear--)
{
if(*front != *rear)
{
palindrome=false;
break;
}
//cin.get();
}
if (palindrome==true)
{
cout << "The word is a palindrome" << endl;
fflush(stdin);
cin.get();
}
else
{
cout << "The word is not a palindrome" << endl;
fflush(stdin);
cin.get();
}
}
good looking bro, but the cout will still print out the symbols of the input in a very strange fashion if they are at the end of the string. Do you have any idea why that might be??? something to do with the null terminator??? i have no idea, huge beginner. Thank u dude for responding and even if i dont put any symbols or spaces in standard input, the cout does in fact make them all uppercase, but a palindrome like MADAM is coming back false....:/// What small glitch am i messing up on?? my algorithim seems to be right...
NEVER MIND!!! YOUR THE MAN! But for informational purposes, could you explain why initiliazing temp array { ' '} did the trick??? all of a sudden the program works could you explain that to me??? thanks alot man!