Solved

How do i take each individaul number and put them in the variable

Posted on 2004-09-27
3
238 Views
Last Modified: 2012-05-05
#include <iostream>

using namespace std;

int main(){

String =0;
NumOne = 0;
NumTwo = 0;
NumThree = 0;
NumFour = 0;
NumFive = 0;

     cout << "Please Enter A Palindrome:  ";
     cin >> String;

     if(NumOne == NumFive && NumTwo == NumFour)
     cout << " Thats correct its a Palindrome " << endl;
     
     else  
     cout << " INCORRECT INVALID PALINDROME" << endl;

     return 0;
}

How do i get what they input into my variables the first character in nume one the second in numtwo and so on .........
Thanks
0
Comment
Question by:tedschnieders
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12165034
You could use

#include <iostream>
#include <string>

using namespace std;

void reverse_string ( string& in, string& out) {

    int nSize = in.size();
    out.erase();
    out.resize(nSize);

    for ( int i = 0; i < nSize; ++i) out[i] = in[nSize - i - 1];
}

int main(){

string strOrig;
string strReverse;

     cout << "Please Enter A Palindrome:  ";
     cin >> strOrig;

    reverse_string(strOrig,strReverse);

    if(strOrig == strReverse)
    cout << " Thats correct its a Palindrome " << endl;
     else  
    cout << " INCORRECT INVALID PALINDROME" << endl;

    return 0;
}
0
 

Author Comment

by:tedschnieders
ID: 12165122
Is there anyway i can break down the string into individual numbers or letters entered
 
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12165148
The operator[] that is used to reverse the string does exactly that. But, breaking that down into individually declared variables will limit you in terms of the size of the strings entered. You could of course use

}int main(){

string str;
char c0, c1, c2, c3 c4;

    cout << "Please Enter A Palindrome:  ";
    cin >> str;

    c0 = str[0];
    c1 = str[1];
    c2 = str[2];
    c3 = str[3];

    //...
 
   return 0;
}


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question