-1

How do I get rid of these errors?Its supposed to calculate your age based on your birth year. This code was made by me and my friend,both of us are begginers in programming.

    #include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofargs, char* pszArgs[]);

int CurrentYear = 2020;

int BirthYear;

cout << "Enter your birth year: "

cin  >> BirthYear;
if (nBirthYear <= 0)
{
   cout << "Birth year cannot be processed" <<endl;
   BirthYear = 2020
}
else (BirthYear > 2020)
{
  cout << "Birth year cannot be processed "  <<endl;
  BirthYear = 2020
}
if (nBirthYear < 2020)
    (cout << "Your age is : " << CurrentYear - BirthYear <<endl;
)
cout << "Press Enter to continue..." << endl;
 cin.ignore(10, '\n');
 cin.get();
 return 0;

these are the errors

||=== Build: Debug in Actualagefinder (compiler: GNU GCC Compiler) ===|
H:\c++\Actualagefinder\main.cpp|13|error: 'cout' does not name a type|
H:\c++\Actualagefinder\main.cpp|16|error: expected unqualified-id before 'if'|
H:\c++\Actualagefinder\main.cpp|21|error: expected unqualified-id before 'else'|
H:\c++\Actualagefinder\main.cpp|26|error: expected unqualified-id before 'if'|
H:\c++\Actualagefinder\main.cpp|28|error: expected unqualified-id before ')' token|
H:\c++\Actualagefinder\main.cpp|30|error: 'cin' does not name a type; did you mean 'main'?|
H:\c++\Actualagefinder\main.cpp|31|error: 'cin' does not name a type; did you mean 'main'?|
H:\c++\Actualagefinder\main.cpp|32|error: expected unqualified-id before 'return'|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

   
  • What errors? Please add the text of the errors to the question. – cigien Aug 18 '20 at 14:42
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions, for example to include the full and complete errors you get and if they're build errors then also add comments in the code where you get the errors. – Some programmer dude Aug 18 '20 at 14:45
  • As for your problems, you should probably get [some decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Aug 18 '20 at 14:46
  • 1
    You're missing a `;` after the cout statement. – cigien Aug 18 '20 at 14:48
  • @cigien is right. And not only in the line he mentioned. Look for missing ';' – Planck Constant Aug 18 '20 at 15:02
  • 2
    My advice is to not worry about missing semicolons at the moment, or the miss-spelled variable names, as those are the *least* of your problems. Instead go back to whatever books or tutorials you have access to, and look at the very first "hello world" program, and how it defines the `main` function. – Some programmer dude Aug 18 '20 at 15:07

2 Answers2

2

In C++, all executed code has to be within a function.
I.e. it should look like

int main(int nNumberofargs, char* pszArgs[])
{
    /* code */
}

yours starts with a prototype, followed by orphaned code.

int main(int nNumberofargs, char* pszArgs[]);
/* code */

That is why the compiler complains that it expects a known type (for a declaration) where you have your code line starting with cout (which you not unreasonably expect to be already declared).

Other lines, which are not necessarily statements, can be local or global variable definititions, the latter being possible outside of functions. That is why the compiler does not complain about them.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

The first issue you need to fix is the int main() declaration. By putting the semi-colon: int main(int nNumberofargs, char* pszArgs[]); you are telling the compiler that it has reached the end of the command, without doing anything. To fix this, like in your if statements, you must encapsulate everything in the after the declaration in curly braces.

Thus, your program becomes:

  #include <cstdio>
  #include <cstdlib>
  #include <iostream>
      int main(int nNumberofargs, char* pszArgs[])
      {
       // Put your stuff in curly braces
      }

Your next bug is in your if statements. To put it simply, if statements don't work like that in C++.

Here is what if statements should be like:

if(something){
  // Do something here
}
else if(something else){ // If there are more than one possible condition, use else if
  // Do something else
}
else{ // If none other condition is satisfied
// Do something 
}

Notice that you cannot have if twice (you can if you have nested if statements (like if statement inside if statement)

Those are the main problems in your program. The rest are small errors. For example, in many places a ; is missing. In other places, you call on undeclared variables:

if (nBirthYear < 2020) // This was probably a typo, nBirthYear is not declared

And in one place you have an ( which is a typo: (cout << "Your age is : " << CurrentYear - BirthYear <<endl;

Also, there is some unnecessary code, like the first two lines (they are not being used).

Nevertheless, here is a working version of your program:

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofargs, char* pszArgs[]){

int CurrentYear = 2020;

int BirthYear;

cout << "Enter your birth year: " << endl;

cin  >> BirthYear;

if (BirthYear <= 0)
{
   cout << "Birth year cannot be processed" <<endl;
   BirthYear = 2020;
}
else if(BirthYear > 2020)
{
  cout << "Birth year cannot be processed "  <<endl;
  BirthYear = 2020;
}
else if (BirthYear < 2020){
    cout << "Your age is : " << CurrentYear - BirthYear << endl;
}
cout << "Press Enter to continue..." << endl;
 cin.ignore(10, '\n');
 cin.get();
 return 0;
}

As the comments said, it would be recommended to look again at whatever resources that you used to make this program, you must have your fundamentals down. Make sure that you know what every part of your programs do, and put comments to remind yourself in the future. I'll try to make this answer concise to help you understand, but you really should go back through what you've read.

Additionally, make sure that you're resources are relatively new, because a lot has changed in C++ since it was created.

Sony
  • 92
  • 4
  • 9