I've read that an #include header.h is a preprocessor (because of #), which means it gets processed before compilation.
Is that why my code can't run? Because I'm trying to make an if statement in main with my function from my header(that takes a parameter) and it won't work.
Source.cpp
#include <iostream>
#include "Header.h"
using namespace std;
int main(){
test(46);
if (test() > 30){
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
Header.h
using namespace std;
int test(int x){
return x;
}
That isn't the problem. I suspect you might be getting a compiler error message (or linker error) because you have declared test(int x) with an integer parameter and then you call it with no parameter, e.g.: test().
I've modified your code to include an integer result:
int main(){
int result = test(46); // Save the result of calling the function
if (result > 30){ // Test the value of the result
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
The test function in Header.h file takes a int as parameter.But in your code you lose it.Pass a int to test function like this.
if (test(42) > 30)
You will get the output: great.
Related
Any cout statement inside main function is not printing any value during debugging but cout inside other functions are printing values during function call (I use vscode )
#include<iostream>
using namespace std;
int main()
{
int a;
a=9;
if(a==9)
{
cout<<"hello";}
return 0;
}
when this is debugged placing a breakpoint on the first line of main()
"hello" is not printed in the debug console.
#include <iostream>
using namespace std;
void fun(int n)
{
if (n > 0)
{
cout << n << endl;
fun(n - 1);
}
}
int main()
{
int x = 3;
fun(3);
cout<<x;
return 0;
}
but when this is debugged by placing a breakpoint in the first line of main()
values are printed like
3
2
1
std::cout is buffered. Try to use std::flush or std::endl.
Also you can try to use std::cerr instead std::cout.
enter image description here
I am supposed to call the function randint to my main function but for some reason it doesn't work. I have 3 files: randint.cpp, randint.h, and main.cpp. And I am supposed to call the function from randint.cpp. I am not sure if I am supposed to just declare the function in the header file and and write the definition in the cpp file.
main.cpp
#include "std_lab_facilities_5.h"
#include "randint.cpp"
#include "randint.h"
int main()
try {
int x = randint();
cout << x;
return 0;
}
catch (exception& e) {
cerr << "error: " <<e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}
randint.cpp
#include "randint.h"
#include <chrono>
using namespace std::chrono;
//linear congruential pseudorandom number generator
int randint() {
//use the clock for an initial pseudorandom number
static long x = time_point_cast<microseconds>(system_clock::now()).time_since_epoch().count();
//calculate the next pseudorandom number
// parameters from glibc(?)
x = (((1103515245L * int (x)) & 0x7fffffff) + 12345)& 0x7fffffff;
return x;
}
randint.h
int randint();
In the .H file inside the class you want
public:
int randint();
and in the .CPP that you want to call the function you need:
randint randint; //<--- Telling the class in this case main that randint exists(second randint can be whatever name you prefer)
randint.randint(); //<--- Here you call the function
I would also recommend you to use the class wizard when creating new files.
I'm learning C++ and tutorial asks me to add another project to what I have now.
Also I'm asked to use forward declaration so I can make use of that added file.
Here is my main project:
#include <iostream>
#include "io.cpp"
using namespace std;
int readNumber();
void writeResult(int x);
int main() {
int x = readNumber();
int y = readNumber();
writeResult(x + y);
return 0;
}
here's the added file called io.cpp:
#include <iostream>
using namespace std;
int readNumber() {
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void writeResult(int x) {
cout << "Sum of your numbers is " << x << endl;
}
![And here's a screenshot so you can see what error I'm getting which talks about multiple definition and you can see where those two files are added.
According to the tutorial my code is okay but compiler complains. Why ?]1
In codeblocks, when creating a new class, it should automatically header file. Programming with header files is the best practice out there. Here's the code I tried and it worked, with io.h.
main.cpp
#include <iostream>
#include "io.h"
using namespace std;
io inOut;
int main()
{
int x = inOut.readNumber();
int y = inOut.readNumber();
inOut.writeResult(x + y);
return 0;
}
io.h
#ifndef IO_H
#define IO_H
class io
{
public:
int readNumber();
void writeResult(int);
};
#endif
io.cpp
#include <iostream>
#include "io.h"
using namespace std;
int io::readNumber()
{
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void io::writeResult(int x)
{
cout << "Sum of your numbers is " << x << endl;
}
I used codeblocks to compile the code written above, and it worked perfectly.
Well as turns out when adding more cpps they're not supposed to be #included on the top. That's what makes compiler say that function is being defined multiple times. All I had to do was just get rid off that one line.
Here's my source:
http://www.cplusplus.com/forum/beginner/44651/
I recently started learning C++ but I came across a problem. The program given below is not giving me the desired result as I only see 'Hi' in the result but not what's written in the void function. Please tell me the reason that this is happening along with the solution.
I am using Xcode 6.3.1 and the I have selected the language C++.
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
void ABC ();
return 0;
}
You are redeclaring a void ABC() function inside main(). Just call ABC(); without the void.
You can take a look at this question about declaring a function within the scope of another.
In your code your function call was wrong.
When you call your function you don't need to add the return type:
#include
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC ();
return 0;
}
you need to call your method and not declare it inside main
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
EDIT 1:
Since you started learning C++ i recommend the following recommendations to make sure your code is cleaner. Please note , these are not rules by any mean , but more of best practices and a style of coding.
Use meaningful names for your variables, methods, functions , classes
... So instead of ABC() name it something that if you (or someone
else is reading it) will now what it suppose to do.
When calling methods and functions try to declare them with the
appropriate returning value. Void by definition doesn't return any
value it just process the code inside of it. so your methods/function
should return appropriate values of do what it suppose to.
Here's version 2 of your code with examples of 3 different methods and calls:
#include <iostream>
using namespace std;
int sum;
string MethodReturningString()
{
return "Hey there i am the result of a method call !";
}
int MethodReturningInt()
{
return 5;
}
void CalculateSum(int x,int y)
{
sum=x+y;
}
int main()
{
cout << MethodReturningString() << endl;
cout << MethodReturningInt() << endl;
cout << "Calculating sum:" ;
CalculateSum(5,4);
cout << sum << endl;
return 0;
}
Happy coding
In C++, like pretty much any other language, you do not specify the return type when calling a function. So change the line that reads:
void ABC ();
to:
ABC();
Try this:
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC();
return 0;
}
You should call a function simply by stating its name and adding parentheses.
instead of using void ABC() for calling the function ABC() in main(), use the following code:
#include
void ABC ()
{
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
I'm new to CPP, and I want to know how to run a function that isn't in its scope. I'm used to doing such things in javascript, and I get an error CPP when I try to do that. What I mean is the below:
#include <iostream>
using namespace std;
int tic_h;
int tic_v;
void echo(string e_val){
cout << e_val;
}
void c_mes(){
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
s_v();
}
void s_v(){
echo("Please enter vertical coordinate: ");
cin >> tic_v;
if(tic_v<4&&tic_v>0){
c_mes();
}else{
s_v();
}
}
void s_h(){
echo("Please enter horizontal coordinate: ");
cin >> tic_h;
if(tic_h<4&&tic_h>0){
s_v();
}else{
s_h();
}
}
int main(){
s_h();
return 0;
}
I get this error:
error: 'sv' was not declared in this scope on line 16
How can I make it work?
You should put a function prototype before using the function, for the compiler to know what it is going to be.
Put
void s_v(); // prototype your functions, this is usually done in include files
Right after the #include line.
You'll need to forward declare dostuff, as in the example below.
By doing this you pretty much tell the compiler that the function will be defined else where, but that you'd like to use it.
Excuse the wording, but putting it the way I did is easily comprehensive by a novice programmer.
#include <iostream>
using namespace std;
void dostuff (); // forward declaration
void test(int b){
if(b<11&&b>0){
cout << "Yay!";
}
else{
cout << "The number is not between 1 and 10.";
dostuff();
}
}
void dostuff(){
int numput;
cout << "Please type a number between 1 and 10:";
cin >> numput;
test(numput);
}
int main(){
dostuff();
}
OP just edited the original snippet provided in his question (which the below is a modification off), I'll leave this post the way it is since it explains the situation quite well.
You need to add void s_v(); before the c_mes() function. This is called a function prototype, and it lets the compiler know that that symbol exists and will be implemented later in the code:
#include <iostream>
using namespace std;
int tic_h;
int tic_v;
void s_v();
void echo(string e_val) {
cout << e_val;
}
void c_mes() {
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
s_v();
}
void s_v() {
echo("Please enter vertical coordinate: ");
cin >> tic_v;
if (tic_v < 4 && tic_v > 0) {
c_mes();
} else {
s_v();
}
}
void s_h() {
echo("Please enter horizontal coordinate: ");
cin >> tic_h;
if (tic_h < 4 && tic_h > 0) {
s_v();
} else {
s_h();
}
}
int main() {
s_h();
return 0;
}
Keep in mind that if you ever change the signature for s_v() (that is, add arguments or change the return type), you will need to update the prototype as well.
Declare dostuff somewhere before the void test definitionm e.g on line 3:
void dostuff();
This way you introduce the signature of dostuff function to your program before the function is defined.
In C++ unlike javascript and some other languages, the parser doesn't find all functions then compile the code.
add
void dostuff();
just after the using namespace std; and it will work :)
This is the same error, you use a function before declare it (s_v()), for solve your error you only should create a prototype of s_v():
void s_v(); //at the start of your file
write this
void c_mes(){
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
echo("-----\n");
echo("X|0|X\n");
s_v();
}
after this
void s_h(){
echo("Please enter horizontal coordinate: ");
cin >> tic_h;
if(tic_h<4&&tic_h>0){
s_v();
}else{
s_h();
}
}