xcode is not showing the output - c++

I tried several time to find where is the problem, but I can not find any thing.So, could anyone help me to find the problem and why I can not see a result?
It might seem stupid question, but I new to programming world :)
This is my code :
#include <iostream>
using namespace std;
// There is the declraction of all functions
float max();
float min();
// This is the main program
int main ( int argc, char ** argv )
{
// Here you can find max
max(504.50,70.33);
// Here you can find min
min(55.77, 80.12);
return 0;
}
// This is the max function
int max(float a, float b){
float theMax;
if (a>b) {
theMax = a;
cout <<theMax ;
}
else{
theMax = b;
cout << b;
}
return theMax;
}
// This is the min function
int min( float c, float d){
float theMin;
if (c >d ) {
theMin =c;
cout << theMin;
}
else {
theMin =d;
cout << theMin;
}
return theMin;
}

You're calling std::max and std::min. That's because you wrote using namespace std, and did not declare your own min and max prior to using them. (You did declare two other min and max functions, but those take zero arguments, not two). So, when the compiler sees max(504.50,70.33); the only candidate is std::max.

You declare these overloads:
float max();
float min();
which are functions that take no arguments and return float.
You're calling
max(504.50,70.33);
and
min(55.77, 80.12);
which are functions that takes two doubles and may or may not return anything.
These match std::max and std::min, not the prototypes you declared.
You then define
int min( float c, float d){
which also doesn't match the prototypes you declared.
In other words, these functions are unknown in main, and the functions that are actually called are std::min and std::max.
Don't use using namespace std; - what you save in typing is lost in clarity and debugging.
You should also rename the functions - it's not a good idea to reuse standard library names.

Related

issue with float value printing in cpp

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans;
}
int main(){
int t=320;
cout<<solve(t);
return 0;
}
output:3.204745728
how should I get output as 3.20
You are using cout twice, when you only want to output one thing. You are also outputting the return value of solve when you don't return anything from solve, this explains the extra digits that you see.
Either do the cout in solve or do it in main, don't do it in both places.
First way
#include <iostream>
using namespace std;
void solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans; // print the answer
}
int main(){
int t=320;
solve(t);
return 0;
}
Second way
#include <iostream>
using namespace std;
float solve(int t){
float ans = static_cast<float>(320)/100;
return ans; // return the answer
}
int main(){
int t=320;
cout<<fixed<<setprecision(2)<<solve(t);
return 0;
}
Another issue with your code is that you are passing the parameter 320 to solve, but you aren't using it there. You might want to change that
float ans = static_cast<float>(t)/100;
It's quite common for beginners to get confused between returning a value from a function, and printing a value in a function, but these are quite different things. The first code above prints the value in the function, the second code above returns the value from the function (and prints it in main).

How does the 'max' as a variable work even though normally it is a function?

i'm like 6 months in C++, only practicing algorithm problems, but I know no more than that.
how does this work? declaring max as a variable even though normally it is a function?
shouldn't the computer confuse them?
(or at least I think it's a function since it appears with green on C++)
what I mean is how does the computer know when i'm using max as a variable and when i'm using it as a function?
Thanks!
Scope.
max-the-variable will replace max-the-function or max-the-type if declared in a narrower scope. Compilation will fail if you declare max-the function and max the variable in the same scope or introduce ambiguity.
Let's look at a few examples:
int max(int x, int y);
int max;
int main()
{
max = max(10,4);
}
This fails to compile outright because we have two different declarations of max at the same scope.
namespace test
{
int max(int x, int y);
}
using namespace test;
int max;
int main()
{
max = max(10, 4);
}
This also fails to compile because the using statement makes which max you want ambiguous even though both maxs are in different scopes. This case can be fixed by removing the ambiguity: ::max = test::max(10, 4);.
int max(int x, int y);
int main()
{
int max = max(10, 4);
}
This one fails because int max is in a narrower scope and replaces int max(int x, int y). There is no ambiguity; the code is trying to call an int as a function, and the function max is no longer visible.
int max(int x, int y);
int main()
{
int max = 42;
}
is totally valid. max is unambiguously an int and max isn't used as a function.
Note: int max = max; will compile, but max will be initialized with its uninitialized self. Not very useful, but I'm sure there's some weird trick that can take advantage of this behaviour.
There is a bit more leeway with max the type.
struct max
{
};
max max;
is valid, but
struct max
{
};
max max;
max fail;
is not. After max max, max is a variable and max-the-type is lost. Take away from this is avoid the always-popular string string; and map map.

Why can't Max be used as a function in this case?

I just started C++ and I don't understand why Max cannot be used as a function in this case.
What I'm trying to do in this code is find the maximum number between the first one and the forth one, on any random number that only has 4 digits.
I've tried inserting Max to int, also to cin but it still does not work. any ideas on how to do it?
Thank you!
#include <iostream>
using namespace std;
int main()
{
int Max, a, b, c, d;
cin>>Max>>a>>b>>c>>d;
Max(a,b) = (a+b+abs(a-b))/2;
Max(b,c) = (b+c+abs(b-c))/2;
Max(c,d) = (c+d+abs(c-d))/2;
Max = (Max(a,b)+Max(b,c)+Max(c,d)+abs(Max(a,b)-Max(b,c)-Max(c,d)))/2;
}
There is some confusion, you'll need to study function syntaxes.
A variable declaration:
int Max;
The above declares a variable called Max.
A function declaration:
int Max(int a, int b);
The above declares a function, Max, which takes 2 int parameters: a and b.
A function definition could be:
int Max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
For extra points, find the calculus formula for return the maximum value of 2 integers. :-)
Notes:
1. The above function can't be on the Left-Hand Side (LHS) of an assignment operation (it doesn't make sense, how would the function be assigned a value?).
2. The function returns a value. You should assign the value to a variable or print it:
int maximum = Max(3,15);
std::cout << "Maximum of 4, 24 is: " << Max(24,4) << "\n";

