Thread: outputting an array

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    outputting an array

    Code:
    void printoutput(const float prices[], const char taxable[], const int lines)	
    {
    	int i;
    
    
    	puts("Price Taxable");
    	puts("----- -------");
    
    
    	for(i = 0; i<=lines; i++){
    	printf("%2.2f%5c\n",prices[i],taxable[i]);
    	}
    
    
    }
    if lines is equal to 6, then its going to print out 7 elements (the last one being garbage value). I know I can do for(i = 0; i<=(lines-1); i++) but is there an alternative?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The typical pattern is:
    Code:
    for (i = 0; i < lines; i++)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    oh man, i feel so dumb, lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array not outputting properly
    By NinjaFish in forum C Programming
    Replies: 9
    Last Post: 04-16-2011, 11:51 PM
  2. Help: Outputting to char array with sprintf
    By todd_v in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 04:16 PM
  3. Outputting an Array, what's wrong...?
    By fp123 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 03:22 PM
  4. outputting words in array of text
    By hopeolicious in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2005, 12:28 PM
  5. What am I doing wrong outputting this array
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2003, 05:32 PM