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.
// 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;
{
elseif
{
cout << "Your palindrome number is " << a << b << c << d << e << endl;
}
else
{
cout << "Enter another number to determine if palindrome."<<endl;
}
}
}
}
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
elseif // error: no condition for this 'if'
{
cout << "foo\n";
}
} // scope A ends
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;
}
}
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?
// 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...
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.
// 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;
}