Crypting text (char array)

Hi,

I must write a code that takes 4 lines and crypt them with a number. I have already looked on the forum since it's a classic homework problem, but I still don't get exactly what I need.
I am confused between the char array and the string array.

What I want is to be able to enter 4 lines, enter one number, and to add this number to every character of the lines. And then cout something that is "crypted".

I am very grateful for any help I can get.

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
#include<iostream>
#include<string>

using namespace std;

const int numberOfRows = 4;

int main() {

    string blabla[numberOfRows] = {"row1", "row2", "row3", "row4"};

    for (int i = 0; i < 4; i++) {
        cout<< "Enter text:" << endl;
        getline(cin, blabla[i]);
        cout << "You wrote: ";
        cout << blabla[i] <<'\n';
    }

    int crypto=0;
    cout<< "Enter crypting number:" << endl;
    cin>>crypto;

    for(int i=0; i<4;i++){ ;
        cout<< blabla[static_cast<char>(i+crypto)] <<'\n';

}
    return 0;
}
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
#include<iostream>
#include<string>

int main() {

    const int numberOfRows = 4;
    std::string text[numberOfRows] ;

    // accept input from the user for each line of text
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( std::string& line : text ) { // for each line in text

        std::cout << "Enter text:\n" ;
        std::getline( std::cin, line ) ;
    }

    // print out the lines that were entered
    std::cout << "\nyou wrote:\n" ;
    for( const std::string& line : text ) std::cout << line << '\n' ;

    int crypto = 0 ;
    std::cout<< "\nEnter crypting number: " ;
    std::cin >> crypto ;

    // print out the encrypted lines
    std::cout << "\nencrypted with key " << crypto << ":\n" ;
    for( const std::string& line : text ) { // for each line in text

        for( char c : line ) { // for each character in the line

            c += crypto ; // add the number to the character to encrypt it
                          // (we assume that the result is within the range of char)

            std::cout << c ; // print out the encrypted character
                             // (we assume that the encrypted character is printable)
        }

        std::cout << '\n' ; // and print a new line at the end of the line
    }

}
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
#include <iostream>
#include <string>
using namespace std;

string operator && ( string text, int n )
{
   for ( char &c : text ) c += n;
   return text;
}

int main()
{
   const int NUMROWS = 4;
   string row;
   int shunt;

   cout << "Input incrementing number: ";  cin >> shunt;   getline( cin, row );

   for ( int i = 1; i <= NUMROWS; i++ )
   {
      cout << "\nInput row " << i << '\n';
      getline( cin, row );
      cout << ( row && shunt ) << '\n';
   }
}
Thank you a lot. It works nicely.

@JLborges: why does it does not have a return 0?
Last edited on
> why does it does not have a return 0?

The main function has several special properties:
...
4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
...

https://en.cppreference.com/w/cpp/language/main_function
Wait? Really?

Was it always this way?
One of the first rules of programming in c++,that I was taught, was that we needed to include return 0. I noticed that programs still ran without it, but I just thought that it was still considered bad form, like forgeting to dot your i's or something.
Topic archived. No new replies allowed.