Thread: array problem?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    array problem?

    ok im not sure if this is an array problem but anyway here it is.

    my problem is im passing some values into a function called taxes and its supposed to return the total which it does however inside of that function im comparing to see whether the person is(S,MJ,MS,SH which are stored in a variable called filing) based on there answer which is stored in status.

    the problem from what i can see is it seems to work sometimes but not other times. in that function like the comparison and for look turn out right sometimes but not other times and im not exactly sure why. i think this is the problem but i could be wrong? cause the test data i wrote out on paper before i made this program worked and then when i typed out the program that specific test data works but then not everything else seems to work and i know this cause it doesnt run through that loop correctly. it should only not do calculations in that taxes function if a 0.0 is passed in for the tax and when it doesnt calculate something when it should it has to be screwing up somewhere around there is my guess?

    also note im aware this board hates the gets() function but its for school so leave it and also i know i have some cout's placed but its just cause its alot easier to deal with for test statements to see if i have my output correct or not rather than dealing with printf()'s percent signs that imstill not 100% familar with.

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <iostream>
    using namespace std;
    
    int get_int(char msg[]);
    double get_double(char msg[]);
    double taxes(double rate,double tax[],double total,char status[],char filing[][2]);
    void display(double rate,double total);
    
    
    int main()
    {
    	char buf[10];
    	char name[40];
    	char filing[5][2];
    	char status[3];
    	char msg[50];
    	char end = ' ';
    	double tax[4];
    	int no_dependents;
    	double wages;
    	double interest;
    	double dividends;
    	double other_income;
    	double total;
    	double rate = 0.0;
    	
    	strcpy(filing[0],"S");
    	strcpy(filing[1],"MJ");
    	strcpy(filing[2],"MS");
    	strcpy(filing[3],"SH");
    	while(end != 'N')
    	{
    	printf("What is your name: ");
    	gets(name);
    	strcpy(msg,"\nWhat are your wages: ");
    	wages = get_double(msg);
    	strcpy(msg,"\nWhat is your interest: ");
    	interest = get_double(msg);
    	strcpy(msg,"\nWhat are your dividends: ");
    	dividends = get_double(msg);
    	strcpy(msg,"\nWhat are your other income amounts: ");
    	other_income = get_double(msg);
    	strcpy(msg,"How many dependents do you have: ");
    	no_dependents = get_int(msg);
    	printf("What is your martial status(S,MJ,MS,SH): ");
    	gets(status);
    	for(int i = 0;i < 2;i++)
    		status[i] = toupper(status[i]);
    	
    	total = (wages + interest + dividends + other_income) - (no_dependents * 2800.00);
    
    	if(total <= 6000)
    	{
    		tax[0] = 2.8;
    		tax[1] = 0.0;
    		tax[2] = 2.3;
    		tax[3] = 0.0;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else if(total > 6000 && total <= 10000)
    	{
    		tax[0] = 7.5;
    		tax[1] = 5.2;
    		tax[2] = 7.2;
    		tax[3] = 3.8;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else if(total > 10000 && total <= 15000)
    	{
    		tax[0] = 9.6;
    		tax[1] = 8.3;
    		tax[2] = 8.9;
    		tax[3] = 7.4;
    		total = taxes(rate,tax,total,status,filing);
    		
    	}
    	else if(total > 15000 && total <= 20000)
    	{
    		tax[0] = 13.5;
    		tax[1] = 12.2;
    		tax[2] = 13.1;
    		tax[3] = 11.0;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else if(total >20000 && total <= 25000)
    	{
    		tax[0] = 15.5;
    		tax[1] = 14.6;
    		tax[2] = 15.2;
    		tax[3] = 13.8;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else if(total > 25000 && total <= 30000)
    	{
    		tax[0] = 17.4;
    		tax[1] = 16.3;
    		tax[2] = 17.2;
    		tax[3] = 15.4;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else
    		total *= .35;
    	display(rate,total);
    	printf("Do you want to enter another user(Y or N): ");
    	gets(buf);
    	end = buf[0];
    	end = toupper(end);
    	}
    
    	return 0;
    }
    	
    double taxes(double rate,double tax[],double total,char status[],char filing[][2])
    {
    	for(int i = 0;i < 4;i++)
    	{
    		if(strcmp(status,filing[i]) == 0)
    		{
    			
    			if(tax[i] != 0.0)
    			{
    				cout << "FILING VARIABLE: " << filing[i] << endl;
    				rate = tax[i] * .01;
    				cout << "RATE: " << rate << endl;
    				total = (total * rate) + total;
    				i = 5;
    			}
    			else;
    		}
    	}
    	
    	return total;
    }
    
    void display(double rate,double total)
    {
    	cout << rate << endl << total << endl;
    	printf("TAX: %d TOTAL: %d\n",rate,total);
    }
    int get_int(char msg[])
    {
    	char buf[40];
        printf(msg);
        gets(buf);
    	return atoi(buf);
    }
    
    double get_double(char msg[])
    {
        char buf[40];
        printf(msg);
        gets(buf);
        return atof(buf);
    }
    hooch

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you need to decide what language you're going to use first. cout is C++, not C. Stop mixing languages. Furthermore, just saying "it doesn't work some times" doesn't really help anyone help you fix your problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    im aware cout is C++ not C i just i find it a much easier print statement when testing for errors especially when i cant remeber which % to use half of the time.

    well when it runs that loop it doesnt go through when it should. theres only two times tax[i] should ever be 0.0 and it seems to somehow be 0.0 more than that and i dont know why? at least thats what i figure cause i dont see that FILING VARIABLE print statement pop up as much as it should. so aside from 2 exceptions i should always see it and i only see it some of the time?
    hooch

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Um.... I could be wrong here, but it seems to me that you're using variables in your functions, such as get_double where you use buf, that were declared in the int main().....If I'm not mistaken this won't work properly because variables declared inside a function will only work for that function. If this is the case, you need to use global variables, which means any variable that is used in multiple functions needs to be declared before the main fuction.
    edit: by the way, its also considered somewhat bad form to declare a variable as you use it, such as the i variable that you use in your for loops. I realize you said you weren't overly concerned about formating but its always a good thing to keep in mind. /edit
    Last edited by CrazedBrit; 02-07-2006 at 09:50 PM.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>im aware cout is C++ not C i just i find it a much easier print statement when testing for errors especially when i cant remeber which % to use half of the time.

    Sorry but that is hardly a good enough reason to be mixing languages. You need to pick one and stick to it.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    Quote Originally Posted by CrazedBrit
    Um.... I could be wrong here, but it seems to me that you're using variables in your functions, such as get_double where you use buf, that were declared in the int main().....If I'm not mistaken this won't work properly because variables declared inside a function will only work for that function. If this is the case, you need to use global variables, which means any variable that is used in multiple functions needs to be declared before the main fuction.
    edit: by the way, its also considered somewhat bad form to declare a variable as you use it, such as the i variable that you use in your for loops. I realize you said you weren't overly concerned about formating but its always a good thing to keep in mind. /edit
    yea theres a buf in main and in getdouble and getint should just be a local variable and not cause any problems unless i misunderstood you?

    and yea i normally dont declare as i use it but its just cause its a counter that gets disposed of quickly.

    any other ideas why that taxes function appears to be screwing up???

    EDIT:
    im aware mixing langauges isnt the brightest of ideas but its for school and i plan on deleting the cout's after im done figuring out what is wrong anyway no need to chew me out for when in my first post i admitted i was doing it and planned on getting rid of it.
    Last edited by ssjnamek; 02-07-2006 at 10:00 PM.
    hooch

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Code:
    double taxes(double rate,double tax[],double total,char status[],char filing[][2])
    {
    	for(int i = 0;i < 4;i++)
    	{
    		if(strcmp(status,filing[i]) == 0)
    		{
    			
    			if(tax[i] != 0.0)
    			{
    				cout << "FILING VARIABLE: " << filing[i] << endl;
    				rate = tax[i] * .01;
    				cout << "RATE: " << rate << endl;
    				total = (total * rate) + total;
    				break; //CHANGE
    			}
    			else;
    		}
    	}
    	
    	return total;
    }
    Apart from what has already been mentioned, the code looks fine to a novice like myself! I woud suggest checking the length of the inputs before copying into your strings.

    Cheers,

    Nick.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    not sure what break did but still same old problem.

    thanks anyway
    hooch

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh gosh stupid me lol

    filing was one array element too small needed to be [5][3] not [5][2]
    hooch

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    The only thing I can think of is limiting the magnitude of the floats. Or, check for 0.00 and nothing less than that.
    Code:
    if(tax[i] < 0.001) ...
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > gets(name);
    Read the FAQ man.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    Quote Originally Posted by Salem
    > gets(name);
    Read the FAQ man.
    teachers give grades not you is my answer to the FAQ lol. though i did read it i think we are going to be using fgets later though. right now i beleive hes more interested in bounding arrays,pointers and structures down our throats.
    hooch

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    ssjnamek
    Registered User
    Join Date: Nov 2001

    4+ years, and still using gets()
    Nuff said.

    > teachers give grades
    A drinking certificate from a wino is nothing to be proud of.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Silly me, I understand now. I must have missed the declaration at the beginning of your functions...sorry about that, disregard my earlier comment.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    	if(total <= 6000)
    	{
    		tax[0] = 2.8;
    		tax[1] = 0.0;
    		tax[2] = 2.3;
    		tax[3] = 0.0;
    		total = taxes(rate,tax,total,status,filing);
    	}
    	else if(total > 6000 && total <= 10000)
    	{
    		tax[0] = 7.5;
    		tax[1] = 5.2;
    		tax[2] = 7.2;
    		tax[3] = 3.8;
    		total = taxes(rate,tax,total,status,filing);
    	}
    You don't need that part of your ifs, since if total was indeed <= 6000, the previous if would be executed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM