C/C++ :: Using X And Y Variables In Function Parameters?
Jan 24, 2015
In my program I created three separate return functions. Each function is labeled:
int boxes(int x, int y);
int leftOver(int x, int y);
double avgItemsShipped(int x, int y, int z);
Is it bad programming practice to use 'x' and 'y' in all of my functions? Should I use the this keyword inside the function? We use this often in my Java class and I know it exists in C++, but I haven't actually seen it used (or used it myself yet).
View 3 Replies
ADVERTISEMENT
Jan 18, 2013
TinyGPS::TinyGPS()
: _time(GPS_INVALID_TIME)
, _date(GPS_INVALID_DATE)
, _latitude(GPS_INVALID_ANGLE)
[code]....
I am wondering about the constructor. I see there appears to be nothing inside of TinyGPS::TinyGPS() as far as parameters go and that that declaration is followed by a ":". First I'm wondering as to the meaning of the colon. As well with the variables defined after the ":" I see some "(0)" and I am wondering as well to the exact meaning of the "(0)". Are those variables being defined as parameters separated by ","?
View 2 Replies
View Related
Aug 27, 2013
What does collection of parameters as argument of a function in C mean? Also any place I can refer to find those parameters?
Googling gives me Parameters and Arguments But not really sure whether that is what is needed.
View 6 Replies
View Related
Mar 19, 2014
So in this function it is already passing the array into the function but the thing is one parameter being passed into the function and if so how do I go about passing 3 arrays as parameters into the function? Also the program only asks for a user entry of hours for three different cars so why would there be a point in making two additional arrays to be passed into the function?
#include <iostream>
#include <iomanip>
using namespace std;
//passing array into function
double calculateCharges(double hours[], int HoursArrayLocation);//call function
[Code] ....
View 12 Replies
View Related
Feb 22, 2015
So I was reading my book and it listed this piece of code. The first piece of code is in the book and the 2nd is just my test on the piece of code. I am curious as to why in the functions parameters there is a reference to aString. I've noticed that removing it has no affect on the outcome of the code.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome (string & aString) {
[Code] ....
View 2 Replies
View Related
Jul 7, 2014
I'm asking how to create a function with default parameters with the possibility to init the parameters that you need.
Code Example :
#include <iostream>
int func(int a = 1, int b = 2, int c = 3, int d = 4) {
return a + b * c / d;
[Code] .....
View 3 Replies
View Related
Nov 19, 2013
So I'm writing a data structure from scratch as part of a university assignment in c++, and I have to write an iterator for it. The problem involves comparison between constant iterators and normal iterators, and I'm going about it in this way: I wrote a constructor of normal iterator which takes a const iterator as its only parameter, hoping that the comparison operator between two normal iterators will be enough:
btree_iterator(const_btree_iterator<T>&conv):_target(conv._target),_index(conv._index),_last(conv._last){}
(and somewhere else)
template <typename T>
bool operator!=(btree_iterator<T> one,btree_iterator<T> other){
return !(other == one);
}
and I'm testing it in this way:
btree<int> bl(5);//the data structure
auto iter = bl.begin();
iter != bl.cend(); //trying to compare iterator with const iterator
but apparently this is wrong, since the compiler tells me something along the line of "no function 'operator!=' which takes ......" It seems the constructor is alright, since the following works:
btree<int>::iterator i(bl.cend());
Am I getting something fundamentally wrong about it? How is this functionality actually implemented in C++ library containers like vector?
View 9 Replies
View Related
Apr 9, 2013
I am creating code for a group project in my class. All my group members made a header file with an object in it with their functions. One of the functions in my partner's code uses a data member of mine in the function, so she has the function parameter a object of my object. (This isn't the code but for example)
class B {
friend class A;
void displayAthing(A object) {
cout<<object.thing<<endl;
}
I have this when I call the function in the cpp file
int main() {
A object;
B b;
b.displayAthing(object);
return 0;
}
However, when I compile, it gives me an error that the function does not take 1 arguments.
View 4 Replies
View Related
Nov 17, 2014
I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?
For example, the following code outputs
Code:
1 2
2 1
Code:
#include <iostream>
#include <stdarg.h>
void foo_func(int v1, int v2)
{
std::cout << v1 << " " << v2 << std::endl;
[Code] .....
View 3 Replies
View Related
Jun 30, 2012
Need setting up counters for this program which should
Given a file of text, assume that
a "word" is 1 or more consecutive, non-whitespace characters
a "sentence" is a series of words terminated by either a period, exclamation point, or question mark
Design a C++ program (using functions/passing parameters) that will
-interactively prompt for and read the name of an input file
-interactively prompt for and read a string to search for
-open the input file (using an input filestream variable) and with one pass through the file
-count the number of "words" in the file
-for each word, make sure all letters, except the first, are lower case - leave the first character unchanged
-count the number of "sentences" in the file
-count the number of letters in the file (A-Z,a-z)
-count the number of consonants in the file (consonants are letters that are not vowels - the vowels are: a, e, i, o, u, A, E, I, O, and U)
-count the number of nonwhitespace characters in the file that are NOT letters
-count the number of times the search string is found in the file (must be an exact match) - search for matches AFTER upper case letters have been coverted to lower case
View 2 Replies
View Related
Feb 2, 2013
Is there anyway we can return 2 values from a called function. Example
Code:
float xxxx(float a, float b, float c, float d)
{///
///
///
}
void xxx() {
int e,f,g,h;
////
////
xxx(e,f,g,h);
}
So if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming.
View 5 Replies
View Related
Dec 9, 2013
I have this code:
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
[Code]...
and it does not compile.
The error is:
test.cpp: In function ‘int main()’:
test.cpp:20:30: error: no matching function for call to ‘func1(std::vector<int>&)’
test.cpp:20:30: note: candidate is:
test.cpp:8:45: note: template<class T, class U> std::map<T, T> func1(U)
test.cpp:8:45: note: template argument deduction/substitution failed:
test.cpp:20:30: note: couldn't deduce template parameter ‘T’
View 3 Replies
View Related
Apr 9, 2014
I'm using the Visual C++ Express 2008 and i need to pass as parameters to a function characters coded in UTF 8. My environment is Windows 7. The editor of the VC++ write in UTF 8 or UTF 16? If it writes in UTF 16 how can i change it?
View 2 Replies
View Related
Oct 24, 2013
I have been experimenting with variadic templates with the aim of caching a call to a class method by storing away the object pointer, method pointer and parameters. I've actually had some reasonable success but have now hit a stumbling block. I now wish to wrap my parameters in a simple template class when I cache them. My success is as follows:
Using variadic template functions to store these pointers and paremeters;
I'm able to pass a method pointer and unwrapped parametersI'm able to pass wrapped parameters on their own.I'm NOT able to pass a method pointer and wrapped parameters I set up a little prototype project to demonstrate the issue and added comments above the function calls to indicate the compilation results. Here is the code:
Code:
#include "stdafx.h"
//////////////////////////////////////////////////
// Basic class with a simple method
//////////////////////////////////////////////////
class MyClass {
public:
char Method( int i, float f ) {
return 'A';
[code]....
But I'm convinced it should take three arguments, the method pointer and two wrapped parameters. Visual studio even suggested it should as shown below:
View 3 Replies
View Related
Mar 16, 2013
What would be the correct way to call a function within main that has file pointer parameters?
function prototype: Code: void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);
View 2 Replies
View Related
Jun 22, 2013
I'm trying to write a simple Delegate class with a Bind() and Invoke() function. For now it only needs to support a void class function with no parameters. I've searched around and found quite a few exmaples, though, those class are heavily templated and I lose track trying to simplify it.
So far my code is following:
Code:
#include <windows.h>
class Test {
public:
void DoSomething() {
MessageBox(NULL, L"Test::DoSomething!", NULL, 0);
[Code] ....
The part I am having difficulty with is assigning &Test::DoSomething to the m_Callback variable.
&tObject::DoSomething works, yet _Callback which I pass &Test::DoSomething to does not work.
Also, why does the following line work:
Code:
m_Callback = &Wrapper<tObject, &tObject::DoSomething>;
When wrapper is like:
Code:
template<class tObject, void (tObject::*Func)()>
void Wrapper(void* Object)
Should it not be Wrapper<class-typename, parameter-1>(parameter-2) // This currently creates an error
View 2 Replies
View Related
Mar 29, 2014
ok I have a class Player with lots of variables and im gonna call a function to checkXp, if I call it with the whole player object does it use a lot more memory then if I just passed the couple things I need?
ex
checkXP(Player* play) // this is a whole object of player
or
checkXP(play->getXP(), play->getLVL()) // the variables I want.
I just realized I may not be able to modify anything from player in the checkXP() function
question 1: does passing the whole object use more memory
question 2: if I passed as just the variables I need, I wont be able to modify anything of object play?
View 1 Replies
View Related
Nov 12, 2012
This is a c program that is failing to compile. The error occurs in the calcLabs() function. The error called out is (btw, I'm using VS 2010): Error4error C2143: syntax error : missing ';' before 'type'
I don't understand why the compiler is not letting me declare variables in the calcLabs() function!
Code:
#include<stdio.h>
#include<stdlib.h>
void calcPercent(double *);
double calcLabs();
double calcExams();
double calcFinal();
char calcLetter(float);
[code]...
View 5 Replies
View Related
Mar 26, 2013
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int doSomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}
I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"
What am I doing incorrectly?
#include <iostream>
using namespace std;
int doSomething(int*, int*);
int main(){
int X, Y, result;
[Code] ....
I have multiplied both x and y by 10 and then added them together!
Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.
<< result << ".
";
system("PAUSE");
return 0;
}
int doSomthing(int *x, int *y)
[Code] .....
View 1 Replies
View Related
Feb 9, 2013
I am trying to display a variable from a class function the code works in debug but the variable is not displayed. Here is my code so far.
#include <iostream>
#include <string>
using namespace std;
class dayType {
public:
string day;
void setday(string);
[Code] .....
View 2 Replies
View Related
Dec 12, 2013
How to use a function twice to calculate two different variables. How to have a function compute the city tax, then it uses the function a second time to compute the county tax.
My program compiles, and runs fine, however it only outputs the city tax calculation. It seems it never attempts to calculate the county tax.
One thing: All of the directives I used is what we are limited to, no more, no less. He requires a set format and will dock points of you use anything but those directives and void main().
Here is my code so far:
/* function for city tax calculation */
#include <iostream>
using namespace std;
#include <string>
#include <cstdlib>
#include <cmath>
#include <iomanip>
const double city_tax_rate=0.03;
[Code] ....
I am using Visual studio 2010.
View 9 Replies
View Related
Jul 8, 2013
It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed. I am writing some functions to try to prove this theory, but I can't get it right.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x = 10;
printf("default x value is %d ",x);
[Code] ......
Code:
#include <stdio.h>
void try1(int x){
printf("x in try1 is %d
", x);
x++;
printf("x in try1 after ++ is %d
[Code]...
View 13 Replies
View Related
Mar 1, 2013
I am developing a program that will read a function (x^2+2x+4 or other function) and then comparing and start assigning variables. My idea is with an array:
int i,x;
char xs;
char function[20];
cin.getline(function, 20);
cout << "Your function is: ";
[Code] .....
Well this is my basically idea, but when the program detect an ^ this will be associate with the exp(x,n); well in general the user enter a function: x^3+3x^2+4x-8 give a value for x for example 3 and the program will convert in -- exp(3,3)+3*exp(3,2)+4*3-8 --, but I don't know how.
View 2 Replies
View Related
Oct 22, 2013
I am trying to return 2 numbers from my function to main(). They are both read in from an input file but it is not working out.
#include <fstream>
#include <iostream>
using namespace std;
ofstream outfile;
void heading(int);
int stuid(int,int);
[Code] ....
View 4 Replies
View Related
May 13, 2014
I am trying to realize a simple code with thread and lambda function.
My goal is to swap 2 variable . I launch 2 thread,
The first:
Put his value in a shared variable and notify it .
wait until an event on a condition variable occur.
read from shared value .
The second wait until an event on a condition variable occur.
Wake up
read from shared value .
Put his value in a shared variable and notify it.
This is the code
thread t1([&p1]()->void{
m.lock();
nt++;
if(nt==1) {
//first thread
unique_lock<mutex> u1(m); ***
[Code] .....
Why it say error "abort() has been called " on the istr ***
View 2 Replies
View Related
Mar 9, 2015
I need making my main function to run while not having any if or for statements. It can only declare variables and functions. Since my main function has command line arguments, how to so.
// This program counts all the words in a given file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE* txtFile = NULL; // File Pointer
char str[1000000];
[Code] ....
View 5 Replies
View Related