The confusing logic of the palindrome

Ok, worlds worst programmer here. I must admit, this isn't nearly as confusing as figuring out the confusing logic of the hollow printed square using loops, but it's still confusing. Here is the code I've written below.

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
  
  // preprocessor directive for streaming, variable, and ending statement.
  #include <iostream>
  using std::cin;
  using std::cout;
  using std::ios;
  using std::endl;

  // preprocessor directive for string, and variable string.
  #include <string>
  using std::string;
  using std::getline;

  int main()

{
  //Jesse Burns program number 1: the basics.
  cout << "Lab 1, Programming Basics" <<endl;
  cout << "Programmer: Jesse Burns" <<endl;
  cout << "Editor(s) used: XP Notepad" <<endl;
  cout << "Compiler(s) used: VC++ 2010 Express" <<endl;

  int num1 = 0;
  int num2 = 0;
  int num3;
  string buf;

  cout << "Enter a number to determine a palindrome or Q to quit:"<<endl;
  cin >> buf; num3 = atoi(buf.c_str());
  cin.ignore(1000,10);

	while(true)
	{

	  if (buf == "Q" || buf == "q")break;
		  cout <<"Enter a number."<<endl;
	}
		

	
  num1 = num3 % 10;
  num2 = num3 / 1000;
  
	 if(num1 == num2)
  
	  num1 = num3 % 10;
	  num2 = num3 / 100;
	  
	 if(num1 == num2)
	  
		  cout <<" The palindrome is "<< num1 << num2 << num3 << num2 << num1<<endl;
		



	    cout << "Press enter to continue..."<<endl;
	
  cin.get();


	}


Ok, so the logic isn't that bad, and I think I'm onto something as far as the pattern is concerned. The only thing I'm worried about is that I can't use the if/else statement without using an actual printed statement.

Now the only thing I don't like about that, is that I don't need that printed statement! So using a while(true) loop to break at Q to end the loop, it keeps on looping. Is there any suggestions or advice on this one?

I'm trying to create a five number palindrome btw, with it ending at a sentinel q. Thanks.
No need to convert the number to an actual number before checking if it is a palindrome. Only check that the syntax of the input is in the format of an actual number. i.e. no leading 0's and no non-digit characters in the string

Also you have just created an inifintely running loop on line 32

See this: http://www.cplusplus.com/forum/general/93075/#msg500176

Also format your code to be readable:
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
// preprocessor directive for streaming, variable, and ending statement.
#include <iostream>
using std::cin;
using std::cout;
using std::ios;
using std::endl;

  // preprocessor directive for string, and variable string.
#include <string>
using std::string;
using std::getline;

int main() {

  //Jesse Burns program number 1: the basics.
  cout << "Lab 1, Programming Basics" <<endl;
  cout << "Programmer: Jesse Burns" <<endl;
  cout << "Editor(s) used: XP Notepad" <<endl;
  cout << "Compiler(s) used: VC++ 2010 Express" <<endl;

  int num1 = 0;
  int num2 = 0;
  int num3;
  string buf;

  cout << "Enter a number to determine a palindrome or Q to quit:"<<endl;
  cin >> buf; num3 = atoi(buf.c_str());
  cin.ignore(1000,10);
  
  while(true) { 
  	if (buf == "Q" || buf == "q") break;
  	cout <<"Enter a number."<<endl;
  }
			
  num1 = num3 % 10;
  num2 = num3 / 1000;
  
  if(num1 == num2)
  	num1 = num3 % 10;
  	num2 = num3 / 100;
  	
  if(num1 == num2)
	 cout <<" The palindrome is "<< num1 << num2 << num3 << num2 << num1<<endl;
	 cout << "Press enter to continue..."<<endl;
	 cin.get();
	  
	  return 0;
}
Last edited on
Topic archived. No new replies allowed.