1 Replies - 226 Views - Last Post: 02 February 2013 - 02:59 PM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Cipher help with ciphering two words

Posted 02 February 2013 - 02:27 PM

I have the following code and it does compile and cipher. However, if I put in two words (i.e. hello world), it will only cipher hello. If a user inputs two words, as "hello world", I am to omit the space and cipher everything. I thought the isalpha function would do that for me, along with the iteration loop, but it only does the one word.
#include<iostream>
#include<string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

class cypher
{
public:
        char apply_cypher(char c);
        cypher(int key1, int key2);
        void encode(string answer);
private:
        int a;
        int b;
};
cypher::cypher(int key1, int key2)
{
        a = key1;
        b = key2;
}
char cypher::apply_cypher(char c)
{
        char result = ((a*c+B)/> % 26)+97;
        return result;
}
void cypher::encode(string answer)
{
        for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
                if (isalpha(*it)){
                        *it = tolower(*it);
                        *it = apply_cypher(*it);
                        *it = toupper(*it);
                        cout << *it;
                }
        }
        cout << endl;
}
int main()
{
        int a = 5, b = 8;
        string answer;
        cout << "Enter the text you want to cipher:";
        cin >> answer;
        cout << "Cypher:";
        cypher cyph(a,B)/>;
        cyph.encode(answer);
        return 0;
}
~                                                                                                                            
                                                                                                           51,1          Bot




Is This A Good Question/Topic? 0
  • +

Replies To: Cipher help with ciphering two words

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Cipher help with ciphering two words

Posted 02 February 2013 - 02:59 PM

This only reads up until any whitespace, so one word.

46        cin >> answer;



Perhaps you could use getline.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1