Solved

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

Posted on 2004-09-27
3
230 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 clear a vector as well as how to detect empty vectors in C++.

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now