Error message: pointer to function - visual-c++

I get an error message saying expression must have (pointer-to-) function type. what am i doing wrong? i just started coding, i know i suck lol. I don't understand how to get the formula for the distance to work.
#include <cmath> //headerfile
#include <iomanip>
#include <iostream>
enter code here
using namespace std;
int main()
{
double d;
double t;
double g;
char choice ='y';
//output numbers to console
while (choice == 'y' || choice =='Y')
{
cout<<"Please input a value for the time"<< endl<<endl;
cin>>t;
g = 32;
d = (g)(t*t);
if (t<0)
cout<<"You cannot have a negative time"<<endl<<endl;
else
cout<<setw(8)<<fixed<<setprecision(2)<<"\n""The distance the ball has fallen is "<<d<<" feet"<<endl<<endl;
cout<<"Would you like to run this again? y for yes, any other key for no."<< endl<<endl;
cin>>choice;
cout<<endl;
}
system ("Pause");
return 0;
}

If (g)(t*t) is supposed to be a normal multiplication operation, then it should be g*t*t.

In your code, g is a double, but you are using it as if it was a pointer to a function (d = (g)(t*t);). If what you really want is to multiply t*t by g, you forgot an *:
d = (g)*(t*t);

Related

Is there a way to perform signed division in eBPF?

I am trying to perform signed division in eBPF but llvm is throwing unsupported error. Is there a way to perform signed division in any other way (direct/indirect) in eBPF?
eBPF doesn't have a signed division instruction in its instruction set.
You can still work around it though. Signed division is nothing more than preserving the XOR of the two sides. Meaning, a output is negative if one or the other is negative, but dividing a negative by a negative number gives a positive back.
This is what I came up with:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
int32_t sdiv(int32_t a, int32_t b) {
bool aneg = a < 0;
bool bneg = b < 0;
// get the absolute positive value of both
uint32_t adiv = aneg ? -a : a;
uint32_t bdiv = bneg ? -b : b;
// Do udiv
uint32_t out = adiv / bdiv;
// Make output negative if one or the other is negative, not both
return aneg != bneg ? -out : out;
}
int main()
{
printf("%d\n", sdiv(100, 5));
printf("%d\n", sdiv(-100, 5));
printf("%d\n", sdiv(100, -5));
printf("%d\n", sdiv(-100, -5));
return 0;
}
I am sure there are better ways to do it, but this seems to work.

std::max giving error C2064: term does not evaluate to a function taking 2 arguments

I am fairly new to C++. I was practicing some ds,algo.This code looks fine to me, but I am getting some error about function not taking 2 arguments. Though I get some error asked in stackoverflow none of the cases match my problem.
#include <iostream>
#include <algorithm>
int ropecutting(int n, int *cuts){
if (n == 0)
return 0;
if (n < 0)
return -1;
int res = std::max(ropecutting(n-cuts[0], cuts), ropecutting(n-cuts[1], cuts), ropecutting(n-cuts[2], cuts));
if(res == -1) return -1;
return res+1;
}
int main(){
int n, cuts[3];
std::cin >> n;
for(int i = 0; i < 3; i ++)
std::cin >> cuts[i];
std::cout << ropecutting(n, cuts);
}
The error I get is,
main.cpp
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5368): error C2064: term does not evaluate to a function taking 2 arguments
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5367): note: see reference to function template instantiation 'const _Ty &std::max<int,int>(const _Ty &,const _Ty &,_Pr) noexcept(<expr>)' being compiled
with
[
_Ty=int,
_Pr=int
]
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5368): error C2056: illegal expression
Wishing someone would point me out in the right direction. Thank you.
Of the overloads of std::max, the only one which can be called with three arguments is
template < class T, class Compare >
constexpr const T& max( const T& a, const T& b, Compare comp );
So since it receives three int values, that function is attempting to use the third value as a functor to compare the other two, which of course doesn't work.
Probably the simplest way to get the maximum of three numbers is using the overload taking a std::initializer_list<T>. And a std::initializer_list can be automatically created from a braced list:
int res = std::max({ropecutting(n-cuts[0], cuts),
ropecutting(n-cuts[1], cuts),
ropecutting(n-cuts[2], cuts)});

How to fix "expected identifier or '(' in C compilation?

I am new to coding and I keep getting stuck in the first few lines of code and I cannot figure out why. This is what I have so far:
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
{
printf("Minute: ");
int n = get_int();
}
I am getting this message when I try to compile the code:
What did I do wrong?
You're trying to call the main function. You should only define it. It will be called when the program is executed (it is the "entry point").
To define it, remove the semicolon after
int main(void)
You can also remove that void keyword
Then move that line down, between
int n; and the { that comes after it
Additionally, you're declaring the n variable twice. After you fix the first error, the compiler will complain about this one. Remove one of the declarations then.
You should remove the semicolon after int main(void) and move the variable declaration for n within the braces. Here is the correct code below.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Minute: ");
int n = get_int();
}

Integer division and remainder

HELP please. I'm new to this so please be nice and descriptive. I coded this in Visual studio and the goal is to find out how many of each L, M, and S trays I need based on the number of people attending. I am trying to divide by remainder and I'm getting an error on the last two lines. "expression must have integral or unscoped enum type" --- I don't even know what that means. English, please?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//prompt user
cout << "Please enter number of guests attending event:";
double attendees;
cin >> attendees;
double large_trays = attendees / 7;
double medium_trays = large_trays % 3;
double small_trays = medium_trays % 1;
The problem is that you are using a 'double' type variable. However the modulo operator is only available for integer type variables like 'int'.
Either use 'int' instead of 'double' or use type casting.
double medium_trays = (int)large_trays % 3;
This exception is being thrown because you are trying to use the modulus arithmetic operator '%' on a non-integer. See this question: Why does modulus division (%) only work with integers?
Try this:
double large_trays = attendees / 7;
int large_trays_rounded = (int)large_trays;
int medium_trays = large_trays_rounded % 3;
int small_trays = medium_trays % 1;

Loop to keep adding spaces in string?

I have the following code:
sHexPic = string_to_hex(sPic);
sHexPic.insert(sHexPic.begin() + 2,' ');
sHexPic.insert(2," ");
I would like to know how I can put this into a counted loop and add a space after every 2nd character. So far all this does is make this string "35498700" into "35 498700", which in the end I want the final result to be something like "35 49 87 00".
I assume you would have to get the length of the string and the amount of characters in it.
I am trying to achieve this in c++/cli.
Thanks.
Here's how it would be done in C++, using a string :) (I'm using C libraries cuz I'm more familiar with C)
#include <stdio.h>
#include <string>
using namespace std;
int main()
(
string X;
int i;
int y;
X = 35498700;
y= X.size();
for(i=2;i<y;i+=2)
{
X.insert(i," ");
y=x.size(); //To update size of x
i++; //To skip the inserted space
}
printf("%s",X);
return 0;
}
Have fun :)
That would "probably" work. If it didn't then please mention so :)

Resources