0

This i the code i have used and for some reason its saying there are undeclared identifiers

#include <string>
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
    bool Firstboot = true;
    EntryPoint();
    cout << "Hello World!" << endl;
    return 0;
}

int MainMenu()
{
    FormatedOut("MainMenu", "Menu1", "Menu1");
}
int Menu1()
{
    FormatedOut("Menu1", "SubMenuOption1", "SubMenuOption1");
}


int EntryPoint() {
    FormatedOut("MainMenu", "Menu1", "Menu1");

    string option;
    cin >> option;
    if (option == "1")
    {
        Menu1();
    }
}

int FormatedOut(string MenuName, string FirstOption, string FirstOptionTarget) {
    cout << "----------------";
    cout << "| " + MenuName;
    cout << "|---------------";
    cout << "|              ";
    cout << "|  " + FirstOption;
    EntryPoint();
}

I have codded this in C++ on Visual Studio 2017

The errors i have been getting for the past half an hour

C3861   'EntryPoint': identifier not found  8
C2065   'cout': undeclared identifier   9
C2065   'endl': undeclared identifier   9
C3861   'FormatedOut': identifier not found 15
C3861   'FormatedOut': identifier not found 19
C3861   'FormatedOut': identifier not found 24    
C2065   'string': undeclared identifier 26
C2065   'option': undeclared identifier 26
C2065   'cin': undeclared identifier    27
C2065   'option': undeclared identifier 27
C2065   'option': undeclared identifier 28
C2065   'string': undeclared identifier 34

Any help that you can give me will be greatly appreciated

  • 1
    You should probably [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or simply go back to the books, tutorials or class-notes you already have. All of them *should* tell you that things needs to be declared before you can use them. – Some programmer dude Aug 01 '18 at 10:09
  • In C++, code is read sequentially, from top to bottom. Identifiers need to be declared BEFORE they are used for the first time. The first usage of `EntryPoint()` is in `main()`, and it is not declared before that, hence the error. The fact that `EntryPoint()` is defined later in the file is irrelevant - the compiler hasn't seen it when parsing the content of the `main()` function. Same for other functions. (I suspect the code you have posted differs from the code that produces the errors since, as shown, concerns with `cout` and `cin` would not be diagnosed). – Peter Aug 01 '18 at 10:25
  • Also check out the infinite recursion you have going on there. Entrypoint calls FormattedOut (side note: which doesn't return anything despite having a non-void return type) and FormattedOut calls EntryPoint. No wonder the linker is unhappy. – Spoonless Aug 01 '18 at 10:41

3 Answers3

0

You need to write function prototypes for the functions that appear after they are called in your source file.

I.e. before main, write

int EntryPoint();

and so on. You'll also probably find that stdafx.h needs to be the first line of the source file too, depending on your compiler settings (that's the name of the precompiled header that MSVC uses).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

The compiler reads from top to bottom just like you. When it reaches this line: EntryPoint(); it has never seen a function called entry point, so it complains. You need to learn about function prototyping.

Put a prototype of your function before the use:

int EntryPoint();
...
int main()
{
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int EntryPoint();
int FormatedOut(string MenuName, string FirstOption, string FirstOptionTarget);
add this before main() it should work
Subodh
  • 40
  • 5