Palindrome

Pages: 12
I was making this palindrome checking program but then this strange error came up , please help me out .

-----------------------------------------------------------------------------

Here's my program :--

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;  

class palinCheck
{
	static void check1()
	{
		int n,d=0,r;
		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;
	}

	static void check2()
	{
		string str,wrd="";
		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;
	}

	static 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;
	}
}


--------------------------------------------------------------------------

Error :--

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1> Palindrome.cpp
1>h:\visual studio 2010\projects\test\test\palindrome.cpp(60): fatal error C1004: unexpected end-of-file found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


--------------------------------------------------------------------------
Last edited on
In the end of the code the last brace is superflouos.

default :
cout << "Invalid Operation" << endl;
}
}


Also the compiler shall isue an error parsing this line

static void main()


and the switch statement shall have a compound statement.
Last edited on
I didn't really get your point vlad. So what should i do to get the program working.
The last } is part of the class definition and requires a semi-colon after it };

Your main method cannot be part of a class either.
Can someone give me the working program snippet. It's really confusing.
Ok so this is what i did.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <conio.h>
#include <string>
using namespace 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).
Why did you decide that these messages are error messages?

By the way it is not clear why variables

int n,d=0,r;
string str,wrd="";

were defined global.

Also in check1 you should use loop do-while istead of while.

The simplest way to check that a string object is palindrome is to use the string costructor that accepts iterators. For example

1
2
3
4
if ( str == std::string( str.rbegin(), str.rend() ) )
{
   std::cout << "The string " << str << " is palindrome." << std::endl;
}
Last edited on
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.
Last edited on
Well the only thing that defines this argument is that different people have different preferences THE END.
@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.


Another article:
http://c2.com/cgi/wiki?GlobalVariablesAreBad
Last edited on
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.
And by the way this program works perfectly fine and there is nothing wrong about it ( at least I don't find anything wrong with it ).
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.
so easy exercise you do on very hard way . look at this . much better and smaller :)
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
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

int main()
{
     char rijec[81];
     bool p=true;
     cin>>rijec;
     int duzina = strlen(rijec);
     if (duzina>0)
     {
             for(int i=0;i<(duzina);i++)
             {
                     if(rijec[i]!=rijec[duzina-1-i])
                     {
                                             p=false;
                     }
             } 
     }
     if(p==true)
     { 
        cout << "Word is a palindrome." << endl; 
     }
     else
     {
        cout << "Word isn't a palindrome." << endl; 
     }
system("PAUSE");
return(0) ;

}

;)
@memkara so easy exercise you do on very hard way . look at this . much better and smaller :)


You are right but i did this to just keep practicing different ways of doing it. You know it's better to know many ways than to know only one. :)

I can give you a shorter one :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>
#include <string>
using namespace std; 

void main()
{	
        string str,wrd="";	
	cout << "\nEnter a literal : ";
	cin >> str;
	for(int i=str.length()-1;i>=0;i--)
		wrd=wrd+str[i];
	if(wrd==str)
		cout << "\n" << str << " is a palindrome" << endl;
	else
		cout << "\n" << str << " is not a palindrome" << endl;
_getch();
}
Last edited on
Pages: 12