C++ :: Call By Reference And Returning Wrong Value
Dec 17, 2013
Write your question here.
#include<iostream>
using namespace std;
void getInput(int& numOfDays, double& itemPrice);
[Code].....
So this program is supposed to calculate the price of an item, and return it back as the variable total of type int. but for some reason it gives me a weird output on total. And am i using call by reference correct?
View 1 Replies
ADVERTISEMENT
Apr 25, 2014
I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.
Here is a snippet:
Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}
Visual studio is saying that the return type doesn't match.
View 3 Replies
View Related
Sep 6, 2013
Example code:
Code:
#include <iostream>
#include <vector>
using namespace std;
class A{
[Code]....
I read somewhere, that we can imagine the reference as a pointer to the vector. So, my question is:
Let's assume that instance of class A, named a, was created with new. We call a.getV() to foo and then we call the destructor of a. foo is safe? Is the copy constructor of std::vector called?
View 1 Replies
View Related
Jan 25, 2014
When returning an object by reference, only the address of the returned-object is returned, and that way we spare pushing a large object into the stack, and also spare time of pushing and popping large object to/from stack.
But what happens when the object that receiving the returned-object, is not a reference, but a 'regular' object?
How is the content of the returned object copied into the receiving object?
See for example in main, wid vs rwid. (I know in the case the returned-object is just one variable, there's no need to return it by reference, but its for simplifying the code).
class Rectangle {
public:
Rectangle(int w=0, int h=0);
[Code].....
View 6 Replies
View Related
Feb 15, 2015
#include<iostream>
using namespace std;
int &fun() {
int x = 10;
return x;
}
int main() {
fun() = 30;
cout << fun();
return 0;
}
The code outputs 10.
Shouldn't it show an error because x is created locally on stack and gets destroyed on function return?
View 1 Replies
View Related
Aug 1, 2014
I have this method that takes a pointer to a class object and right now when printing it, it's returning the location in memory 0x100300000. I've tried tweaking the function in a few different ways but I cant get it to return the object instead of the location.
Here's the vector that addSale accesses and the deceleration of the addSale ethod in the employee class.
class Employee{
protected:
....
public:
[Code]....
View 4 Replies
View Related
May 14, 2012
struct emp {
char name[20];
int age;
float sal;
}e1 = {"hello", 24, 2345.67}, e2 = {"hi", 34, 45678.89};
[Code] ....
Query: I am unable to get emp &fun() and fun() = e2...
View 3 Replies
View Related
Jan 7, 2015
I would like to get the this pointer by call by reference. Is this possible? I hoped to get it with this code, but it doesn't work:
Code:
"cpp"]class DemoClass {
public:
DemoClass();
int x;
void setParam(const DemoClass ¶m){
param=this;
[Code] ....
I get always the error code "C2678". But I don't understand how I should change my code to avoid this.
View 4 Replies
View Related
Nov 15, 2012
Consider the following:
Code:
float foo( float const *bar )
)
{
float temp;
temp = *bar;
...
}
Something I am noticing is that temp right after the assignment to *bar, is not the same value as *bar. This is for a project at work and the code runs on an embedded board with an ARM processor. I've copied the function into a standalone program for both Visual Studio and Code::Blocks and it works correctly there.
View 12 Replies
View Related
Feb 19, 2014
I'm writing a program that shows check fees for different amounts of checks. My other fees are showing up right but the .10 cents is not. It's showing up as .08 cents per check when I run the program.
Oh yeah the + 10 is for a $10 fee
Code:
else if (checks < 20 || checks >= 0) {
fee = .10 * checks + 10;
cout<< "Bank service charge for the month is $ " << setprecision(4) << endl;
}
View 3 Replies
View Related
Feb 11, 2013
For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.
So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time (NULL));
[Code] ....
View 3 Replies
View Related
Feb 14, 2012
I have the following function:
Code:
public static void Des(byte[] key, byte[] inBuf, byte[] outBuf) {
if (key.Length != 8 || inBuf.Length != 8 || outBuf.Length != 8) {
throw new Exception("Des: Buffer or key length incorrect.");
}
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
[Code] ....
I do not get the expected output of
Code: { 0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56 }
instead I get
Code: { 0xC6, 0x61, 0x8D, 0x63, 0x0B, 0x39, 0x31, 0xB0 } am I doing something wrong?
I've even tried reversing the byte order of the test data, and I don't get the right result. I've tested the same data using C++ with CryptoPP on Linux, and I get the expected result, but I'm porting this code to run on .Net, and this is my main stoppage in the process at the moment.
View 5 Replies
View Related
May 24, 2014
My whole code is working but when the results are sent to my phone only the temperature value is reading correct the light value is all jungled up and moisture and pressure values are reading as 0.
Code:
#include "taskFlyport.h"
#include "grovelib.h"
#include "barometer.h"
#include <string.h>
extern BOOL incomingSMS;
extern int incomingIndexSMS;
extern BYTE incomingMemSMS;
[Code]...
View 4 Replies
View Related
May 26, 2014
I am only receiving the correct temperature value. The other 3 values are reading wrong. My format string isent matching the variable list. What I need to do.
float data_bar = 0.0;
float data = 0.0;
float anVal = 0.0;
float moistureVal = 0.0;
while(1) {
// Get the new value using the get() function
[Code] ....
View 3 Replies
View Related
May 12, 2013
I recently wanted to create a (yet) simple program that simulates a mouse movement.So far I managed to make the program work. It does move the mouse, click when expected but the problem is the location it does click at.Here's my code:
#include <Windows.h>
#include <stdio.h>
int leftclick (DWORD x, DWORD y);
int main(){
[code]......
The problem now is: I want the program (for testing purposes) to click at (1920, 1080) and (100, 100) afterwards. Now it does click within a specific range. When I use GetCursorPos to retreive the cursors position it differs quite a bit from where I expected the click to be.
a second question I have is: When I declare the following flag (in the code above) the program does use relative coordinates even though it shouldn't.
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
Where as it works out well when I add a MOUSEEVENTF_MOVE to it.I couldn't find any solution to this in Microsoft MSDN or any other website.
View 2 Replies
View Related
Jan 20, 2013
i'm making a for loop for a mini game which required the user to enter the input number.Let say if the user accidently entered a character instead of integer the whole program will go haywire so is there anyway to check for the error and prompt the user to input the data again?Here is the simple program...
for(row=0;row<4;row++)
{
printf("Enter Row%d:",row);
for(col=0;col<4;col++)
{
scanf("%d",&num[row][col]);
}
View 1 Replies
View Related
Jan 19, 2015
I have been experimenting with a program that loads WAV file and tries to read 1s and 0s from audio file (binary coded information). So far I got working code, but the problem is that I'm not getting correct data. I do get somewhat similar data. The problem is that I get data that amplifies low amplitudes, so instead some noise and binary data I get lots of noise and hard to recognize binary data. My code is this
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
// An unsigned char can store 1 Bytes (8bits) of data (0-255)
[Code] .....
This is wav file in binary viewer (after header)
0030 0032
00300035003200320035003A0048004A
004C0049004A00510049004300360038
and translated to decimal
48 50
4853505053587274
7673748173675456
and this is output from "wavosaur" program (and this one is correct)
0.001465
0.001526
0.001465
0.001617
0.001526
0.001526
0.001617
0.001770
0.002197
0.002258
0.002319
0.002228
0.002258
0.002472
0.002228
0.002045
0.001648
0.001709
You can see it's not the same. I do get the same data like I do from binary viewer, but I don't understand how they are translated to amplitude value. ? Where is the catch?
So this is not really question about c++, but about wav file structure and reading data.
View 2 Replies
View Related
Jul 11, 2014
I have this function that is supposed to take a float as a parameter and then call the getLine() method to accept the users input. The function basically just checks to see if what the user input was of the same data type, if it is it returns the input value, if not then it keeps looping through taking new input until its correct. The problem is no matter what number you put in the output always returns as 140734799803512.
float InputValidation(){
float num;
string strInput;
while (true){
getline(cin, strInput);
[Code] ....
You also need to include <string> and <sstream>.
View 3 Replies
View Related
Jul 14, 2014
Why my calculate function is producing a zero?I feel like it may have something to do with there being zeros the arrays it takes. The arbitrary cout statements are just for my debugging purposes. Input is formatted as follows:
<int>
<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
and so on
#include <iostream>
#include <string>
#include <sstream>
int** parse_input(int &num_items, int &pouch_size);
int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value);
[Code] ....
View 1 Replies
View Related
Sep 6, 2014
I feel as if I'm so close and I'm just messing up a value somewhere, but I am at a loss as to what I should change. Here is my code:
#include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
[Code] ....
View 2 Replies
View Related
Jun 7, 2014
I have a game that requires the switching of tiles. When I try to switch the blank tile (_) with 5, the 5 switches with the 8 instead of the tile switching with the blank tile. The code behaves the same with top row. I have observed that this only happen in the row and column bound by [0][0]; For example the four top left tiles ([8, 7, 5, are 4]) are behave erratically.
Here is sample code!
//swap if columns are equal and blank is to bottom
else if ((blankCol==tileCol) && (blankRow-tileRow==1)) {
temp = board[blankRow][blankCol];
board[blankRow][blankCol] = board[tileRow][tileCol];
board[tileRow][tileCol] = temp;
return true;
}
I am thinking perhaps there may be a double switch but then again why does it for only the left column and top row.
Attached image(s)
View 2 Replies
View Related
Feb 25, 2014
i'm getting a new error is a wrong return type.
WebAPI.cs
namespace DarkAPP___WForm.Libs {
class WebAPI {
System.Net.WebClient wc = new System.Net.WebClient();
public WebAPI() {
[code].....
View 6 Replies
View Related
Nov 12, 2013
Code:
#include <stdio.h>
int main(){
unsigned char x, y=10;
scanf ("%c", &x);
[Code] ....
If I take out scanf and assign x a value then it works, but if I enter, for example, 10 into scanf it comes up as 234 rather than 100.
View 13 Replies
View Related
Apr 10, 2013
Consider:
Code: template<unsigned int N>
class Test
{
private:
[Code]....
I just cannot understand why (clearly, we are calling <0, 0>, not <0, 8>). If I replace "N" with 8, it works as expected (at least for the beginning of the loop). I only tested on MSVC.
View 5 Replies
View Related
Feb 15, 2015
I have a question about my program.. I would like to make a program that if you put "ABC" on INT, the program will restart again... below are my coding...but it not works..if i put ABC on INT, the "Invalid Mark.. please re-insert mark :" will repeat, repeat and repeat infinitely...
Code:
#include <stdio.h>
void calcul() {int mark;
char * grade;
if (!(scanf("%d",&mark))){printf("
}
[code]....
View 7 Replies
View Related
Feb 18, 2015
I am trying to convert uint32_t or unit16_t to chars in little endian. When I was reading the char s from binary file I used memcpy so I did not need to convert them, but now I try this:
Code:
static void write2Bytes(unsigned char ** pDestStr, uint16_t* item) {
(*pDestStr)[0] = ((uint16_t) item & 0x00ff);
(*pDestStr)[1] = ((uint16_t) item & 0xff00) >> 8;
*pDestStr+=2;
[code]...
In the last function I try either to cast to uint_32_t or unsigned char but in both case I got wrong values. The original binary string read from left: 0x36000003 (it is number corresponding to 48Mbytes).
And the result when casting to unsigned char is T000000 and T(>00 for uint_32. So what's wrong? I need to get some string with will be corresponding to 0x36000003. does this kind of convertion take a lot of compute performance when I one would use it offten (like when one would do convesions for bitmaps).
View 7 Replies
View Related