Stuck on a challange

Stuck on a challange to have user input a value and determie if it is a palindrom; I tried reversing the checks and still the same and the cout's for each line in the for loop was just to verify the comparrison was correct
here is what i have
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
#include <iostream>
#include "stdio.h"
using namespace std;
int main(void)
{
	char strNum[25], strNumReverse[25];
	cin >> strNum;
	int j = atoi(strNum);
	for ( int i = 20; i < j; i++ )
	{
	cout << "i:" << i << "\n";
	char strNum1[25];
	itoa(i,strNum1,10);
	cout << "strNum1:" << strNum1 << "\n";
	strcpy(strNumReverse,strrev(strNum1));
	cout << "strNumReverse:" << strNumReverse << "\n";
	int intReverse = atoi(strNumReverse);
	cout << "intReverse:" << intReverse << "\n";
	if  ( i == intReverse); 
	{
		cout << i << ": Is a palindrome\n";
		continue;
	}
	if ( i != intReverse) 
	{
		cout << "It is not a palindrome\n";
	}
	}
	return 0;
}
Hmm looks like you're having some fun learning!

here are some handy references you'll definitely want to take a look at:

http://www.cplusplus.com/reference/cstdlib/atoi/

http://www.cplusplus.com/reference/cstring/strlen/

the first problem you've got involves using atoi instead of strlen

the second problem you have is the scope of your for loop encloses nearly the entire code

you taught me that strrev is apparently a thing! Who knew!

honestly the first thing I'd do in your case is study up a bit on std::string

http://www.cplusplus.com/reference/string/string/

here's a quick little example:

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

#include <iostream>
#include <string>

using namespace std;

int main(){

    string input;
    string output;

    cout << "Hello, please enter a string, I'm going to do fun things with it!";

    cout << "\n\n"; //newline is backslash n for the uninitiated

    getline(cin,input); //read a whole line from the user (can use spaces)

    int counter = 0;

    int maxCounter = input.size();

    while(counter < maxCounter){

        if (counter % 2 == 0){
            output += input[input.size()-counter-1];
        }
        else{
            output += "-";
        }

        counter = counter + 1;
    }

    std::cout << std::endl; //using namespace std is splendid isn't it?

    cout << "\n" << "\n" << "****\n\n"; //oh output stream operator how I love thee

    cout << output;

    cout << "\n\n\n\t\t()()\n\n\t\t(o.o)\n\n\t\t(\")(\")\n\n";
}

Topic archived. No new replies allowed.