How can I format this file?

I am currently working on an extra credit assignment for my CS014 class. I have the assignment finished, however, I have run into a bump while submitting. The instructions are to "Submit a main.cpp with your solution to the extra credit and a cipher.cpp which contains only a function 'cipher' that can be used to decipher the piazza post." I am having trouble with the cipher file. I tried to submit it a few different ways, however it resulted in an error from the website. I have two more submission attempts so I'd like to get it right the next time.

I first tried to submit it as follows:
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
#include <iostream>

using namespace std;

void cipher(string &str)
{
    for(unsigned int i = 0; i < str.size(); i++)
        if((str[i]>=65)&&(str[i]<91))
        {
            str[i]=str[i]-13;
            if (str[i]<=65)
                str[i]=str[i]+26;
        }
        else if((str[i]>=97)&&(str[i]<123))
        {
            str[i]=str[i]-13;
            if (str[i]<=96)
                str[i]=str[i]+26;
        }
    return;
}

int main()
{
    string code;
    cout << "Enter text to unscramble\n";
    std::getline(std::cin, code);
    cipher(code);
    cout << endl << code;
    return 0;
}


This resulted in a "multiple definition of `main'" compiler error on the website. I then took out the main function and am getting a "multiple definition of `cipher(std::string&)'" error now. How can I correct this? The main.cpp file does not use this function. The command that will be used to compile this is
 
g++ main.cpp cipher.cpp -Wall -o a.out


Thank you.
Last edited on
You aren't following the instructions:
which contains only a function 'cipher'

So get rid of the main function.

Presumably it will be compiled with a pre-written main for testing.
"Submit a main.cpp with your solution to the extra credit and a cipher.cpp which contains only a function 'cipher' that can be used to decipher the piazza post."

Shouldn't you have two files? Where is the other one? Your cipher() function should be in a file by itself, and main() should be in a file by itself.

Do you know how to compile from the command line? Try it yourself before submitting to make sure it works.

BTW, I did the above, ran the program twice and got:


input: I AM CAESAR
output: V NZ PNRFNE

input: V NZ PNRFNE
output: I [M C[ES[R


I don't think this is how you mean your cipher to work. I suggest not using the ascii character codes in your program. Use the actual character you want to represent when possible to avoid errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void cipher(string &str)
{
    for(unsigned int i = 0; i < str.size(); i++)
        if((str[i] >= 'A')&&(str[i] <= 'Z'))
        {
            str[i]=str[i]-13;
            if (str[i]< 'A')
                str[i]=str[i]+26;
        }
        else if((str[i] >= 'a')&&(str[i] <= 'z'))
        {
            str[i]=str[i]-13;
            if (str[i]< 'a')
                str[i]=str[i]+26;
        }
    return;
}

Last edited on
Here's the main file
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
#include <iostream>
#include <list>
#include "cipher.cpp"

int COUNT = 1;
using namespace std;

class Print
{
public:
    Print();
    ~Print();
};

Print::Print()
{
    cout << " " << COUNT;
    COUNT++;
}

Print::~Print()
{

}

int main()
{
    int i;
    cout << "Enter a number to print out all previous numbers for:\n";
    cin >> i;
    cout << COUNT++;
    Print temp[i-1];
    cout << endl;
}


The extra credit was to print out numbers from 1 to N with N being a number input by the user. The catch is, no conditionals can be used.

The cipher works, it has to unscramble the following phrase:
 
Gur rkgen perqvg unf orra cbfgrq va Mlobbxf puncgre gjragl sbhe. Lbh arrq gb ragre gur pbqr 'pvcure' gb ivrj vg. Tbbq yhpx!

This phrase is how we found out we had an extra credit assignment.

My first thought for this was use ASCII since I have been doing that a lot in my assembly class. I like your method better Browni3141 :b

I did try to remove the main class, which is when I started to get a "multiple definition of `cipher(std::string&)'" error.
Normally you should use a header file to declare functions/classes/etc, and implement those functions in a .cpp file. I'm a little bit confused as to what the actual task is, but I'm guessing it's what tpb said? The instructor will probably provide their own main.cpp and you only have to provide the implementation of the cipher function?

If you have to provide your own main() and can't upload an extra header file, you have to forward declare void cipher(string&); above your main(). You don't have to #include anything extra.
Ok, it all makes sense now. I removed the main function long ago but I kept on getting the multiple definition error. Turns out I had forgotten to remove the #include "cipher.cpp" line from my main. Once I removed that, it all worked perfectly. Thanks for the help guys!
Last edited on
Topic archived. No new replies allowed.