Results 1 to 5 of 5

Thread: C++ Arrays

  1. #1
    Join Date
    May 2007
    Beans
    137

    C++ Arrays

    I'm having trouble adding asterisks (*) to the end of my lines. Here's what it's supposed to look like

    Code:
    Belvidere        **********
    Freeport         ********
    Byron            ************
    Stillman Valley  ***************
    Rockford         *********
    Each * represents a point each city has. Here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    // constants
    // Dimensions beyond first must be global
    const int CITY_SIZE = 20;
    
    int main(void)
    {
        // constants
        const int SIZE = 5;
    
    
        // function prototype(s)
        // return by value is used
        void printStars(const int [], const char [][CITY_SIZE], const int);
    
        // local data (other than arrays)
    
        // array data declaration(s) & initialization(s)
        // array data: use data from sample output above
        // TODO #1: declare & initialize array of ten integers
    	int points[] = {10,
    					8,
    					12,
    					15,
    					9};
    
        // TODO #2: declare & initialize array of five cities
    
    	char cities[][CITY_SIZE] = {"Belvidere",
                                    "Freeport",
                                    "Byron",
                                    "Stillman Valley",
    								"Rockford"};
    
        // start the program
        cout << "*** start of 276Arrays_Ex04.cpp program ***" << endl;
        cout << endl;
    
        // display the required output
        // TODO #3: call display function, passing count array & size
        printStars(points, cities, SIZE);
    
        // terminate the program
        cout << endl;
        cout << "*** end of 276Arrays_Ex04.cpp program ***" << endl << endl;
        cin.get();
    
        return 0;
    }   // end main()
    
    //--------------------------------------------------------------------//
    // Function Name: printStars(const int[],
    //                            const char [][], const int)
    // Purpose: to display cities and their points in a graph
    // Values received: city points array, citie names array, size
    // Values returned: <none>
    // Notes:
    //               Prints column headings
    //               Loop until all cities processed
    //                  print row label (name of one city)
    //                  Loop until all stars printed for one city
    //                     One star is printed
    //                  End inner loop
    //                  New line
    //               End outer loop
    //--------------------------------------------------------------------//
    // TODO #4: code the display function
    void printStars(const int points[],
                    const char cities[][CITY_SIZE],
                    const int size)
    {
       
       // print column headings
       cout << left << setw(15);
       cout << "City";
       cout << setw(2) << " "; // insert 2 spaces between
       cout << left << setw(21);
       cout << "City Points";
       cout << endl;
       cout << endl;
       cout << "---------------";
       cout << setw(2) << " "; // insert 2 spaces between
       cout << "1---5----10---15---20";
       cout << endl;
    
    
       // print detail lines
       for (int i = 0; i < size; i++)
       {
          // print city name
    
    	cout << cities[i];
    	cout << endl;
    
          // nested for loop
    	for (int stars = 0; stars < points[i]; stars++)
       {
          // print stars for the city
    
     
    
    	}
       }
    }
    The problem lies at the very bottom in the "print stars for the city" I've tried everything but I never get the * next to the names like they are supposed to. Any suggestions?

  2. #2
    Join Date
    Nov 2009
    Beans
    1,081

    Re: C++ Arrays

    Code:
            cout << cities[i];
    	cout << endl;
    Ask yourself what this does.

  3. #3
    Join Date
    May 2007
    Beans
    137

    Re: C++ Arrays

    Quote Originally Posted by Some Penguin View Post
    Code:
            cout << cities[i];
    	cout << endl;
    Ask yourself what this does.
    I tried cout << cities[i] << "*" << endl;

    but how do get the correct amount of * by each specific city?

  4. #4
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C++ Arrays

    The endl writes a newline to stdout, so you would probably want to write the *s before it. As for writing the correct amount - It looks like you have a for loop that will do nicely .
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  5. #5
    Join Date
    Aug 2009
    Beans
    105

    Re: C++ Arrays

    yes agree with the previous post..
    you are using endl, which will put a new line of the output screen...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •