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.
#include <iostream>
usingnamespace std;
void cipher(string &str)
{
for(unsignedint 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;
}
elseif((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
"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(unsignedint 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;
}
elseif((str[i] >= 'a')&&(str[i] <= 'z'))
{
str[i]=str[i]-13;
if (str[i]< 'a')
str[i]=str[i]+26;
}
return;
}
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!