Setting a variable through a excel sheet

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.

Andy
Last edited on
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.
Depends on how limited your limited experience is?

For the most basic approach (which needs you know about the least stuff, but does take more coding on your part), you will will need to know about:

1. for and while loops
http://www.cplusplus.com/doc/tutorial/control/
2. string comparison
http://www.cplusplus.com/reference/string/string/compare/
http://www.cplusplus.com/reference/cstring/strcmp/
3. arrays
http://www.cplusplus.com/doc/tutorial/arrays/
4. file reading
http://www.cplusplus.com/doc/tutorial/files/

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.

And reading Excel is even harder!

Andy

Last edited on
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;

extern float total_sales = 0.0;
extern float ct_rate = 0.0;
extern float st_rate = 0.0;
extern float county_tax = 0.0;
extern float state_tax = 0.0;
extern float total_tax = 0.0;

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;
}

void calculate()
{
county_tax = round(100 * (total_sales * ct_rate) / 100;

state_tax = round(100 * (total_sales * st_rate) / 100;

total_tax = round(100 * (county_tax + state_tax) / 100;


}


void output()

{

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!)

And you might want to read the first, sticky post in this forum:
Console Closing Down
http://www.cplusplus.com/forum/beginner/1988/

And a related article:
Why system() is evil
http://www.cplusplus.com/articles/j3wTURfi/

Andy

PS Could you please go back and reformat the code in your earlier posts to use code tags.

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
// little C++ Shell cog wheel button ---->
// (if missing try refreshing your browser)
#include <iostream>
using namespace std;

int main() {
    cout << "Hello code tags!\n";

    return 0;
}

Last edited on
Topic archived. No new replies allowed.