Is there a way to fix this error : no match for 'operator[]' (operand types are 'empl' and 'int')

Im getting homework done but i faced this problem i tried everything changing variables name, changing function ....
I looked into this problem on google still no idea how to fix this error.
#include<stdio.h>
#include<iostream>
struct empl {
char nom;
char pre;
float salaire;
double cin;
}empl;
struct empl t[50];
struct empl E;
int taille(int n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n", &n);
return 0;
}
int remplire(int n, struct empl t, int i)
{
for (i = 1; i <= n; i++)
{
printf("t[%d].nom= ", i);
scanf("%s\n", &t[i].nom);
printf("t[%d].prenom= ", i);
scanf("%s\n", &t[i].pre);
printf("t[%d].salaire= ", i);
scanf("%f\n", &t[i].salaire);
printf("t[%d].CIN= ", i);
scanf("%lf\n", &t[i].cin);
}
}
int main()
{
int i, n;
int taille(int n),
taille(n);
int remplire(int n, struct empl t, int i);
remplire(n, t, i);
}
Although your code is written mostly in C style, you seem to be compiling it with a C++ compiler, as it accepts #include <iostream> (though you do not appear to use anything from it), and it has a notion of operator overloading. C and C++ are distinct languages, and the distinction is somewhat relevant here.
In either language, however, the code you have presented is flawed. The problem is with the several expressions in function remplire that follow this pattern: &t[i].nom. Absent any operator overloading (which is not available in C anyway), the [] operator in those expressions requires one of its operands to designate either a pointer or an array, and the other to designate an integer. Although there is a file-scope variable t that is an array, inside remplire() that is shadowed by a function parameter with the same name. Inside that function, then, t refers the the parameter, which is a struct empl, not an array or pointer (or integer).
Your compiler ought to be giving you another clue, too, where you call that function ...
remplire(n,t,i);
..., passing the global t as an argument. The compiler very much should complain about a type mismatch between the second argument (t) and the corresponding function parameter.
Perhaps what you wanted to do is simply to declare remplire() to accept a structure pointer as its second parameter:
int remplire(int n, struct empl *t, int i)
While you're at it, do remove the redundant local declaration of that function inside main(). You don't need that as long as remplire() is defined before main(), and if you want to have a separate declaration of that function then it would best be placed at file scope, and probably in a header file.
First of all iostream is C++ header coming from standard library and will not work for C program. And now issues:
int taille (int n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n",&n);
return 0;
}
This function is called with an input parameter - that means you can pass a value into a function, but not access the parameter and hope it will be used in other places. To correct this you should declare the function should look like this:
int taille (int * n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n", n);
return 0;
}
Next function - similar problem, it should look like this:
int remplire (int n , struct empl * t ,int i)
{
for (i=1;i<=n;i++)
{
printf("t[%d].nom= ",i);
scanf("%s\n",&t[i].nom);
printf("t[%d].prenom= ",i);
scanf("%s\n",&t[i].pre);
printf("t[%d].salaire= ",i);
scanf("%f\n",&t[i].salaire);
printf("t[%d].CIN= ",i);
scanf("%lf\n",&t[i].cin);
}
}
Or even like this:
int remplire (int n , int i)
as t is global variable. Also this function should return some value as it is declared to return int.
And now the main function:
int main()
{
int i,n;
int taille(int n),
taille(n);
int remplire(int n,struct empl t,int i);
remplire(n,t,i);
}
Don't redeclare functions inside another function, even if it is permissible it does not mean you should do it. Also main function should return 0 if everything works fine. To correct the function write it like this:
int main()
{
int i,n;
taille(& n);
remplire(n,& t,i);
}
Some good advice, please read some books to learn how to program in C if you want to go that way.
There are many issues in your code.
Starting reading a good C textbook is advised.
You probably want this:
#include <stdio.h>
#include <iostream>
struct empl {
char nom[30];
char pre[30];
float salaire;
double cin;
}empl;
struct empl t[50];
struct empl E;
int taille(int & n)
{
printf("saisie la taille de tableaux\n");
scanf("%d", &n);
return 0;
}
void remplire(int n, struct empl *t)
{
for (int i = 0; i < n; i++)
{
printf("t[%d].nom= ", i);
scanf("%s", &t[i].nom);
printf("t[%d].prenom= ", i);
scanf("%s", &t[i].pre);
printf("t[%d].salaire= ", i);
scanf("%f", &t[i].salaire);
printf("t[%d].CIN= ", i);
scanf("%lf", &t[i].cin);
}
}
int main()
{
int n;
taille(n);
remplire(n, t);
}
It's still poor code and it's written mostly in C style, but it compiles and works as intended.
In C++ you'd do this totally differently.
You've declared this global array variable t
struct empl t[50];
and also declared a parameter t in this function
int remplire (int n , struct empl t ,int i)
Inside the function it's going to treat any instance of t as to be the parameter as that is nearer in scope than the global variable. So when you have code like this...
scanf("%s\n",&t[i].nom);
...it's going to throw up errors because t isn't an array.
The solution is to use variable names that have meaning like "employee_array" rather than single letters.
Also that call to scanf is wrong as for strings you don't need to pass in a pointer to the variable, so it should look like
scanf("%s\n",t[i].nom);
But you'd also need to make nom be a string too - currently it's only a char.

