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;
}

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;
}

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.