C :: Calling Functions With Arguments Using Pointer Variables As Operators
Feb 2, 2013
There are, or course, better ways to do this, but I need to stick to some rules:
(1) Use only pointer variables and not arrays or structs.
(2) Use the three functions shown--regardless of easier methods.
The program should ask for some input, operate on those numbers, and then display the results. I know I am confused over these things:
(1) All that syntax using '*' and '&' or neither.
(2) How to use the char type correctly.
(3) How to use a char type input as an operator (a + b).
(4) How to use the pointer of the operator variable (+,-,*,/) in an actual equation.
Code:
#include <stdio.h>
#include <stdlib.h>
// *** Prototype Functions ***
void Post_Results (float*);
void Calculate (float*, float*, char*, float*);
void Get_Numbers (float*, char*, float*);
[Code]......
View 5 Replies
ADVERTISEMENT
Jan 10, 2013
I believe I have the syntax correct but I'm having difficulty calling my overloaded == operator in main (last snip-it of code). Below are several files explaining the code.
Commission.h here is where the friend bool operator == exists and I believe I have it initialized correctly.
#ifndef COMMISSION_H_INCLUDED
#define COMMISSION_H_INCLUDED
using namespace std;
class Commission {
public:
Commission();
Commission(int, double, double, double );
~Commission();
[Code] .....
View 3 Replies
View Related
Apr 9, 2015
#include <iostream>
using namespace std;
int main(){
int polje[8][8];{
for(int i=0;i<8;i++)
for(int j=0;j<8;j++) {
[Code] .....
I don't get any errors,the program works. The problem is that it doesnt work how it should. This is a simple program that multiplies 2 variables(i and j)that go from 0 to 7. The problem I have is with the logical operators,i want the program to skip multiplication with 0 and when the 2 variables are the same value. When i try using only 1 logical operator it work.
View 2 Replies
View Related
May 5, 2014
Say I have a function pointer with this definition:
void ( *pressFunc ) ( void*, void* );
And i did this function:
void functionWithOneArg ( void* testPtr );
And i did this
pressFunc = &functionWithOneArg;
One. Would C actually let me do this? ( Assigning a function with one argument to a function with two )
Two. If so, what would happen to the second argument that is passed the function when its called? Does it just get 'cut off' and only the first argument is passed?
View 2 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Nov 25, 2014
In C you can just load and call the address of a function without defining its arguments like this:
Code: int (__stdcall *pMessageBox)();
int main() {
HMODULE h=0;
h = LoadLibrary("user32.dll");
pMessageBox = GetProcAddress(h, "MessageBoxA");
// MessageBoxA
pMessageBox(0, "MessageBoxA has been called!", "MessageBox Example", MB_OK);
return 0;
}
In C++ the same code gives "too many arguments for call" error unless you define the function first with its parameters.
Is there a way to do it the same way in C++?
View 10 Replies
View Related
Mar 1, 2013
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
View 14 Replies
View Related
Sep 8, 2014
I have struck with issue regarding process arguments
Part of my code below:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = MsiTran;
[b]info.Arguments = "-a """ + MSTpath + "" " + MSIpath"";[/b]
MSIpath and MSTpath are already defined string variables.
I get error: only assignment call increment decrement .....can be used as statement
How can setup my arguments to cal the pre-defined variables.
View 4 Replies
View Related
Nov 11, 2014
I am stucked in a problem of overloading arithmetic operators such as "+,*" for a class in the form
class Point {
int N; // dimension of the point
double *Pos; // length of N
}
My assign operator is :
Point& Point::operator= (const Point& pt) {
N= pt.N;
if(Pos == NULL) Pos = new double[N];
memcpy(Pos, pt.Pos, N*sizeof(double));
[Code] ....
The add operator "+" is:
Point operator+( const Point& pt1, const Point& pt2 ) {
Point ptr = Point(pt); // this is a constructor
for (int i=0; i<pt1.N; i++) ptr.Pos[i] += pt2.Pos[i];
return ptr;
}
Based on the above overloading, What I am going to do is :
P = alpha*P1 + beta*P2; // alpha and beta are double constants, P1 and P2 are Points objes
It is ok with Intel C++ 14.0 compiler, but does not work with the microsoft visual c++ 2012 compiler in debug mode in visual studio 2012.
I stepped in those operators and found that visual c++ compiler deconstructs the ptr in operators "*" and "+" before its return while intel c++ finished the operation P = alpha*P1 + beta*P2; and delete those ptrs at last.
Portability of my operator overloading is worse. How to get those arithmetic operators overloading for class with pointers in it.
View 3 Replies
View Related
Feb 21, 2013
How do I call these functions from Mechanical.h???
Mechanical.h
#ifndef MECHANICAL_H_
#define MECHANICAL_H_
class statics { public:
template<class T> struct Inertia_types {
T Rec(T _x, T _y);
T Tri(T _x, T _y);
[Code] ...
I am trying to create templated functions which I can apply all data types to except for strings and other types such as wchar. Am I also writing these correctly? This is my first attempt ever doing this.
View 1 Replies
View Related
Apr 6, 2013
Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,
#include <iostream.h>
#include <conio.h>
void main();
void loop()
[Code]....
so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?
View 7 Replies
View Related
Mar 14, 2013
Having issues calling my arguments from my templates, when i declare x and n in the main it comes up with errors
code below:
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
int intInput1, intInput2;
[code].....
View 4 Replies
View Related
Feb 4, 2013
Having a little trouble passing arguments to functions.
I wrote this simple program to get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do...
Code:
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
void menu(int HP,int Gold,int Armour);
[Code] ....
View 3 Replies
View Related
Aug 23, 2013
I'm having some problems with a function. The function is supposed to find the two largest values in an array.
Code:
void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;
[Code]....
I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.
Code:
int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;
[Code]...
Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?
View 10 Replies
View Related
Feb 28, 2014
Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?
I mean either using:
void ptr_Func(int *x)
{
*x = *x+1
}
or
int ptr_Func(int x)
{
return x + 1;
}
In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.
View 7 Replies
View Related
Jul 6, 2013
I am trying to get this statistical equation to work. m! / n! (m-n)!.
Code:
#include <stdio.h>
long factorial_M(long); // function prototype //
long factorial_N(long);
long total (long);
int main(int argc, const char * argv[]) {
[Code] .....
View 13 Replies
View Related
Mar 27, 2013
I have a vector of pointers inside a seperate Exam class.
vector <Question* > question_list
The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.
in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.
My problem now is calling the write function from a Exam function. I've tried several methods, such as:
for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}
but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"
I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.
View 15 Replies
View Related
Mar 1, 2013
I want to create two threads which will be calling even and odd functions where even function should print even number and odd function should print odd number.Can it be possible with condition variable? What is the code in both the cases i.e. two separate function and with condition variable.
View 6 Replies
View Related
Nov 17, 2014
I have to do this assignment but really don't understand the calling and called functions yet. Rewrite your program for computing the value of a sinking fund so that there is a C++ function that is called to calculate the value, i.e., a function that returns the accumulated value based on the number of years with the annual interest rate compounded monthly and a fixed-size monthly deposit.
My program is:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double R, r, t;
[code].....
View 1 Replies
View Related
Jan 5, 2015
I'm working on a code that reads a text file and follows the instructions written in that file. An example of the text file:
*sum
11 4 61 2
1 2 0 14
17 99 1 1
*subtract
5 6 7
1 1 1
45 6 9
I want to read the text file line by line, each time I find an instruction (that is, the keywords *sum or *subtract) I want to make a summation (in the case of *sum) of all the numbers in each of the following lines, and repeat until the next instruction appear. So the text file above should generate:
78
17
118
-8
-1
30
That is:
11 + 4 + 61 + 2 = 78
1 + 2 + 0 + 14 = 17
17 + 99 + 1 + 1 = 118
5 - 6 - 7 = -8
1 - 1 - 1 = -1
45 - 6 - 9 = 30
how can I implement a dictionary(I'm guessing a dictionary is the best way to go here) so that when I read the string *sum i can call a function that does the summation of the following lines.
the size of the text file may be big (several mb) and the number of different instructions to search for may be in the magnitude of hundreds.
View 5 Replies
View Related
Nov 4, 2014
Im currently working on a class assignment and the pseudo code containing the instructions that I need to complete list:
//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call Init() and pass 0, 0, 0, 0, and 0 as the parameters.
//--------------------------------------------------------------------------------------------------------------
My code for this is:
Point::Point(){
void Init(0, 0, 0, 0, 0);
}
The code I wrote for the function Init is here:
Point::Init(int mX, int mY, color mColor){
mX = 0;
mY = 0;
mColor = pInitColor;
}
My problem here is that whenever I try calling this function in the point class, I get an error next to void Init saying incomplete type is not allowed. Also visual studio is telling me that it expects a ')' after my first zero in that line.
View 1 Replies
View Related
Feb 10, 2013
I have Mq4 platform calling functions from DLL some how it doesnt make any differents when I make the call....
// DLL Code
#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)
using namespace std;
std::string My_String;
#define stringify(mymonths ) # mymonths
[Code] ....
I call this function
bool counts = StartRulls(bars);
Print("counts =",counts );
2013.02.09 23:03:242010.03.31 16:37 My_Mq4.mq4 EURCAD,M5: counts = 1
My result always stays 1
View 4 Replies
View Related
Apr 21, 2014
Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).
Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:
write("The number {0} is greater than {1}.", 5, -3);
will print
The number 5 is greater than -3.
write("There are no format specifiers here.");
will print
There are no format specifiers here.
The format specifiers and their meanings are listed in the following table
Specifier MeaningFormat Output for 1.62 Output for 2.0
none default {0}1.62 2
ccurrency{0:c}$1.62 $2.00
escientific{0:e}1.620000e+000 2.000000e+000
ffixed point{0:f}1.620000 2.000000
iround to int{0:i}2 2
Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.
Suggested extensions:
-Add an optional alignment specification in the format , e.g., make the format of the form {index[,alignment][:specifier]}, where alignment is an integer specifying the width of the field in which the corresponding argument will be printed. If alignment is positive, align to the right, if it is negative, align to the left.
-Accept an optional integer after the specifier letter, specifying the required precision in the output. For example, {0:f2} will print the number 1.6234 as 1.62, but {0:f5} will print it as 1.62340.
View 1 Replies
View Related
Sep 6, 2013
How to pass arguments from other functions to main. i want to write a program like nano well not exactly like nano editor. I have a function f_read(char* filename[]), and fopen() get filename[1] as file name and *filename[2] as "r" read mode and rest of the code will read from a file.
I want is this char filename[] to main(int argc , char argv[])
how can i do that??
View 4 Replies
View Related
Apr 24, 2013
I'm working on a parallel Fortran program by using MPI, which calls a very good random number generator function from C (drand48 and srand48 for seed).
My Fortran program file "Par_PICFort_4.f95" is smt. like:
Code: PROGRAM main
IMPLICIT NONE
Include 'mpif.h'
[Code]....
I compile the code as: Code: mpif77 -o PIC Par_PICFort_4.f95 drand48.c Program seem to be running, but after checking the simulation results, I see that drand48 is not working in a parallel way. Creating independent random numbers which different seeds is very important for me.
Question: Is it possible to run the drand48.c file as parallel?
View 2 Replies
View Related
Mar 12, 2014
I have a program that is working very well when I pass C++ vectors as arguments to my functions by reference, but I get some compilation errors when try to make a modification. I am also posting the entire program and its output below. so that you can see what is going on. I have commented out the line that causes an error.(Some of the indentation that got corrupted when I copied the code to the browser.)
This program basically calculates the coefficients of a least square polynomial and then evaluates this polynomial at artificial data points and verifies that this actually reproduces the original data within reasonable floating point error.
The function that computes the coefficients of the least square polynomial is Code: vector<double> LSPVecValued_GSL( const int, const vector<float> &, const vector<float> &); and as you can see it returns a vector by value, and this vector contains the coefficients of the least square polynomial.
There is also a function that evaluates this polynomial by accepting a vector argument by reference : Code: float evaluate_polynomial(double, vector<double>& ) ; I have also created another version of the evaluation function which accepts the same vector argument by value: Code: float evaluate_polynomial_ByValue(double t, vector<double> vec_a) ; In the program I call the first evaluation function (whose vector argument is passed by reference) by first using an intermediate vector variable containing the coefficients, and then I pass this vector as an argument to the evaluation function, as follows:
Code:
vec_a = LSPVecValued_GSL( deg, vec_x , vec_y);
for(int j=0; j< n ; j=j+20 ) {
cout<<"x["<<j<<"] = " << vec_x[j] << " ,y["<<j<<"] = " << vec_y[j] <<" , p(x["<<j<<"]) ( EVALUATED FROM REFERENCE) = "
<< evaluate_polynomial( vec_x[j], vec_a) << endl; // This version works without error
[Code] .....
As you can see above, I am also able to call the second evaluation function (the one whose vector argument is passed by value) directly by plugging in the function LSPVecValued_GSL"(...)" and this works without error, and this is a one step process, only one line of code is involved.
However, I get a compilation error (line number 12 that I have commented out above) if I try to plug in the function "LSPVecValued_GSL(...)" into the first evaluation function that expects a vector argument by reference. I tried to put a "&" in front ofLSPVecValued_GSL but this did not fix the bug.
What syntax is appropriate to use the first evaluation function (which accepts a vector argument by reference) if I want to plug in the vector-valued function LeastSquarePolynomial_GSL directly in the the first version of the evaluation function which expects a vector argument by reference?
View 14 Replies
View Related