Calling a function in main

I'm just learning C++ and I have a little code here:
using namespace std;
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
double moon_g();
}
double moon_g (double a, double b)
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
It compiles, but when I run it it only prints out:
This program will calculate the weight of any mass on the moon
but doesn't execute the moon_g function.
This line:
double moon_g();
doesn't actually do anything, it just states that a function double moon_g() exists. What you want is something like this:
double weight = moon_g();
cout << "Weight is " << weight << endl;
This won't work yet, because you don't have a function double moon_g(), what you have is a function double moon_g(double a, double b). But those arguments aren't really used for anything (well, they are, but there's no reason to have them passed in as arguments). So eliminate them from your function like so:
double moon_g()
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
double a;
cin>>a;
double b=(17*9.8)/100;
double mg=a*b;
return mg;
}
(And declare the function before you call it.) More refinements are possible, but that'll be enough for now.
This is a function declaration:
double moon_g();
this won't call a function, and if you did have it correct, which means adding two parameters since that is how you define it below:
moon_g( a, b ) ;
it would not work because you either need to move the definition of moon_g before main or add a forward declaration before main like this:
double moon_g (double a, double b) ;
Although it seems like a and b are not inputs but values you want to return back to main then you would need to use references and it would need to be declared and defined like this:
double moon_g (double &a, double &b) ;
^ ^
A useful thread to read especially if you are starting out would be What is the difference between a definition and a declaration?.
Which compiler you use makes a difference here clang provides the following warning:
warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
double moon_g();
^~
while I can not get gcc nor Visual Studio to warn me about this. It is useful in the long run to try code in different C++ compilers when you can, it can be a very educational experience and you don't have to install them either since there are plenty of online C++ compilers available online.
There is huge difference between calling a function and declaring it just as there is difference between local variables and function arguments.
I suggest reading basic tutorials first.
Anyway, thats how code should look like:
#include <iostream>
using namespace std;
double moon_g ()
{
double a,b;
cout<<"Enter the mass in kilograms. Use decimal point for any number entered\n";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
cout<<"Result is: "<<moon_g();
}
There are two problems in your code.
Firstly, if you want to call your function
double moon_g (double a, double b) // this means if you want to call moon_g() you must provide arguments a and b, otherwise, the you will encounter an compile error.
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
you should provide the two parameters a and b.
But a and b are calculated in the body of function definition, it is unnecessary to declare the two parameters. You can write like this.
double moon_g () //this means function moon_g() does not accept any arguments
{
double a, b; // declare a and b in the definition body instead of in the arguments list
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
Then, in the main function, your calling function statement is wrong. You may want to receive the return value. So, you should write the code like this.
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
double ret = moon_g();
}
Finally, it is mostly recommended that the function which will be called by another function should be declared or defined previously.