Ceasar cipher

So I want my code to just encrypt the first 5 letters, but I get error message Too many arguments to function call, expected 0, have 1. On line 13.
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
#include <iostream>
#include <string>

using namespace std;
int main()
{
    int key=13;
    string plaintext;
    
    cout << "Enter plaintext: ";
    getline(cin, plaintext);
    
    for (int i = 0, n = plaintext.length(5); i < n; i++)
    {
        if (isalpha(plaintext[i]))
        {
            int offset = 65;e
            
            if (islower(plaintext[i]))
                offset = 97;
            
            int cipheredLetter = (((int)plaintext[i] - offset + key) % 26) + offset;
            
            cout << (char)cipheredLetter;
        }
        else
            cout << plaintext[i];
        
    }
    
    cout << endl;
    return 0;
}
Last edited on
Take a look at the documentation for std::string::length(). How many arguments does that function take? How many arguments are you trying to pass it?

Do you know what that function actually does?
I get error message Too many arguments to function call, expected 0, have 1. On line 13. 


So go to line 13 ... what is the only function called there? ... the member function length of the string plaintext ... which should have 0 arguments (), but you have given it 1 (5)

That is exactly what your compiler tells you!


Line 17
int offset = 65;e
isn't going to help you either.
Sure, but how do I make possible to only encrypt the five first letters?
String function length() does not take any parameters.
http://www.cplusplus.com/reference/string/string/length/

plaintext.length(5) doesn't make any sense.
You could do something like this:
1
2
3
4
    if (plaintext.size() >= 5)
        for (unsigned int i = 0; i < 5; i++)
        {
        // etc. 

Note, the .size() and .length() member functions do the same thing - but size is shorter to type, and has the advantage of being commonly available in other standard containers such as std::vector.
Last edited on
Topic archived. No new replies allowed.