Get help on programming - C++, Java, Delphi, etc.
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 10:10
Hello again all.
This is probably my 600th thread about C++ but who really cares...
Here is the code.
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
using namespace std;
void encryptionEngine(char* input)
{
int lineLength = strlen(input);
for(int i = 0; i < lineLength + 1; i++)
out<<++(*input);
out<<"\n";
for(int k = 0; k < lineLength + 1; k++)
out<<*(++input);
out<<"\n";
}
int main(int argc, char** argv)
{
if(argc < 3)
{
cout<<"too few arguments here"<<endl;
cout<<"USAGE: ./cos121_prac2_task1 filename mode steps"<<endl;
}
else if(argc > 4)
{
cout<<"too many arguments here"<<endl;
cout<<"USAGE: ./cos121_prac2_task1 filename mode steps"<<endl;
}
else
{
string inputFile = argv[1];
ifstream input(argv[1]);
string mode = argv[2];
int steps = 3;
if(argc == 4)
steps = atoi(argv[3]);
if(!input)
cout<<"FILE NOT FOUND : " + inputFile<<endl;
else if(mode != "e" && mode != "d")
cout<<"INCORRECT MODE : Only e or d accepted."<<endl;
else
{
if(mode == "e")
ofstream out("encodedMessage.txt");
else
ofstream out("decodedMessage.txt");
string line;
getline(input,line);
char* lineChar;
lineChar = new char[line.length() + 1];
strcpy(lineChar, line.c_str());
encryptionEngine(lineChar);
}
}
return 0;
}
and here is the error:
Code: Select all
g++ -O2 -g -Wall -Wextra -static -c main.cpp -o main.o
main.cpp: In function ‘void encryptionEngine(char*)’:
main.cpp:14: error: ‘out’ was not declared in this scope
main.cpp:15: error: ‘out’ was not declared in this scope
make: *** [main.o] Error 1
I know where the error occurs, but how do I fix it...
Kronos
Moderator Emeritus
Posts: 4280 Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:
Post
by Kronos » 31 Mar 2009, 10:24
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
using namespace std;
void encryptionEngine(char* input)
{
int lineLength = strlen(input);
for(int i = 0; i < lineLength + 1; i++)
out<<++(*input);
out<<"\n";
for(int k = 0; k < lineLength + 1; k++)
out<<*(++input);
out<<"\n";
}
perhaps use cout?
rustypup
Registered User
Posts: 8872 Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:
Post
by rustypup » 31 Mar 2009, 10:26
<edit>
too slow...
"out" or "cout"... ?
‘out’ was not declared in this scope - "You're using a reference which hasn't been declared." - the compiler is rarely wrong about these things...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 10:35
I need to output it to the file specified in the main method.
ofstream out("encodedMessage.txt")
OR
ofstream out("decodedMessage.txt")
rustypup
Registered User
Posts: 8872 Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:
Post
by rustypup » 31 Mar 2009, 10:47
DarkRanger wrote: I need to output it to the file specified in the main method.
and where are you
declaring "out"?... think about it, whilst considering the meaning of the {} braces with respect to scope...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 10:54
Should I declare out outside the main method?
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 10:58
okay, I declared out as follows
Code: Select all
using namespace std;
ofstream out;
Now I get a new error:
Code: Select all
main.cpp: In function ‘int main(int, char**)’:
main.cpp:44: error: no match for call to ‘(std::ofstream) (const char [19])’
main.cpp:46: error: no match for call to ‘(std::ofstream) (const char [19])’
make: *** [main.o] Error 1
rustypup
Registered User
Posts: 8872 Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:
Post
by rustypup » 31 Mar 2009, 11:10
did you read the
API ... "
no match for call to " generally means no method pattern matches your usage...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 11:31
Ok, I changed it a bit:
Code: Select all
using namespace std;
void encryptionEngine(char* input)
{
//int lineLength = strlen(input);
string line;
line = input;
if(ifstream("encodedMessage.txt"))
{
ofstream output("encodedMessage.txt");
output<<line<<endl;
output.close();
}
else
{
ofstream output("decodedMessage.txt");
output<<line<<endl;
output.close();
}
}
This seems to write it to the correct files.
Now, how do I implement ceaser cipher?
rustypup
Registered User
Posts: 8872 Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:
Post
by rustypup » 31 Mar 2009, 11:34
DarkRanger wrote: how do I implement ceaser cipher?
you mean
ROT13 ?
Most people would sooner die than think; in fact, they do so - Bertrand Russel
DarkRanger
Registered User
Posts: 8346 Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:
Post
by DarkRanger » 31 Mar 2009, 11:52
Yay!! It works.
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
using namespace std;
int steps = 3;
string mode;
void encryptionEngine(char* input)
{
string line;
line = input;
int lineLength = line.length();
if(mode == "d")
{
ofstream output("decodedMessage.txt");
for(int i = 0; i < lineLength; i++)
{
input[i] = int(input[i]) - steps;
}
output<<input;
output.close();
cout<<"Message succesfully decoded. Output saved to decodeMessage.txt"<<endl;
}
else
{
ofstream output("encodedMessage.txt");
for(int i = 0; i < lineLength; i++)
{
input[i] = int(input[i]) + steps;
}
output<<input;
output.close();
cout<<"Message succesfully encoded. Output saved to encodeMessage.txt"<<endl;
}
}
int main(int argc, char** argv)
{
if(argc < 3)
{
cout<<"too few arguments here"<<endl;
cout<<"USAGE: ./cos121_prac2_task1 filename mode steps"<<endl;
}
else if(argc > 4)
{
cout<<"too many arguments here"<<endl;
cout<<"USAGE: ./cos121_prac2_task1 filename mode steps"<<endl;
}
else
{
string inputFile = argv[1];
ifstream input(argv[1]);
mode = argv[2];
if(argc == 4)
steps = atoi(argv[3]);
if(!input)
cout<<"FILE NOT FOUND : " + inputFile<<endl;
else if(mode != "e" && mode != "d")
cout<<"INCORRECT MODE : Only e or d accepted."<<endl;
else
{
if(mode == "e")
ofstream out("encodedMessage.txt");
else
ofstream out("decodedMessage.txt");
string line;
getline(input,line);
char* lineChar;
lineChar = new char[line.length() + 1];
strcpy(lineChar, line.c_str());
encryptionEngine(lineChar);
}
}
return 0;
}
EDIT: Changed it a bit again.
Also, Rusty, NO idea what it is called in the real world, but the Prac specs call it Ceaser Cipher.