• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ help

Journer

Banned
ok...i'm working on the fallowing:

void getInfo (int getTnum, double& getAmt, int& getTtype, int& getTloc){
int trans;
ifstream TransNum;
TransNum.open("statement.txt");
TransNum >> trans >> getAmt >> getTtype >> getTloc;
while (trans!=getTnum){
TransNum >> trans >> getAmt >> getTtype >> getTloc;
}
TransNum.close();
return;

case 4:
int trans;
cout << "Enter transaction number: ";
cin >> trans;
getInfo(trans);
cout.precision(3);
cout << "Amount: $" << /*returned amount*/ << endl;
cout << "Type: " << printTransType(/*returhned amount*/) << endl;
cout << "Location: " << printTransLocation(returened-loc) << endl;
break;

am i doing the declarations for the references (thats what & is, right?) correctly? ex. funct (int& a)
do something with a...

but how do i call it in the menu function (the one with the case 4 switch)...

thanks 🙂
 
& means pass by reference. Basically sends the variables not the value to the function. So anything you do to the variable inside the function effects the variable you passed to it.
 
how do u recieve passed variable?

here is what i'm working on:

#include <iostream>
#include <fstream>
using namespace std;
void getInfo (int, double& , int& , int& );
void printTransType (int);
void printTransLocation(int);
int main(){
int input;
cout << "Enter transaction number: ";
cin >> input;
getInfo(input);
cout.precision(3);
cout << "Amount: $" << getInfo(getAmt) << endl;
cout << "Type: " << printTransType(getInfo(getTtype)) << endl;
cout << "Location: " << printTransLocation(getInfo(getTloc)) << endl;
return 0;
}
void getInfo (int input, double& getAmt, int& getTtype, int& getTloc){
int trans;
ifstream TransNum;
TransNum.open("statement.txt");
TransNum >> trans >> getAmt >> getTtype >> getTloc;
while (trans!=input){
TransNum >> trans >> getAmt >> getTtype >> getTloc;
}
TransNum.close();
return;
}
void printTransLocation(int x){
switch (x){
case 1: cout << "Grocery";
break;
case 2: cout << "Store";
break;
case 3: cout << "School";
break;
case 4: cout << "Bank";
break;
default: cout << endl << "Trans Location Print Error" << endl;
//I put this in here to help me debug the program
}
return;
}
void printTransType (int x){
switch (x){
case 1: cout << "Debit";
break;
case 2: cout << "Credit";
break;
default: cout << endl << "Trans Type Print Error" << endl;
//I put this in here to help me debug program errors
}
return;
}
 
these are the errors i keep getting:


source3.cpp(11) : error C2660: 'getInfo' : function does not take 1 arguments
source3.cpp(13) : error C2065: 'getAmt' : undeclared identifier
source3.cpp(14) : error C2065: 'getTtype' : undeclared identifier
source3.cpp(15) : error C2065: 'getTloc' : undeclared identifier

visuall studio 05
 
void getInfo (int, double& , int& , int& );

You need to put the variables names in there on function header .
 
Originally posted by: Cooler
void getInfo (int, double& , int& , int& );

You need to put the variables names in there on function header .

i did this:
void getInfo (int input, double& getAmt, int& getTtype, int& getTloc);

and it still gives the errors 🙁
 
Based on yoiur original example
cout << "Amount: $" << getInfo(getAmt) << endl;
cout << "Type: " << printTransType(getInfo(getTtype)) << endl;
cout << "Location: " << printTransLocation(getInfo(getTloc)) << endl;

Notice that getInfo is still being called with only one parameter.

 
okay...i completely re-did it so that everything was done inside of getInfo...

now in main()...how do i call getInfo without inputting anything?
ex:

int main(){

getInfo();

return 0;
}

maybe:
getInfo(int, double&,int&,int&);
?
 
Originally posted by: Journer
Originally posted by: Cooler
void getInfo (int, double& , int& , int& );

You need to put the variables names in there on function header .

i did this:
void getInfo (int input, double& getAmt, int& getTtype, int& getTloc);

and it still gives the errors 🙁

The function definition needs the types of the parameters to be written out in full, because that's a definition -- the parameter types are unknown otherwise. But you don't have to write out the types in full for a function call because at that time the required types are known -- you just need to supply parameters which have the correct types.

Read your textbook / manual on function declarations, calls and reference parameters.

So given a function declaration:

void getInfo (int input, double& getAmt, int& getTtype, int& getTloc);

A call sequence might be:

int input;
double amount;
int tType;
int tLoc;

input = 42;
getInfo (input, amount, tType, tLoc);

After this point, you'd have amount, tType, and tLoc populated by the function call if everything went well inside the function.

 
holy ******! i need to get started on that thing. it's due tuesday, isn't it? what section are you in?
 
Oh boy, this brings back memories of my C++ class from high school. I learned C++ the last year they taught CompSci in C++ and not Java... IMO, C++ > Java (taken 2 classes in Java in college so far).

Granted, it IS easier to write decent code quickly on Java, but to write great code you need a language like C++ or C (though that's not OO).

As far as answering the OP, the pass-by-reference was already explained... the solution to some of the errors is to make sure the function declarations and calls have the same # and type of parameters... otherwise it won't work. If you have a good IDE it should point you in that direction. For Java Eclipse almost writes your code for you...
 
Originally posted by: nkgreen
holy ******! i need to get started on that thing. it's due tuesday, isn't it? what section are you in?

003....yah dude...ive been working on it the past 2 days, lmfao...its long as fvck
 
Originally posted by: Cooler
& means pass by reference. Basically sends the variables not the value to the function. So anything you do to the variable inside the function effects the variable you passed to it.

Yikes ...

In english, the & is a reference to a memory location. So anything you do to the variable inside the function effects the variable you passed to it.
 
Originally posted by: Journer
Originally posted by: nkgreen
holy ******! i need to get started on that thing. it's due tuesday, isn't it? what section are you in?

003....yah dude...ive been working on it the past 2 days, lmfao...its long as fvck

yeah, i'm in section 10. i've worked on it about 20 minutes, but the values i pass in from the file are different than what they are supposed to be. so i got pissed and watched clerks.

 
Originally posted by: nkgreen
Originally posted by: Journer
Originally posted by: nkgreen
holy ******! i need to get started on that thing. it's due tuesday, isn't it? what section are you in?

003....yah dude...ive been working on it the past 2 days, lmfao...its long as fvck

yeah, i'm in section 10. i've worked on it about 20 minutes, but the values i pass in from the file are different than what they are supposed to be. so i got pissed and watched clerks.

yah...i gave up passing the variables and just did everything inside of getInfo...ill get some pts off, but at least the thing will work :/
our teacher sucks at explaining...and i couldnt figure out how to get a reference to work from the notes, my book, or online...oh well...i must be handicapped 😛

 
The getInfo routine must have the calling parameters match when it is called.

The declaration of the functions must match the definition of the functions.

Use of the & (as previously pointed out) allows direct manipulation of the variable memory, which should be defined in your main body.


All types of referenced to the functions getInfo must match up in the number of parameters and parameter types.

A mismatch of any kind will generate errors.
 
Back
Top