Compile Error

I keep getting the error "Control may reach end of non-void function". Keep in mind I haven't implemented the function when the bool variable is false yet.


#include "utility.h"
#include "caesar.h"
#include "vigenere.h"
#include <iostream>



string vigenereCipher(string original, string keyword, bool encrypt) {
// TODO: implement

// returning a string to avoid compile error

if (encrypt){
keyword=toUpperCase(keyword);
keyword=removeNonAlphas(keyword);
char firstCapitalLetter='A';
string ciphertext="";
int j=0;

for (int i=0; i<original.size(); i++) {
if ((original[i] >=65 && original[i] <=90) || (original[i] >= 97 && original[i] <= 122)) {
ciphertext=ciphertext + shiftAlphaCharacter(original[i], (keyword[j]-firstCapitalLetter));

if (j==(keyword.size()-1)) {
j=0;
}
else j++;
}
else ciphertext=ciphertext + original[i];
}

return ciphertext;
}



}
Last edited on
> "Control may reach end of non-void function"
> I haven't implemented the function when the bool variable is false
that's what's telling you
Hello Slasher320,

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

You may think that this bit of code is where your error is, but it may not be. It is very helpful to post the whole code because the error may start somewhere you are not thinking about. This also includes any header files that you wrote.

Also it is best to include the complete error message. Others may and will see things in the error message that you are not use to at the moment.

Tip: you defined string ciphertext=""; the (="") is not necessary as the string when defined is empty and needs no initialization.

Another thing at the top of your program sometimes the order of the include files does make a difference. In this case "iostream" should be first so it will cover the header files that you do include. And if you put header files like "iostream", "string" and others in your header files, the ones that are surrounded by double quotes, they should not be there.

Since it is not there I will not mention it, but you will have a problem when you say string ciphertext; because it will need to be std::string ciphertext; to work properly. You will also need to include the header file "<string>" after "<iostream>".

Hope that helps,

Andy
Hello Slasher320,

A properly formatted and indented code:
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
#include <iostream>
#include <string>  // <--- Added.

//#include "utility.h"
//#include "caesar.h"
//#include "vigenere.h"

std::string vigenereCipher(std::string original, std::string keyword, bool encrypt)
{
	// TODO: implement

	// returning a std::string to avoid compile error

	if (encrypt)
	{
		keyword = toUpperCase(keyword);
		keyword = removeNonAlphas(keyword);
		char firstCapitalLetter = 'A';
		std::string ciphertext;  // <--- Changed.

		int j = 0;

		for (int i = 0; i < original.size(); i++)
		{
			if ((original[i] >= 65 && original[i] <= 90) || (original[i] >= 97 && original[i] <= 122)) {
				ciphertext = ciphertext + shiftAlphaCharacter(original[i], (keyword[j] - firstCapitalLetter));

				if (j == (keyword.size() - 1))
				{
					j = 0;
				}
				else j++;
			}
			else ciphertext = ciphertext + original[i];
		}

		return ciphertext;
	}
}

int main()
{

}

After seeing ne555 response I realized that the only return statement is within the if statement, but if the if statement is bypassed because the condition is false the is no return statement to satisfy the function.

Also notice how I did the "#includes" at the top of the program. The extra blank lines are to make it easier to understand and follow. not all are needed, but help.

Hope that helps,

Andy
Topic archived. No new replies allowed.