C++ error... Pffft...

Get help on programming - C++, Java, Delphi, etc.
Post Reply
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:

C++ error... Pffft...

Post by DarkRanger »

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...
Image
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: C++ error... Pffft...

Post by Kronos »

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?
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C++ error... Pffft...

Post by rustypup »

<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:

Re: C++ error... Pffft...

Post by DarkRanger »

I need to output it to the file specified in the main method.

ofstream out("encodedMessage.txt")

OR

ofstream out("decodedMessage.txt")
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C++ error... Pffft...

Post by rustypup »

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:

Re: C++ error... Pffft...

Post by DarkRanger »

Should I declare out outside the main method?
Image
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:

Re: C++ error... Pffft...

Post by DarkRanger »

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
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C++ error... Pffft...

Post by rustypup »

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:

Re: C++ error... Pffft...

Post by DarkRanger »

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?
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C++ error... Pffft...

Post by rustypup »

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:

Re: C++ error... Pffft...

Post by DarkRanger »

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.
Image
Post Reply