Why doesn't a void function print anything in C++? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Im just starting to learn C++. While writing a simple calculator, I found that when calling a void function, it doesn't print when it should. I have simplified the code to better represent my problem.
#include <iostream>
using namespace std;
void helloguys()
{
cout << "test";
}
int main()
{
cout << "This is a ";
void helloguys();
cout << " guys.";
}
I expected to get "This is a test guys.", but all I got is "This is a guys."
The compiler never reported any kind of problems.

Try removing void before your function call:
int main()
{
cout << "This is a ";
helloguys();
cout << " guys.";
}
helloguys(); will call the function.
void helloguys(); is a function prototype, not a call.

Related

Using #define the wrong way round works for calling functions. Why? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
C and C++ documentation for the use of #define suggests that this should not work as I am using the define to replace the text MyFunc() with _myfunc(), which is a function that does not exist:
#define MyFunc _myfunc
void MyFunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc();
return 0;
}
My guess is that the compiler is being clever. It knows that _myfunc() does not exists and therefore does not replace the text and simple uses MyFunc().
I can't find any documentation to support this theory. Does anyone know whether this is correct?
After the preprocessor has run, your program will look like:
void _myfunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc(); // #1
return 0;
}
Ignoring other errors here (lack of includes, ...), the compiler can find _myfunc declared and defined, so naturally it will be found by overload resolution at the call site #1.

printf working fine when kept outside of for loop but causes some error while running when kept in the for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.

mt19937 random number range - weird output 00A8106E [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a little game in c++, and therefore I need a function to return random numbers in a specific range.
Most of the answers I found were similar to this one https://stackoverflow.com/a/19728404/5780938, and I think this is the solution I'm looking for.
To test if the function does, what I want it to, I tried outputting the results in several different ways.
At the moment my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <random>
int zufälligeZahl();
int main()
{
using std::cin;
using std::cout;
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
return 0;
}
int zufälligeZahl()
{
std::random_device rd;
std::mt19937 zGenerator(rd());
std::uniform_int_distribution<int> uni(1, 13);
int random_integer = uni(zGenerator);
return random_integer;
}
I've tried this in many different ways, but no matter what I do, it doesn't work. Either the output is something like 00A8106E, or I don't get any output at all.
I'm using Visual Studio Community 2015.
You are not calling the function zufälligeZahl, you are printing out the address of the function.
Fix your code by actually calling the function:
cout << zufälligeZahl() << "\n";
You forgot the parentheses.

Calling bool function inside if parameters [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
so I have a C++ bool function that I've written that looks like this:
bool EligibileForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
bool eligible = false;
if (CompanyUsed==CompanySubscribed)
eligible = true;
return (eligible);
}
Now in my main() this function is called as the only parameter for an if statement:
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << "\nAfter your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ".\n";
}
The main function was written by my teacher and we wrote the other functions, so this if statement isn't supposed to be changed.
So I understand what the if statement is trying to accomplish, by basically saying "if (true) do this..." since the EligibleForDiscount will return a boolean value.
However, g++ is giving me an error with the if statement, telling me that EligibleForDiscount is not declared in this scope.
But I'm not trying to use it as a value but as a call to a function.
It may be because of two reasons:
You misspelled the function name when called : if (EligibleForDiscount(CompanyUsed, CompanySubscribed)) should be written like your implementation of the function, which is EligibileForDiscount.
This can happen if you forgot to declare the prototype of the function, which is an indicator to the program that you're going to use that function. You simply need to write somewhere before you use the function bool EligibileForDiscount(const char , const char)
One of these should work!
Because : EligibileForDiscount != EligibleForDiscount with an additional "i", just a typo.
p.s. you can write EligibleForDiscount like this:
bool EligibleForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
return CompanyUsed==CompanySubscribed;
}

Why does writing into my string not work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
The following Code is not working. I get an error at the command cin >> h. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
string h = " ";
cout << "hi" << endl;
cin >> h;
cout << h << endl;
system("pause");
return 0;
}
Random guessing:
You forgot to #include <string>
You forgot to include <string> and C++ punished you for that.
Ah, but every man and his dog should know that by not including <string>, you were using the default >> operator, that has well know issues with strings.
C++'s "leave the progammers free to shoot themselves in the foot" philosophy at its best.
C++ lore tells the unfortunate wandereds should use getline instead of cin >>, but there have been heated debates among scholars on this fine doctrine point.