Thread: automobile program

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Thumbs down automobile program

    This is a program i am trying to get working. It is supposed to read in values of cars and then input howmany sold for each model for each sales person. it works fine until it gets to ten then it goes 1232323 and will not stop can anyone help me or show me as to why this is happening?
    Code:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	int i,j;
    	int iAuto[10];
    	int iSales[9][11];
    	int iTotSalesperPerson[11];
    	int TotSales;
    
    	// Reading prices for the Automobiles
    	for(i=0;i<10;i++)
    	{
    	cout << "Automobile "<< i << " Price: ";
    	cin >> iAuto[i];
    	}
    	// Reading Sales for Models & Sales Person
    	for(i=1;i<=10;i++)
    		for(j=1;j<=8; j++){
    
    	cout << "Model "<< i << " - Sales Person " << j << ":";
    	cin >> iSales[i][j];
    	
    	}
    	// Sales Calculation for each person
    	for(j=1;j<=8;j++)
    		for(i=1;i<=10;i++){
    	iTotSalesperPerson[j]+=iSales[i][j];
    	cout << iTotSalesperPerson[j];
    		}
    
    	// Total Sales
    		for(j=1;j<=8;j++){
    	TotSales+=iTotSalesperPerson[j];
    	cout << TotSales;
    		}
    }

  2. #2

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    arrays are zero based
    so use i=0;i<10;i++ loops

    2. in your case iSales[i][j] i goes to 10 and j to 8 - so switch dimentions in declaration of the array

    3. main should be int

    4. probably use of vectors is preffered over the arrays
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    cool it let me add all the cars however it says the use of
    Code:
    TotSales
    is used without being defined after i enter all the cars any ideas?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    TotSales+=iTotSalesperPerson[j]; is evaluated to
    TotSales=TotSales+iTotSalesperPerson[j];
    because you do not initialize the TotSales to anything it contains garbage

    So what you get is
    TotSales="garbage"+iTotSalesperPerson[j];
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    here is the code
    Code:
    #include <iostream>
    using namespace std;
    
    
    
    int main()
    {
    	int i,j;
    	int iAuto[10];
    	int iSales[9][11];
    	int iTotSalesperPerson[11];
    	int TotSales = 0;
    
    	// Reading prices for the Automobiles
    	for(i=0;i<10;i++)
    	{
    	cout << "Automobile "<< i << " Price: ";
    	cin >> iAuto[i];
    	}
    	// Reading Sales for Models & Sales Person
    	for(i=0;i<=10;i++)
    	for(j=0;j<=8;j++)
    	{
    	cout << "Model "<< i << " - Sales Person " << j << ":";
    	cin >> iSales[j][i];
    	}
    	// Sales Calculation for each person
    	for(j=0;j<=8;j++)
    		for(i=0;i<=10;i++)
    	iTotSalesperPerson[j]+=iSales[j][i];
    	cout << iTotSalesperPerson[j];
    
    	// Total Sales
    	for(j=0;j<=8;j++)
    	TotSales += iTotSalesperPerson[j];
    	cout << TotSales;
    	
    
    }
    now it only displays one value filled with garbage i dont understand

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
        int iTotSalesperPerson[11] = {0}; //this initializes all your array elements.
    initialize your variables.
    Last edited by robwhit; 08-07-2007 at 09:46 PM.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    thanks for that info it displays now the total number of automobiles sold for all employees, i need it to display prices and the sales table and calculate the total dollar sales for each salesperson and the total dollar sales for all salespersons how would i start that?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    use loops. try it first and then come back when you have problems.

    http://www.cprogramming.com/tutorial/lesson8.html

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Note that
    Code:
    // Sales Calculation for each person
    	for(j=0;j<=8;j++)
    		for(i=0;i<=10;i++)
    	iTotSalesperPerson[j]+=iSales[j][i];
    	cout << iTotSalesperPerson[j];
    means
    Code:
    for(j=0;j<=8;j++)
    {
    	for(i=0;i<=10;i++)
    	{
    		iTotSalesperPerson[j]+=iSales[j][i];
    	}
    }
    cout << iTotSalesperPerson[j];
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    thanks gents i read the loop but i dont understand how to make it work! can someone show me?

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    What do you want repeated? How many times? Is the number of the repetition it's on important at a given time?

    Are your loops going to count, or are they going to wait for something (other than counting)?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you first start, put braces around all your control statements so that you will know what's being repeated. Also, be careful that you don't use array indexes that are too big or small, as people have stated. For instance:
    Code:
    for(j=0;j<=8;j++)
      for(i=0;i<=10;i++)
    These will use the eighth and tenth items respectively, whereas, a ten item array only has indexes up to nine, and so forth.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by citizen View Post
    Code:
    for(j=0;j<=8;j++)
      for(i=0;i<=10;i++)
    These will use the eighth and tenth items respectively, whereas, a ten item array only has indexes up to nine, and so forth.
    j is 0-8 inclusive, and i is 0-10 inclusive, therefore they are the ninth and eleventh items.

    gunghomiller: the convention is to use the < operator though (for readablility)
    Code:
    for(j=0; j<9; j++)
      for(i=0; i<11; i++)
    if you don't know what to write in code, then write out step by step in english what you want the program to do. try to make it resemble code as much as possible.
    Last edited by robwhit; 08-08-2007 at 12:25 AM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > j is 0-8 inclusive, and i is 0-10 inclusive, therefore they are the ninth and eleventh items.
    That is assuming that I didn't count from the zeroth element, of course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM