Thread: Newbie Help ASAP

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    9

    Exclamation Newbie Help ASAP

    I keep running into a run-time error... and it says that "tp" is uninitialized... what am I missing here?

    Code:
    #include <stdio.h>
    #pragma warning(disable:4996)
    
    void input_sales(int [3][4]); 
    void input_profit_margins(int[4]);
    void calc_store_totals(int [3][4],int[]);
    void calc_item_totals(int [3][4],int[]);
    void calc_item_profits(int[4],int[4],int[]);
    int calc_total_profit(int[4]);
    void print_report(int [3][4],int[3],int[4],int[4],int[4],int);
    
    int main()
    {
    	int sales[3][4];
    	int store_totals[3];
    	
    	// here we go 
    	int item_totals[4];
    	int profit_margins[4];
    	int item_profits[4];
    	int total_profit;
    
    
    	input_sales(sales);
    
    	input_profit_margins(profit_margins);
    
    	calc_store_totals(sales,store_totals);
    
    	calc_item_totals(sales,item_totals);
    
    	calc_item_profits(item_totals,profit_margins,item_profits);
    
    	total_profit = calc_total_profit(item_profits);
    
    	print_report(sales, store_totals, item_totals, profit_margins, item_profits, total_profit);
    
    	return 0;
    }
    
    void input_sales(int s[3][4])
    {
    	int row,col;
    
    	
    	for(row=0;row<=2;row++)
    	{
    		for(col=0;col<=3;col++)
    		{
    			printf("Enter the sales for Store #%d Item # %d\n",row+1,col+1);
    			scanf("%d%*c",&s[row][col]);
    		} 
    	}
    
    }
    
    void input_profit_margins(int pm[4])
    {
    	int item;
    	for (item=0; item<=3; item++) {
    		printf("Enter the profit margin for Item # %d\n", item+1);
    		scanf("%d%*c",pm[item]);
    	}
    }
    
    void calc_store_totals(int s[3][4],int st[])
    {
    	int row, col;
    
    	for(row=0;row<=2;row++)
    	{
    		st[row]=0;
    
    		for(col=0;col<=3;col++)
    		{
    			st[row]+=s[row][col];
    		}
    	}
    }
    
    /*taking as inputs:
    
    s - sales
    it - item_totals (this is what we'll write to)
    */
    
    void calc_item_totals(int s[3][4],int it[])
    {
    	
    	int row, col;
    
    	for (col=0; col<=3; col++) {
    		for (row=0; row<=2; row++) {
    			it[col]+=s[row][col];
    		} 
    	}
    
    	/*so what's happening here is this:
    	*set col=0 (ie-Burritos)
    	*set row=0 (ie-Store#1)
    	*add Store 1 value for Burritos to Burritos col of item_totals
    	*set row=1 (ie-Store#2)
    	*add Store 2 value for Burritos to Burritos col of item_totals
    	*set row=2 (ie-Store#3)
    	*add Store 3 value for Burritos to Burritos col of item_totals
    	*set col=1 (ie-Tacos)
    	*set row=0 (ie-Store#1)
    	*add Store 1 value for Tacos to Tacos col of item_totals
    	
    	get the pattern?*/
    }
    
    /*taking as inputs:
    
    it - item_totals
    pm - profit_margins
    ip - item_profits (this is what we'll write to)
    */
    
    void calc_item_profits(int it[4],int pm[4],int ip[])
    {
    	int item;
    
    	// multiply each item by its profit_margin
    	// store in ip
    	for (item=0; item<=3; item++) {
    		ip[item]=it[item]*pm[item];
    	}
    }
    
    /*taking as input:
    ip - item_profits
    returning:
    tp - total_profit
    */
    
    int calc_total_profit(int ip[4])
    {
    	int item;
    	int tp;
    	
    	for(item=0; item<=3; item++) {
    		tp+=ip[item];
    	}
    
    	return tp;
    }
    
    /*taking as input:
    
    s - sales
    st - store_totals
    it - item_totals
    pm - profit_margins
    ip - item_profits
    tp - total_profit*/
    Last edited by slapgun87; 07-14-2010 at 02:51 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    All statements must end with an ;

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    tp isn't undeclared. It's uninitialized. That's probably what the compiler is warning you about.
    Oh, never mind. I missed the comma vs semi-colon issue. But the uninitiliazed aspect is still an error.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    Sorry about that, my error. The program will accept the first profit margin (pm) from the for loop but then spit out the run-time error and won't allow me to input the (pm) for items 2, 3, 4. What am I missing here?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by slapgun87 View Post
    What am I missing here?
    An ampersand:
    Code:
    scanf("%d%*c",&pm[item]);

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

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    The error occurs at:

    int calc_total_profit(int ip[4])
    {
    int item;
    int tp;

    for(item=0; item<=3; item++) {
    tp+=ip[item];
    }
    return tp;
    }

    "Run-Time Check Failure #3 - The variable 'tp' is being used without being initialized."
    Last edited by slapgun87; 07-14-2010 at 04:00 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the error is very descriptive. tp doesn't have an initial value, so you're trying to append something to junk.
    "Variable is being used" <--- you're trying to use a variable.
    "without being initialized" <--- you haven't initialized said variable.
    What part do you not understand?

    Also, you're missing the names of the parameters in the prototypes:
    void input_sales(int [3][4]); <--- what is the name of that int array it accepts?
    http://sourceforge.net/apps/mediawik...arameter_names
    Last edited by Elysia; 07-14-2010 at 05:03 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    Also, you're missing the names of the parameters in the prototypes:
    void input_sales(int [3][4]); <--- what is the name of that int array it accepts?
    SourceForge.net: Do not remove parameter names - cpwiki
    Prototypes don't need variable names, just types. For that matter, you can call something "foo" in the prototype, and call it "bar" in the definition, and it also doesn't matter.


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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it's still bad practice, as the link clearly indicates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  5. Newbie C Programer Needs Help ASAP
    By Halo in forum C Programming
    Replies: 4
    Last Post: 02-21-2002, 01:52 AM