Error: Expected initializer before void
Mar 11, 2017 at 8:35am UTC
Hey guys today, after a long time I was coding in C++ and I got an Error:
Expected initializer before void
Can anyone help?
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
#include <iostream>
#include <string>
using namespace std;
string input;
int count = 0, length
void caesarCipher (string phrase)
{
length = (int ) phrase.length()
for (count = 0; count < length; count++)
{
if (isalpha(phrase[count]))
{
phrase[count] = tolower(phrase[count]);
for (i = 0; i < 13; i++)
{
if (phrase[count] == 'z' )
phrase[count] = 'a' ;
else
phrase[count]++;
}
}
}
cout << "Results: " << phrase << endl;
}
int main()
{
cout << "Enter your phrase: " ;
cin >> input;
caesarCipher(input);
return 0;
}
Mar 11, 2017 at 9:36am UTC
int count = 0, length
Missing a semi-colon.
Mar 11, 2017 at 9:38am UTC
You need a semicolon at the end of line 7.
But should avoid global variables, and always declare one variable per line of code. Delay declaration until you have a sensible value to assign to the variable.
I like to put function declarations before main, with the definitions after:
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
#include <iostream>
#include <string>
using namespace std; // avoid this
string input;
int count = 0, length
void caesarCipher (std::string& phrase);
int main()
{
std::string input;
std::cout << "Enter your phrase: " ;
std:: cin >> input;
caesarCipher(input);
return 0;
}
void caesarCipher (const string& phrase)
{
std::size_t length = (int ) phrase.length()
std::string OutputPhrase;
for (std::size_t count = 0; count < length; count++)
{
if (isalpha(phrase[count]))
{
OutputPhrase[count] = tolower(phrase[count]);
for (i = 0; i < 13; i++)
{
if (phrase[count] == 'z' )
OutputPhrase[count] = 'a' ;
else
OutputPhrase[count]++;
}
}
}
cout << "Results: " << OutputPhrase << endl;
}
I would also avoid changing the original phrase, I made a new variable and changed that instead.
Topic archived. No new replies allowed.