nickwelsh1989
nickwelsh1989

Reputation: 1

how do I fix this code

I am taking an online course.

The homework question is:

Use the rand() function to print the first 50 numbers between 1 and 100. Try to put every 5 random number in one line only. Use setw() function to align.

I currently have the following. I know that it is an infinite loop and the error I think the problem is in my if statement.

#include <iostream>
#include <time.h>
#include <iomanip>

using namespace std;

int main() {
    int random_number;

    srand(time(NULL));

    random_number = rand() % 100 + 1;
    cout << "50 random numbers are:" << endl;
    while (random_number < 100, rand() % 100 + 1)
    {
        cout << random_number << setw(5) << endl;
        if (random_number == 100)
            break;
    }
    system("pause");
}

Upvotes: 0

Views: 141

Answers (4)

Mercurial
Mercurial

Reputation: 3885

I understand many of above codes are good for op. For as we can see op is new to coding here is my simplified attempt.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main() {
    int random_number, count=0; //Using count as a counter.
    srand(time(NULL));

    cout << "50 random numbers are:" << endl;
    //checking two conditions. count and random number generated is less than 100
    while (count <50) 
    {
        if((random_number = rand() % 100 + 1) > 100 ) continue;
        count++;
        cout<< setw(5) << random_number ;
        //Try to put every 5 random number in one line only.
        if (count %5==0) cout<<endl; 
    }

}

Problems with your code:

  • You were generating random number only once since it was outside the while loop.
  • No correct stopping criteria of while loop. Giving infinite loop.
  • Your solution formatting wasn't as asked in the question.

Thanks.

Upvotes: 1

sehe
sehe

Reputation: 393934

Since this is homework, I cannot post a straight answer. However, this should count as a good demonstration of modern c++:

See it Live On Coliru

#include <boost/spirit/include/karma.hpp>
#include <boost/iterator/function_input_iterator.hpp>
#include <boost/random.hpp>

boost::random::variate_generator<boost::mt19937, boost::uniform_int<int> > rdngen(
        boost::mt19937(), boost::uniform_int<int>(1, 1000));

int main() {
    auto f = boost::make_function_input_iterator(rdngen, 0),
         l = boost::make_function_input_iterator(rdngen, 100);

    auto range = boost::make_iterator_range(f, l);

    using namespace boost::spirit::karma;
    std::cout << format_delimited(columns(5) [ right_align(10) [ *auto_ ] ], "\t", range) << "\n";

}

Upvotes: 0

I think that for what are you posting, that you want to print 50 random numbers between 1 and 100, isn't it? If this is so, I notice the mistake is in the leave condition of the while loop. You need a variable for counting the displayed numbers and you should check if this variable has reached the 50 displayed numbers.

srand(time(NULL));

random_number = rand() % 100 + 1;
cout << "50 random numbers are:" << endl;
for (int i = 0;i<50;++i)
{
   random_number = rand() % 100 + 1
   cout << random_number << setw(5) << endl;
//   if (random_number == 100)
//       break;
}
system("pause");

In the way you have coded the leaving condition it is posible that never leaves. It is necessary that you get a 100 for exiting the loop

I hope this post was helpful for you Thanks regard.

Upvotes: 0

vsoftco
vsoftco

Reputation: 56577

Your while is faulty, see previous comment. This should work.

#include <iostream>
#include <time.h>
#include <iomanip>

using namespace std;

int main() {
    int random_number;

    srand(time(NULL));

    cout << "50 random numbers are:" << endl;
    for(int i=0; i<50; i++)
    {
        random_number = rand() % 100 + 1;
        cout << setw(5) << random_number << endl;
    }
    system("pause");
}

or, if you want to use a while,

#include <iostream>
#include <time.h>
#include <iomanip>

using namespace std;

int main() {
    int random_number;

    srand(time(NULL));

    cout << "50 random numbers are:" << endl;
    int counter=0;
    int random_number;
    while(counter<50)
    {
        counter++; 
        random_number = rand() % 100 + 1;
        cout << setw(5) << random_number << endl;
    }
    system("pause");
}

but just use for as it is more compact.

Upvotes: 1

Related Questions