Caesar Cipher Class

So my course just started teaching classes and our first assignment was to do a ceaser cipher. The goal is to take a int value to shift a given input (This is what the program should do: http://www.braingle.com/brainteasers/codes/caesar.php ). I'm getting several strange errors that want me to put in semi colons in weird places and say that I don't have return types when as far as I can tell I do. I've included the header, class, and main files, and then the error output. Any help would be very appreciated. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Caesar.h
#pragma once
#ifndef CAESAR
#define CAESAR

using namespace System;

class Caesar{
public:
	Caesar();				//default constructor, sets shift data to 0
	Caesar(int overShift);	//overload constructor, sets shift data to the value of its argument mod 26
	string encode(string);	//encode method, takes a string and returns a string encoded
	string decode(string);	//decode method, takes a string and returns a string decoded

private:
	int shift;				//private data member shift

};
#endif 


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Caesar.cpp
#include "stdafx.h"
#include <iostream>
#include "Caesar.h"

using namespace System;
using namespace std;

//the default constructor
Caesar::Caesar(){
	shift=0;
}

//overload constructor
Caesar::Caesar(int overShift){
	shift=overShift%26;
}

//encode method
string Caesar::encode(string words){
	char wordChar[]=words; 
	for(int i=0; i<wordChar.length();i++)
	{
		if(wordChar[i]>=65 && wordChar[i]<=90)
		{ //if character is an uppercase letter, encode it
			wordChar[i]+=shift;
			if(wordChar[i]>90)
			{ //if character went over 90
				wordChar[i]=(wordChar[i]-90)+65;
			}
		}
		else if(wordChar[i]>=97 && wordChar[i]<=122)
		{ //else if character is a lowercase letter, encode it
			wordChar[i]+=shift;
			if(wordChar[i]>122)
			{ //if character went over 90
				wordChar[i]=(wordChar[i]-122)+97;
			}
		}
		else
		{ //character is a non letter, transfer as is
			
		}
	}
	return wordChar;
}

//decode method
string Caesar::decode(string words){
	char wordChar[]=words;
	for(int i=0; i<wordChar.length();i++)
	{
		if(wordChar[i]>=65 && wordChar[i]<=90)
		{ //if character is an uppercase letter, encode it
			wordChar[i]-=shift;
			if(wordChar[i]<65)
			{ //if character went under 65
				wordChar[i]=(65-wordChar[i])+65;
			}
		}
		else if(wordChar[i]>=97 && wordChar[i]<=122)
		{ //else if character is a lowercase letter, encode it
			wordChar[i]+=shift;
			if(wordChar[i]>122)
			{ //if character went over 90
				wordChar[i]=(122-wordChar[i])+97;
			}
		else
		{ //character is a non letter, transfer as is

		}
	}	
		return wordChar;
}


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//main

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "Caesar.h"

using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{

	string wordsToEncode[100];
	string wordsToDecode[100];
	string encodedWords[100];
	string decodedWords[100];
	Caesar driver;
	int i=0;
	string task;
	cout<<"Would you like to encode (enter 'E'), decode (enter 'D'), or quit (enter 'Q')?"<<endl;
	cin>>task;
	while(true)
	{
		if(task=="E")
		{ //encode words
			cout<<"Input the words to encode, max 100, end with #"<<endl;
			while(i<100)
			{
				cin>>wordsToEncode[i];
				encodedWords=driver.encode(wordsToEncode[i]);
				if(i==0)
				{
					cout<<"Words Encoded:";
				}
				cout<<encodedWords[i];
				i++;
			}
	
		}
		if(task=="D")
		{ //decode words

		}
		if(task=="Q")
		{ //quit
			break;
		}
		else
		{ //invalid input
			continue;
		}
		cout<<"Would you like to encode (enter 'E'), decode (enter 'D'), or quit (enter 'Q')?"<<endl;
		cin>>task;
	}

    return 0;
}



1>------ Build started: Project: BrownKHW3, Configuration: Debug Win32 ------
1>  BrownKHW3.cpp
1>Caesar.h(11): error C2146: syntax error : missing ';' before identifier 'encode'
1>Caesar.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(11): error C2061: syntax error : identifier 'string'
1>Caesar.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(11): warning C4183: 'encode': missing return type; assumed to be a member function returning 'int'
1>Caesar.h(12): error C2146: syntax error : missing ';' before identifier 'decode'
1>Caesar.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(12): error C2061: syntax error : identifier 'string'
1>Caesar.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(12): warning C4183: 'decode': missing return type; assumed to be a member function returning 'int'
1>BrownKHW3.cpp(32): error C2660: 'Caesar::encode' : function does not take 1 arguments
1>  Caesar.cpp
1>Caesar.h(11): error C2146: syntax error : missing ';' before identifier 'encode'
1>Caesar.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(11): error C2061: syntax error : identifier 'string'
1>Caesar.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(11): warning C4183: 'encode': missing return type; assumed to be a member function returning 'int'
1>Caesar.h(12): error C2146: syntax error : missing ';' before identifier 'decode'
1>Caesar.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(12): error C2061: syntax error : identifier 'string'
1>Caesar.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Caesar.h(12): warning C4183: 'decode': missing return type; assumed to be a member function returning 'int'
1>Caesar.cpp(19): error C2511: 'std::string Caesar::encode(std::string)' : overloaded member function not found in 'Caesar'
1>          Caesar.h(7) : see declaration of 'Caesar'
1>Caesar.cpp(48): error C2511: 'std::string Caesar::decode(std::string)' : overloaded member function not found in 'Caesar'
1>          Caesar.h(7) : see declaration of 'Caesar'
1>Caesar.cpp(74): fatal error C1004: unexpected end-of-file found
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


There is no namespace called System in C++, and line 12 looks like C# or something.

The correct declaration for main in C++ is:
int main()
or
int main(int, char**)
OP might be doing C++/CLI? which is a different language from C++.
In all the examples my professor has given me, they say both
1
2
using namespace System;
using namespace std;

and the deceleration main is the default for a CLR Console Application
 
int main(array<System::String ^> ^args)
CLR Console Application


This is C++/CLI.

If you are coding normal C++, you don't want this. Instead you want an "Empty Project" or a "Win32 Console Application"


Though if your professor is doing using namespace System; you might be coding C++/CLI... in which case I don't know if many people here will be able to help you... as many of us are unfamiliar with it.

It might be worth it to ask your professor if you are coding C++ or C++/CLI. It's probably good to know what it is he's actually teaching you.
Last edited on
My professor has been using and having us make things in CLR console application, so I'd assume we're using C++/CLI. Thanks for the help, and if anyone out there is good at C++/CLI please let me know.
Topic archived. No new replies allowed.