So i am new to c++, I don't know what is wrong with my program, I keep looking for the issue but can't find why it won'r run. So i am trying to develop a program which reads the numbers the user inputs and convert those into rows of asterisks. The limits for height is 5 and for length is 60. If i type -1, then it should exit and show the user how many numbers i input (e.g 1 2 3 4 5 6 3 2 1 = 9 number) and should show the rows of asterisk depending on each number. It keeps telling me to use pointers, i know what pointers are but dont know how to apply them to my program.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ios>

using namespace std;

int main()
{

    const int MAX=60;
    int num, count = 0;
    count[MAX];
    cout << "enter a list of positive numbers, enter -1 to end the list: \n";


    do
    {   

        cin>> num;

        if (num >= 0)
        {
            count++;
        }


        else if ( num == -1)

        {
                   for (int i = 0; i < MAX; i++)

        {

              cout << " you entered "<< count <<" numbers\n";
              cin >> count[i];


        }        
        for(int y=0;y<5;y++)
        {

                for(int x=0;x<MAX;x++)
        {

               if (count[x]>=20-y)


                   cout<<"*";

               else cout<<" ";
         }




                       exit (0);
               }
    }

    while (num != 0);



    system("pause");
    return 0;
}

Dani AI

Generated

The posted code has a few clear root causes that explain why it doesn't run and why the output is wrong: a scalar counter and an array share the same name, there is no proper array declaration, the loop/sentinel logic is inconsistent (the code checks while(num != 0) but the sentinel was -1), input is read in the wrong place, and a magic constant (20) is used where the stated height is 5. As noted, count[MAX] is not a valid declaration when count is already an int. 's example shows a clean design for rows; below is a compact alternative that stores inputs and prints a vertical histogram while demonstrating simple pointer iteration.

A safe, minimal plan:

  • Declare an array to hold up to 60 values and a separate int n = 0 to count inputs.
  • Read numbers with while (cin >> num && num != -1), validate range, and store into the array while checking bounds.
  • After input ends print how many numbers were entered.
  • To draw a vertical chart (height = 5): loop rows from 5 down to 1; for each stored column print '*' if the column value >= current row, otherwise print a space. Use a pointer to iterate the stored values if the instructor wants pointer practice.

Example (original, compact):

#include <iostream>

int main() {
    const int MAX = 60;
    const int MAX_HEIGHT = 5;
    int values[MAX] = {0};
    int num, n = 0;

    std::cout << "Enter non-negative numbers (0.." << MAX_HEIGHT << "), -1 to finish:\n";
    while (std::cin >> num && num != -1) {
        if (num >= 0 && num <= MAX_HEIGHT) {
            if (n < MAX) values[n++] = num;
            else { std::cerr << "Max columns reached\n"; break; }
        } else {
            std::cerr << "Out of range: must be 0.." << MAX_HEIGHT << '\n';
        }
    }

    std::cout << "You entered " << n << " numbers\n";

    for (int row = MAX_HEIGHT; row >= 1; --row) {
        for (int *p = values; p < values + n; ++p)
            std::cout << ((*p >= row) ? '*' : ' ');
        std::cout << '\n';
    }
}

Notes: avoid system("pause") and exit() inside logic, use descriptive names (values, n) to prevent confusion, and consider std::vector<int> if dynamic sizing is needed. The pointer loop above is a simple, readable way to practice pointers while keeping bounds checking explicit. 's approach is still useful if horizontal rows as C-strings are preferred.

Recommended Answers

All 2 Replies

line 13 : what type is count[MAX]?
It is also not a good idea to name a counter and an array with the same name. This can lead to confusion and unneeded errors.

This example to a similar problem may help you to rethink / restart your code.

// rowsOfStars.cpp //

// demo of a way to accept ONLY VALID inoput  ...//

/*
    So i am new to c++, I don't know what is wrong with my program,
    I keep looking for the issue but can't find why it won'r run.

    So i am trying to develop a program which reads the numbers
    the user inputs and convert those into rows of asterisks.

    ...

*/


#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring> // re. strlen //

using namespace std;

const unsigned COLS = 60, ROWS = 5;


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    cout << msg << flush;
    int chr = cin.get();
    if( chr != '\n' ) while( cin.get() != '\n' ) ; /* flush stdin ... */
    return chr;
}
bool more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return false;
    /* else ... */
    return true;
}



int main()
{
    char ary[ROWS][COLS+1] = {0}; // + 1 to '\0' terminate //

    unsigned numStars;
    unsigned row = 0, col;
    for( ; ; )
    {
        cout << "How many stars for row[" << row
             << "] (valid entries here 0.." << COLS << ") : " << flush;
        if( cin >> numStars && cin.get() ) // accepts only valid unsigned numbers //
        {
            if( numStars <= COLS ) // check if within valid range ... //
            {
                for( col = 0; col < numStars; ++col )
                    ary[row][col] = '*';

                ++ row;
            }
            else
                cout << "Max entry here is " << COLS << " ... try again ...\n";
        }
        else
        {
            cin.clear();
            cin.sync();
            cout << "Invalid entry ... ONLY positive int's are valid here ...\n";
        }

        if( row == ROWS  )
        {
            cout << "Now empty rows ... \n";
            break;
        }
        if( !more() )
            break;
    }

    // now can show rows as C strings ... //

    for( unsigned r = 0; r < row; ++ r )
        if( ary[r][0] != 0 ) cout << "row[" << r << "] len "
            << strlen(ary[r]) << " is: " << ary[r] << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.