Skotti 2105 Posted May 8, 2013 Here is my current code, I can't seem to get the variables to carry over from each voided function from when they are called upon. I'm sure its something simple to fix, but I can't see it.My teacher says something about "scope" however the book has very poor examples and google is not my friend today for some reason...Thanks in advance.// NetPay2.cpp : Defines the entry point for the console application.// William DeYoung 5/5/13#include "stdafx.h"#include <iostream>#include <iomanip>#include <string>#include <fstream>#include <stdlib.h>using namespace std;//ending data streamvoid pause(){string junk;cout << "Press some keys and then enter to exit.";cout << endl;cout << endl;cin >> junk;}//get salary, pay by hour ectvoid GetRate(){int hourlyRate;cout << "What is the hourly rate of pay?: ";cin >> hourlyRate;while (hourlyRate < 5.5 || hourlyRate > 200.00){ cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate;}cout << endl;}// number of hours workedvoid GetHours(){float hours;cout << "Hours worked?: ";cin >> hours;if (hours < 0.0){ cout << "Invalid amount of hours! What are the hours worked?: " << endl; cin >> hours;}else if (hours > 60.0){ cout << "Hours are greater than 60." << endl;}}void PrintStub(){string name;float hours, hourlyRate;int finalPay;finalPay = hours * hourlyRate;cout << name << " has earned " << finalPay << ".";}void CalcFICA(){int finalPay, fICA;fICA = finalPay * 0.0765;cout << "FICA: " << fICA;}void CalcFedTax(){int finalPay, fedTax;fedTax = finalPay * 0.22;cout << "Federal tax: " << fedTax;}void CalcStateTax(){int finalPay, stateTax;stateTax = finalPay * 0.22;cout << "State tax: " << stateTax;}int _tmain(){//variables, string, intsstring name;float hours, hourlyRate;int finalPay, fedTax, stateTax, fICA;cout << "Employee name?";cin >> name;cout << endl;GetRate();GetHours();PrintStub();CalcFICA();CalcFedTax();CalcStateTax();pause();return 0;} Quote Share this post Link to post Share on other sites
splewis 283 Posted May 8, 2013 (edited) It's all a scope issue: when you declare a variable inside a function, it can only be used inside that function.For example, finalPay is declared inside _tmain. So when all the little functions try to reference it, the variable cannot be found.One solution is to declare it outside any function, then it will be set (via cin) in the main. A better solution would be to pass needed information as a parameter.Also, look at the getHours method, it reads into data to a variable then ... does nothing with it. That'd be a good place to return the data you read in, then use the function elsewhere. You could change it to this, which would let you say float hours = getHours(); elsewhere.float GetHours(){float hours;cout << "Hours worked?: ";cin >> hours;if (hours < 0.0){ cout << "Invalid amount of hours! What are the hours worked?: " << endl; cin >> hours;}else if (hours > 60.0){ cout << "Hours are greater than 60." << endl;}return hours;} Edited May 8, 2013 by splewis 2 waffles and Skotti reacted to this Quote Share this post Link to post Share on other sites
Skotti 2105 Posted May 9, 2013 It's all a scope issue: when you declare a variable inside a function, it can only be used inside that function.For example, finalPay is declared inside _tmain. So when all the little functions try to reference it, the variable cannot be found.One solution is to declare it outside any function, then it will be set (via cin) in the main. A better solution would be to pass needed information as a parameter.Also, look at the getHours method, it reads into data to a variable then ... does nothing with it. That'd be a good place to return the data you read in, then use the function elsewhere. You could change it to this, which would let you say float hours = getHours(); elsewhere.float GetHours(){float hours;cout << "Hours worked?: ";cin >> hours;if (hours < 0.0){ cout << "Invalid amount of hours! What are the hours worked?: " << endl; cin >> hours;}else if (hours > 60.0){ cout << "Hours are greater than 60." << endl;}return hours;}Im not sure I understand what you mean for parameters.as for the hours, that will be used later on. this code is not complete, but i need to be able to transfer the variables from one void function to another. Quote Share this post Link to post Share on other sites
splewis 283 Posted May 9, 2013 By using parameters to pass things I mean chaining calcStateTax to something like this:void CalcStateTax(int finalPay){ stateTax = finalPay * 0.22; cout << "State tax: " << stateTax;}You should really use the getRate, getHours method by returning the values too (what I posted earlier). That way, you can just say flout hours = getHours();float hourlyRate = getRate();Then the other "calculate" methods will take these as parameters and thus can use the variables inside the function. 1 Skotti reacted to this Quote Share this post Link to post Share on other sites
Mimic 3307 Posted May 9, 2013 You can't use a variable defined in a specific function inside of a different function. Local variables are only available to the function they are defined in. For instance, you use hours and hourlyPay as local variables in a specific function, and then use it in another function without it being defined in that function's scope. To fix this, you should probably make hours, hourlyRate, and finalPay global variables in that they aren't defined inside of a specific function, and can be read/written from any other function in your code. Alternatively, you can pass along those values as inputs to other functions (float/int/bool myFunction(int/float/char/*char)) to do whatever logic you have to do within those functions, and return a value based on it. 1 Skotti reacted to this Quote Share this post Link to post Share on other sites
MisterTurkes 544 Posted May 9, 2013 It's all a scope issue: when you declare a variable inside a function, it can only be used inside that function.For example, finalPay is declared inside _tmain. So when all the little functions try to reference it, the variable cannot be found.One solution is to declare it outside any function, then it will be set (via cin) in the main. A better solution would be to pass needed information as a parameter.Also, look at the getHours method, it reads into data to a variable then ... does nothing with it. That'd be a good place to return the data you read in, then use the function elsewhere. You could change it to this, which would let you say float hours = getHours(); elsewhere.float GetHours(){float hours;cout << "Hours worked?: ";cin >> hours;if (hours < 0.0){ cout << "Invalid amount of hours! What are the hours worked?: " << endl; cin >> hours;}else if (hours > 60.0){ cout << "Hours are greater than 60." << endl;}return hours;}WHY DO YOU KNOW FUCKING EVERYTHING?! Quote Share this post Link to post Share on other sites
Skotti 2105 Posted May 9, 2013 You can't use a variable defined in a specific function inside of a different function. Local variables are only available to the function they are defined in. For instance, you use hours and hourlyPay as local variables in a specific function, and then use it in another function without it being defined in that function's scope. To fix this, you should probably make hours, hourlyRate, and finalPay global variables in that they aren't defined inside of a specific function, and can be read/written from any other function in your code. Alternatively, you can pass along those values as inputs to other functions (float/int/bool myFunction(int/float/char/*char)) to do whatever logic you have to do within those functions, and return a value based on it.I see. Thanks both of you. The only reason I'm not using global variables is because 1, they are bad, and 2 its a requirement. Quote Share this post Link to post Share on other sites
splewis 283 Posted May 9, 2013 You can't use a variable defined in a specific function inside of a different function. Local variables are only available to the function they are defined in. For instance, you use hours and hourlyPay as local variables in a specific function, and then use it in another function without it being defined in that function's scope. To fix this, you should probably make hours, hourlyRate, and finalPay global variables in that they aren't defined inside of a specific function, and can be read/written from any other function in your code. Alternatively, you can pass along those values as inputs to other functions (float/int/bool myFunction(int/float/char/*char)) to do whatever logic you have to do within those functions, and return a value based on it.I see. Thanks both of you. The only reason I'm not using global variables is because 1, they are bad, and 2 its a requirement.That's good When I first started programming all of my variables tended to be global and all the methods were void. That was bad....If you haven't written and functions with arguments or return statements now is the time to start! It really is more natural and easier. Quote Share this post Link to post Share on other sites
Skotti 2105 Posted May 9, 2013 (edited) Ok, so I thought I got it, but turns out I didn't.void GetRate(){float hourlyRate;cout << "What is the hourly rate of pay?: ";cin >> hourlyRate;while (hourlyRate < 5.5 || hourlyRate > 200.00){ cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate;}cout << endl;}// number of hours workedvoid GetHours(){float hours;cout << "Hours worked?: ";cin >> hours;if (hours < 0.0){ cout << "Invalid amount of hours! What are the hours worked?: " << endl; cin >> hours;}else if (hours > 60.0){ cout << "Hours are greater than 60." << endl;}}void PrintStub(){float finalPay;string name;float hours, hourlyRate;finalPay = hours * hourlyRate;cout << name << " has earned " << finalPay << ".";}So, to use the local variables in other void functions I simply just changevoid GetRate(){float hourlyRate;cout << "What is the hourly rate of pay?: ";cin >> hourlyRate;while (hourlyRate < 5.5 || hourlyRate > 200.00){ cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate;}cout << endl;}tovoid GetRate(float hourlyRate){cout << "What is the hourly rate of pay?: ";cin >> hourlyRate;while (hourlyRate < 5.5 || hourlyRate > 200.00){ cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate;}cout << endl;}Because it didn't really work for me. Edited May 9, 2013 by Skotti Quote Share this post Link to post Share on other sites
splewis 283 Posted May 9, 2013 (edited) You've mixed it up a bit.The getRate example should return the value (i.e. have return type float instead of void & say return hourlyRate), it should have no parameters.Now, you could do something similar without any return types if you passed a pointer, and the function would put it in. But the way the code it set up now it does nothing with the value it reads in again.Only use the parameter to help compute things and pass data, not to actually put something into the parameter. This is the case where it should be a return statement and you just say float hourlyRate = getRate(); in the main.Here is a simple example that shows what a return statement does:int square(int x) { int sq = x * x; return sq;}int main() { // read in input int input; cin >> input; int squaredValue = square(input); cout << input << " ^2 = " << squaredValue;}I used different names for the local sq inside the square function and squaredValue in the main on purpose. In a sense, you are declaring the variable twice this way. Edited May 9, 2013 by splewis Quote Share this post Link to post Share on other sites
Dickbutt 170 Posted May 9, 2013 What does a global variable look like in C++? Like all the variables are in main or something?I know how it looks like in JavaScript, PHP and whatnot but not in C/C++ because of the whole scope thing. Quote Share this post Link to post Share on other sites
splewis 283 Posted May 9, 2013 (edited) You can declare the variables outside of the function, like so:#include <stdio.h>using namespace std;const double pi = 3.141592; // global constantint counter = 0; // global variable, also known as "a drunken mistake"int main() { printf("Ima guess pi is around %f \n", pi); for(int i = 1; i <= 10; i++) counter += i; printf("counter was %d \n", counter); return 0;} Edited May 9, 2013 by splewis Quote Share this post Link to post Share on other sites
Dickbutt 170 Posted May 9, 2013 Oh huh, I was never taught that. I thought that statement would just throw a compiler error (I'm sure it will throw warnings though for sure) Quote Share this post Link to post Share on other sites
Mimic 3307 Posted May 9, 2013 Oh huh, I was never taught that. I thought that statement would just throw a compiler error (I'm sure it will throw warnings though for sure)Nope. It's actually very standard to do this in C rather than C++, mostly when programming small scale embedded systems.In order to pass a value from a function to another function, the input function must:Be able to be passed a variable.Return a value.Currently none of your functions can input a value or output a value because there's no return type or inputs. For a function to be able to return a variable it must not be a void function. In other words, you want something along the lines of:float PrintStub(float hours, float hourlyRate) {....return hours*hourlyRate;}Where you could call that function by x = PrintStub(hours, hourlyRate) and it would the product of the two to the variable x. By changing void to float, the value returned by the function is a float.For a better explanation, click here. Quote Share this post Link to post Share on other sites
Skotti 2105 Posted May 9, 2013 god i feel like such a retard. double GetRate(){float hourlyRate;cout << "What is the hourly rate of pay?: ";cin >> hourlyRate;while (hourlyRate < 5.5 || hourlyRate > 200.00){ cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate;}cout << endl;return (hourlyRate);}Its still not returning a value and when it gets to PrintStub, it says there is no value because hours isn't being initialized. Quote Share this post Link to post Share on other sites
centran 4457 Posted May 9, 2013 // NetPay2.cpp : Defines the entry point for the console application.// William DeYoung 5/5/13//#include "stdafx.h"#include <iostream>#include <iomanip>#include <string>#include <fstream>#include <stdlib.h>using namespace std;//ending data streamvoid pause(){ cout << endl << "Press ENTER to exit."; cin.get(); cin.get();}//get salary, pay by hour ectfloat GetRate(){ float hourlyRate; cout << "What is the hourly rate of pay?: "; cin >> hourlyRate; while (hourlyRate < 5.5 || hourlyRate > 200.00) { cout << endl << endl << "Invalid amount of hours! What is the hourly pay?: "; cin >> hourlyRate; } cout << endl; return hourlyRate;}// number of hours workedfloat GetHours(){ float hours; cout << "Hours worked?: "; cin >> hours; if (hours < 0.0) { cout << "Invalid amount of hours! What are the hours worked?: "; cin >> hours; } else if (hours > 60.0) { cout << "Hours are greater than 60." << endl; } return hours;}float finalPay(float hours, float hourlyRate){ return hours * hourlyRate;}void PrintStub(string name, float pay){ cout << name << " has earned " << pay << "." << endl;}void CalcFICA(float pay){ int fICA; fICA = pay * 0.0765; cout << "FICA: " << fICA << endl;}void CalcFedTax(float pay){ int fedTax; fedTax = pay * 0.22; cout << "Federal tax: " << fedTax << endl;}void CalcStateTax(float pay){ int stateTax; stateTax = pay * 0.22; cout << "State tax: " << stateTax << endl;}int main(){//variables, string, ints string name; float hours, hourlyRate, pay; cout << "Employee name?"; cin >> name; cout << endl; hourlyRate = GetRate(); hours = GetHours(); pay = finalPay(hours, hourlyRate); PrintStub(name, pay); CalcFICA(pay); CalcFedTax(pay); CalcStateTax(pay); pause(); return 0;} Quote Share this post Link to post Share on other sites
Skotti 2105 Posted May 9, 2013 motherfucking centran you are my man. remind me to buy you a steam game. Quote Share this post Link to post Share on other sites
Dickbutt 170 Posted May 9, 2013 (edited) I did it myself too(I'm not sure what the requirements of the assignment is but that's what I got from your source)Things I've noticed when redoing the code:- If you are going to use functions to "Get" values, try doing it for everything. The name was being initialized in main and not it's own function like the GetRate() and GetHours() function.- Be wary of scope. If you are going to use the variables again in another function, just store everything in one function. In this case, I see you have the variables you want to use in main() but then are initializing them again in the function you are calling. This will only change the value in the function and not the one in main();. Much easier to understand once you start using class/struct- You keep changing the variable types for some reason. You have a variable in main() as a float and then have it as an int in the function. Bad idea since data may get corrupted and again, might not get the value you're aiming for.- I changed all the floats to double because of the reason above. Or at least Visual Studio was throwing warnings changing a float into a double. I suggest using doubles instead (someone can correct me here. I rarely use floats)- You really need to work on improving your coding style. It's a bit messy so far so it's harder to read and trying to find errors. Try spacing stuff better and it'll help you when you start debugging larger programs. And it's in good style too especially if you were to work in teams.- If you're working with functions, remember function prototypes either in the header file or near the top of the source file (below the library calls). It's used so that the compiler knows that there are functions and what it should use.- Don't use "void" functions for everything. If you're going to use a function to reassign a value of a single variable, use whatever data type that variable is and reassign the value using the return value of that function. Voids, from what I used it so far, is used to output stuff and to change multiple variables in a single function.There are other stuff I forgot and i'll post it if I remember.EDIT - If you want, I can post my source but Centran seems to have posted a complete one already Edited May 9, 2013 by Dickbutt Quote Share this post Link to post Share on other sites