#include <iostream>
#include <conio.h>
#include <string>
usingnamespace std;
int n,d=0,r;
string str,wrd="";
void check1()
{
cout << "\n\nEnter a integer number -->\t";
cin >> n;
int copy=n;
while (n!=0)
{
r=n%10;
d=d*10+r;
n=n/10;
}
if (d==copy)
cout << "\n\nThe number " << copy << " is a palindrome" << endl;
else
cout<< "\n\nThe number " << copy << " is not a palindrome" << endl;
}
void check2()
{
cout << "\n\nEnter a string literal -->\t";
cin >> str;
for(int i=str.length()-1;i>=0;i--)
wrd=wrd+str[i];
if(wrd==str)
cout << "\n\nThe string literal " << str << " is a palindrome" << endl;
}
void main()
{
int c1;
cout << "1.\tCheck whether or not an integer number is a palindrome\n" << endl;
cout << "2.\tCheck whether or not a string literal is a palindrome\n" << endl;
cout << "3.\tAll of the above\n" << endl;
cout << "\nEnter your choice (Serial no.) -->\t";
cin >> c1;
switch (c1)
{
case 1:
check1();
break;
case 2:
check2();
break;
case 3:
check1();
check2();
break;
default :
cout << "Invalid Operation" << endl;
}
_getch();
}
Error :
The program is executing perfectly and the palindrome checking with numbers are working ( Check1() is working ) but when it comes to checking with string literals ( Check2() ) it's not working.
This is the error message that pops up when i check with strings :
'Test.exe': Loaded 'H:\visual studio 2010\Projects\Test\Debug\Test.exe', Symbols loaded.
'Test.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Test.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Test.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Test.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'Test.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The program '[8208] Test.exe: Native' has exited with code 0 (0x0).
I declared the variables global because it doesn't hurt to do so does it ? And as far as my check1() is concerned it's working perfectly I did it with while loop because that is shorter and if you use your brain a little you will get my logic. I got the whole program right now anyways so thanks .
What i missed out was a simple else statement in check2(), here let me show you :
1 2 3 4 5 6 7 8 9 10 11 12
void check2()
{
cout << "\n\nEnter a string literal -->\t";
cin >> str;
for(int i=str.length()-1;i>=0;i--)
wrd=wrd+str[i];
if(wrd==str)
cout << "\n\nThe string literal " << str << " is a palindrome" << endl;
else
cout << "\n\nThe string literal " << str << " is not a palindrome" << endl;
}
@PlanetNumbed
I declared the variables global because it doesn't hurt to do so does it ?
You are wrong. At least they confuse a reader of the code. Moreover they prevent to use the functios several time consequently because the variables should be reinitialized.
Shortly speeking it is a very bad style of programming.
@vlad from moscow
You are wrong. At least they confuse a reader of the code. Moreover they prevent to use the functios several time consequently because the variables should be reinitialized.
Shortly speeking it is a very bad style of programming.
How is it supposed to confuse the reader ? And also you are wrong we can use the functions several times it will cause no problem at all. Moreover it has a advantage because while writing your code if you need to use the same variable again then you can easily do it because once you declare the variables globally it can be shared by all the functions while if you declare it locally you have to declare it again.
They confuse a reader because it will think that the variables will be used somewhere outside function check1.
Also you are wrong saying that the funcion can be called several times. For example it is supposed in function check1 that variable d will be always initialized to 0.
int n,d=0,r;
However after the first call of check1 d will not be equal to 0. The similar situation wth variable wrd.
One more it is very and very bad style of programming.
@PlanetNumbed Please don't take the comments on the coding style to be a personal criticism of you yourself.
Remember that people are here to help you - and everyone else, to learn about programming, both in C++ and more generally. It is widely accepted that the use of global variables is bad practice, it's certainly an idea worth considering.
See for example this page: http://www.learncpp.com/cpp-tutorial/42-global-variables/
In some programming languages, all variables are global. Having worked professionally for years using such languages, let's just say I'm very glad that C++ gives plenty of ways to avoid them.
I had been programming with java language previously and I was taught to use global variables more often when working with functions that's the reason why i still have the tendency to use global variables that's it. And I'm not taking anything personally.
I see that your teachers were very weak programmers and understood nothing in object oriented programming,
Reaization of a function shall be hiden from outer code. Otherwise such a function is unsafe.
In fact your function check1 requires presence of the global varables. So it is not enough to have the definition of the function. You also need to have definitions of the global variables.
Now assume that the definition of the function is in some separate module and a user has only its declaration. How will he know that the function requires the global variables and how will he set these global variables that to call the function several times? And more important why shall he bother about initialization these variables each time when he call the function? Does it make his life easier?
Moreover the names of the global variables can conflict with other names.
So this function realization is very and very bad. Its definition is split over the program.
Maybe you are right but in cases where different functions need to share the same variable in that case globalization is necessary.
Can you explain to me in detail why using global variables is a bad practice because I have been using them all along and I never had a single problem with them.
Realizations of functions shall not depend of global variables. Such a design with global variables should be considered as an exception from the general rule.