Error Code Debugging for C++ CodeBlocks - c++

I keep getting an error message in CodeBlocks it says:
Error: 'Addnumbers' was not declared in this scope
I just started with C++ and have no idea what this means. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fnum;
int snum;
cout << "Enter First number" << endl;
cin >> fnum;
cout << "Enter Second Number" << endl;
cin >> snum;
Addnumbers (fnum, snum);
return 0;
}
int Addnumbers(int fnum, int snum){
int ans = fnum+snum;
return ans;
}

You need to declare the function before it's used:
int Addnumbers(int fnum, int snum);
int main()
{
}
int Addnumbers(int fnum, int snum)
{
// ...
}
The first declaration is what is called a prototype, and tells the compiler that somewhere there is a function named AddNumbers with the specified arguments and return type. Then you can have the definition anywhere, even in another source file.
In C++ (as well as in C or other languages base on C) everything must be declared before it it used. That's how the compiler will know that stuff exists.

You need to either move Addnumbers before main, or to do a forward declaration:
#include <iostream>
using namespace std;
int Addnumbers(int fnum, int snum);
int main()
{

Related

Global Array in C++

Can anybody tell me what is wrong in the following code when I initialize a global array and want to print its value outside main() function
#include <iostream>
using namespace std;
int global_array[5] = {10,20,30,40,50};
cout << global_array[2];
int main()
{
cout << "Hello World!" ;
}
The error keep popping is
error: 'cout' does not name a type|
The statement cout << global_array[2]; is not a declaration (it is an expression). Only declarations are allowed outside of functions.
So, if you want to print anything outside of main function, you can only do so by having the expression within another function.
I think the problem is that the code you have that does the printing is outside of any function. Statements in C++ need to be inside a function. For example:
#include <iostream>
using namespace std;
void hello();
int global_array[5] = {10,20,30,40,50};
void hello()
{
cout << global_array[2];
}
int main()
{
hello();
cout << "Hello World!" ;
}
Before asking a question, you can search: ‘cout’ does not name a type
Thanks you.
if you want to call it from outside the main it should be in a function something like this
#include <iostream>
using namespace std;
int global_array[5] = {10,20,30,40,50};
int pre()
{
cout << global_array[2];
return 0;
}
int x = pre();
int main()
{
cout<<"Hello World";
return 0;
}
as i mentioned it on comment it can be done via c++ classes.
#include <iostream>
int global_array[5] = { 10,20,30,40,50 };
struct foo
{
foo()
{
std::cout << global_array[2] << std::endl;
}
};
foo f;
int main()
{
}

error: '' was not declared in this scope

I was trying to do a simple program to learn a bit more about reference passage, pointers and how to use this 2 on structs in C++ and I got a few questions.
I got an error on the code below "error: 'totalStudent' was not declared in this scope" and my question is, how should I declare the "totalStudent".
#include <iostream>
using namespace std;
struct test{
char name[30];
int age;
};
void addStudent(struct test *ptrTest,int *totalStudent){
for(int i=0;i<2;i++){
cout<<"\nInsert the name: ";
cin.sync();
cin.getline(ptrTest->name,sizeof(ptrTest->name));
cout<<"\nInsert the age: ";
cin.sync();
cin>>ptrTest->age;
*totalStudent+=1;
}
}
void showStudent(struct test *ptrTest,int totalStudent){
for(int i=0;i<totalStudent;i++){
cout<<"\nName: "<<ptrTest->name;
cout<<"\nAge: "<<ptrTest->age;
}
};
int main()
{
struct test t;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
I can't use pointers and reference passages with structures very well. I can only use them when I'm not using structs.
You forgot to declare this variable in the scope of main:
int main()
{
struct test t;
// LIKE THIS
int totalStudent;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}

Obtain variables in function from other function C++

I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}

Redeclaring a function inside a function

I stumbled across a strange c++ snippet. I consider this as bad code. Why would someone repeat the function declaration inside a function? It even compiles when changing the type signature to unsigned int sum(int, int) producing the expected result 4294967294j. Why does this even compile?
#include <iostream>
#include <typeinfo>
using namespace std;
int sum(int a, int b){
return a + b;
}
int main()
{
int sum(int, int); // redeclaring sum???
int a = -1;
auto result = sum(a, a);
cout << result << typeid(result).name() << endl;
}
Edit: It compiles for me... but is it valid C++ code? If not why does the compiler (mingw 4.8.1) allow it?
Sometimes there is a sense to redeclare a function inside a block scope. For example if you want to set a default argument. Consider the following code
#include <typeinfo>
using namespace std;
int sum(int a, int b){
return a + b;
}
int main()
{
int sum(int, int = -1 ); // redeclaring sum???
int a = -1;
auto result = sum(a, a);
cout << result << typeid(result).name() << endl;
result = sum(a);
cout << result << typeid(result).name() << endl;
}
Another case is when you want to call a concrete function from a set of overloaded functions. Consider the following example
#include <iostream>
void g( int ) { std::cout << "g( int )" << std::endl; }
void g( short ) { std::cout << "g( short )" << std::endl; }
int main()
{
char c = 'c';
g( c );
{
void g( short );
g( c );
}
}
If that's the actual code, there's no reason to do it.
If the function sum is defined somewhere else though, the declaration inside main makes it accessible only inside main. You can't use it anywhere else in that translation unit (unless of course you declare it). So it's a sort of limiting visibility to where it's needed, but, granted, it's not very readable.
Regarding changing the return type - that's illegal. You're not seeing any issues with unsigned int, but if you try
char sum(int, int); // redeclaring sum???
you'll see there's a problem there.

Why wouldn't the following program compile?

Why wouldn't the following code compile? Basically what is not right in the following code? I'm assuming that declaring the same variable twice without assigning any value would be the problem.
#include <iostream>
using namespace std; int foo() { return 1; }
int main() { int a; int a; cout << foo() << endl; return 0;}
remove one "int a;" declaration. Even if it was possible, there is no reason to do that.