Visual studio Error help

On visual studio, I keep on getting a error for the "void convert" part of my code, i have changed it a couple of other times and I just keep getting the same error. over and over. Is there any way i can change this?

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;

int main(){
string s;

void convert(string& s);
{
for (int i = 0; i < s.length(); i++) {
s[i] = toupper(s[i]);
}
}

std::cout << "This is the test message.It has a couple sentences in it.";
convert(s);
std::cout << s << endl;
return 0;
}
You can't define one function directly inside another.

Wrong:
1
2
3
4
int main(){
    void foo(){
    }
}

Right:
1
2
3
4
5
void foo(){
}

int main(){
}
1
2
3
4
5
void foo(){
}

int main(){
}
and I just keep getting the same error.


Next time please cut and paste the error into your post. Most of us have trouble reading your mind from so far away.


BattleCrazy, in case you missed the detail from previous posts, after you move "convert" out from inside the "main" function, you have one more problem:

The ';' character at the end of "convert(string &s)" should not be there when the code defines the function.

What you've typed out is the declaration. They are two different but related notions.

The declaration is just the signature, like

void convert( string & );

The declaration, in case that's not clear, is like an entry in a table of contents. More specifically, it "declares" the function will come later in the code (or in another CPP file, called a translation unit, or compilation unit).

The definition includes the code itself, in brackets, like this:

1
2
3
4
5
6
7
void convert( string & s)
{
for (int i = 0; i < s.length(); i++) 
  {
   s[i] = toupper(s[i]);
  }
}


Note that there's no ';' in the first line of that code.

Now, if you put the definition BELOW the main function, you'll get an error when you call convert, but that's simple to deal with.

You need to place a declaration ABOVE the main function.

C/C++ are "one pass compilers", and so such declarations (sometimes called forward declarations) are required to inform the compiler this function will be given later.

You'll get the hang of it as you practice.
Topic archived. No new replies allowed.