Retooled palindrome problems

Hey. Worlds worst programmer here. I retooled my palindrome, but am still having problems with the if/else statement. I don't get it. I put the if/else statement in the proper place with the proper curly brace, yet I still get the error message that states I'm not using the if before the else statement when it's clear that I am. Any help would be appreciated.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 // Lab 2b, Palindrome
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express

// The necessary C++ file library
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

int main()

{

  // Jesse Burns, Lab 2b.
  cout << "Lab 2b, Palindrome\n";
  cout << "Programmer: Jesse Burns\n";
  cout << "Editor(s) used: JNotePad\n";
  cout << "Compiler(s) used: VC++ 2010 Express\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;

  // Variables for Palindrome
  int a;
  int b;
  int c;
  int d;
  int e;
  int n;
  string buf;

  cout << "Enter five numbers to determine if it is a palindrome or press q or Q to exit." <<endl;
  cout << "Enter the first number."<<endl;
  cin >> buf; a = atoi(buf.c_str());
  cin.ignore(1000, 10);

   cout << "Enter the second number."<<endl;
  cin >> buf; b = atoi(buf.c_str());
  cin.ignore(1000, 10);

   cout << "Enter the third number."<<endl;
  cin >> buf; c = atoi(buf.c_str());
  cin.ignore(1000, 10);

   cout << "Enter the fourth number."<<endl;
  cin >> buf; d = atoi(buf.c_str());
  cin.ignore(1000, 10);

   cout << "Enter the fifth number."<<endl;
  cin >> buf; e = atoi(buf.c_str());
  cin.ignore(1000, 10);

              a = n / 10000;
              n = n % 10000;
              b = n / 1000;
              n = n % 1000;
              c = n / 100;
              n = n % 100;
              d = n / 10;
              n = n % 10;
              e = n / 1;
              n = n % 1;
  while (true)
  {
      if ( buf == "Q" || buf == "q" )break;
      {
             else if
            {
                cout << "Your palindrome number is " << a << b << c << d << e << endl;
            }
             else
             {
                cout << "Enter another number to determine if palindrome."<<endl;
             }
    }
    }

}
1
2
3
4
5
6
7
8
9
10
11
if ( condition ) break;
// the 'if' above controls only whether the break-statement happens
// the scope A below has no connection to the 'if' above

{ // scope A starts
  // no 'if' here, so the following 'else' is an error
  else if // error: no condition for this 'if'
    {
      cout << "foo\n";
    }
} // scope A ends 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 while (true)
  {
      if ( buf == "Q" || buf == "q" )break;

             if
            {
                cout << "Your palindrome number is " << a << b << c << d << e << endl;
            }
             else
             {
                cout << "Enter another number to determine if palindrome."<<endl;
             }

    }


So do I just rewrite it like the one above?
if needs a condition...if (what...?)
Yeah you're right. I guess the confusing thing about this is what my instructor expects.

int n = 12345
a = n / 10000;
n = n %10000;

b = n / 1000;
n = n % 1000;


But the modulus operator knocks off a digit. So the above would be "2345" and "345" respectively. Wouldn't it just be easier to input a number and have it as
if ( a == e && b == d)
cout << "This is a palindrome."<<endl;

?????
Okay, so I think I fixed the problem. The only problem that I still have is with the pesky if/else statement. For some reason, I get an error message with the last 'else' statement, even though it's crystal clear that I do in fact, have an if statement before? Any suggestions?

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
63
64
// Lab 2b, Palindrome
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express

// The necessary C++ file library
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

int main()

{

  // Jesse Burns, Lab 2b.
  cout << "Lab 2b, Palindrome\n";
  cout << "Programmer: Jesse Burns\n";
  cout << "Editor(s) used: JNotePad\n";
  cout << "Compiler(s) used: VC++ 2010 Express\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;

  // Variables for Palindrome
  int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;
  int e = 0;
  int n = 0;
  string buf;

  cout << "Enter five numbers to determine if it is a palindrome or press q or Q to exit." <<endl;
  cout << "Enter the first number."<<endl;
  cin >> buf; n = atoi(buf.c_str());
  cin.ignore(1000, 10);

              a = n / 10000;
              n = n % 10000;
              b = n / 1000;
              n = n % 1000;
              c = n / 100;
              n = n % 100;
              d = n / 10;
              n = n % 10;
              e = n / 1;
              n = n % 1;

  while (true)
  {
             if ( buf == "Q" || buf == "q" )break;
              if (a == e && b == d)continue;
              cout << "Your palindrome number is " << a << b << c << d << e << endl;
              else
              cout << "Enter another number to determine if it is a palindrome."<<endl;

    }
    }

You haven't put braces around your if statements (or indentation, so its hard for us to read). Your logic for the if statements is a bit dodgy, too: Why do you have the continue on line 58? Also, you never give the user the chnce to enter another palindrome...
Last edited on
Oh, well the q/Q is what I used as a loop to exit the program. Would I have to create another loop or if/else statement for another prompt? I did fix the issue though. It seems to be working.

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
63
64
65
66
67
68
// Lab 2b, Palindrome
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express

// The necessary C++ file library
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

int main()

{

  // Jesse Burns, Lab 2b.
  cout << "Lab 2b, Palindrome\n";
  cout << "Programmer: Jesse Burns\n";
  cout << "Editor(s) used: JNotePad\n";
  cout << "Compiler(s) used: VC++ 2010 Express\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;

  // Variables for Palindrome
  int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;
  int e = 0;
  int n = 0;
  string buf;

  //The input needed to determine if number is a palindrome.
  cout << "Enter five numbers to determine if it is a palindrome or press q or Q to exit." <<endl;
  cout << "Enter the first number."<<endl;
  cin >> buf; n = atoi(buf.c_str());
  cin.ignore(1000, 10);

  //The necessary conversion math using modulars.
  a = n / 10000;
  n = n % 10000;
  b = n / 1000;
  n = n % 1000;
  c = n / 100;
  n = n % 100;
  d = n / 10;
  n = n % 10;
  e = n / 1;
  n = n % 1;

  //The user enters Q/q to exit the loop, or enters a number to determine if it's a palindrome.
  while (true)
  {
  if ( buf == "Q" || buf == "q" )break;
  {
  if (a == e && b == d)
  cout << "Your palindrome number is " << a << b << c << d << e << endl;
  }
  else
  cout << "Enter another number to determine if it is a palindrome."<<endl;

}
Topic archived. No new replies allowed.