Complete beginner when it comes to C++ (Or coding in general) just wanted to try and write a program in C++ that was assigned to me in a class that uses a pseudocode. I was able to do it but I wanted to see if I could take it further by instead of having the amount for a variable to be set to just typed in, having it check against a sheet using preset identifiers.
Ex Current: st_tax, type in the number so say 0.07
Ex Future: st_tax, type postal code: NC sets to 0.08 or CA sets to 0.11
So just having a list that it will use to set the variable to the correct amount.
There's a 2nd part that refers to counties but I'll get back to once I know how viable this is.
So, this is the section that sets the st_tax variable. Very simple.
void input()
{
cout << "Enter Total Sales for month: ";
cin >> total_sales;
cout << "Enter County tax rate in 0.00 format: ";
cin >> ct_rate;
cout << "Enter State Tax rate in 0.00 format: ";
cin >> st_rate;
}
Do you need the data to be stored in Excel format? You could use, for example, a tab-delimited text file. These can be loaded, edited and save by Excel but are rather easier to read using C++ code.
If your data file is just postal codes of the form NC, CA, ... plus the VAT rates you could just separate the fields with spaces, but using tabs allows you to easily handle spaces in names and the like.
I just thought that using excel would make it similar, but I looked at tab_delimited and that looks like it would work. How would I go about making it that when a user puts in an input it would connect to the txt file to pull the correct information?
Is this something a bit more complex and I need to work with c++ more or is it something that I could input with my limited experience.
Plus how to handle the user input if it can be either a tax rate (number) or postal code (string.)
It would make sense to read the file into a table (using array(s)) at program start, and then search the table for the required value, rather than looking at the file each time.
More involved approaches would use std::map or std::vector with the appropriate algorithm.
Thanks for the help and the suggestions, I'll start reading through them ( already started to some extent). I definitely can see the usefulness of an array and I feel like it will be something I can get a fair bit of use out of.
Also, to give an idea of how limited my experience in c++ is, I literally started looking into it friday and this is the limits of what I can do.
#// Program 1
#include <iostream>
using namespace std;
cout << total_sales << "gives you a County Tax of " << county_tax << " and a State Tax of " << state_tax << " , Which combined is equal to: " << total_tax << endl;
}
int main()
{
input();
calculate();
output();
system("PAUSE");
}
(Not sure I actually need the extern for the variables or if I can just set them without it.)
But I'm trying think of different things I can do with this and make it better so that when I go to make a program that would be something that can really be used then I'll have a better understandings of different things that are possible.
Not sure I actually need the extern for the variables
You don't need it here (when all your code is in one source file.)
extern tells the compiler that a variable or function is defined in another "translation unit" (which is basically a source file plus the contents of all the header files that it includes) and corresponds to an object file (.o or .obj).
Note that global variables are best avoided, so while they're OK to use until you learn better approaches, don't get into the habit of using them (unless forced to!)