Alright. Basically the point of this program is to allow a user to select how many fractions they want printed on the screen. It is supposed to print 3 fractions PER line. If a user requests 4 fractions..it would print 3 and print the 4th fraction by itself on the next line.

The numerator is any number between 1 - 10. Denominator is any number between 1 - 20. Currently...with what I have it gets stuck in an infinite loop and I can't quite put my finger on what is causing it.

On a side note, I'm really rusty with C++. It's been years.

#include <iostream>
#include <cstdlib> 

using namespace std;

struct Fraction
{
	int num;  //non-negative number
	int den; //non-negative number
};

int main(void)
{
	Fraction *intList;
	int arraySize;
	
	cout << "How many fractions would you like to display? ";
	cin >> arraySize;
	cout << endl;
	
	intList = new Fraction[arraySize];
	
            //initializing values of the array
            for(int i=0; i<arraySize; i++)
            {
                    intList[i].num = (rand()%10)+1;
                    intList[i].den = (rand()%20)+1;
            }
    
                    for(int z=0; z<arraySize; z+3)
                    {
                  
                     cout << intList[z].num << "/" << intList[z].den << "     ";
                     if (z+1 > arraySize)
                     {
                             break;
                     }
                     else if (z+1 < arraySize)
                     {
                       
                         cout << intList[z+1].num << "/" << intList[z+1].den << "     ";
                     }
                     if (z+2 > arraySize)
                     {
                             break;
                     }
                     else if (z+2 < arraySize)
                     {
                       
                         cout << intList[z+2].num << "/" << intList[z+2].den << "     " << endl;
                     }
                    }

system("PAUSE");	
return 0;
}

Recommended Answers

All 4 Replies

Your problem lies here: for(int z=0; z<arraySize; z+3) the statement z + 3 does not change the value z.

Perhaps you meant to say z += 3 But that would not work well. z should be counting up by one, and your tests in the loop should test z against 3, not arraySize. (actually, test if z is a multiple of three, and your program would be able to print any number of fractions, up to three per line.)

Alright, so I amended this a little bit. Now when I attempt to print it will do 1 fraction on the first line and end the line, even if there is a remainder. It acts like it is divisible by 3 with 0 as the remainder. Take a look at this and see if you spot something off:

#include <iostream>
#include <cstdlib> 

using namespace std;

struct Fraction
{
	int num;  //non-negative number
	int den; //non-negative number
};

int main(void)
{
	Fraction *intList;
	int arraySize;
	
	cout << "How many fractions would you like to display? ";
	cin >> arraySize;
	cout << endl;
	
	intList = new Fraction[arraySize];
	
            //initializing values of the array
            for(int i=0; i<arraySize; i++)
            {
                    intList[i].num = (rand()%10)+1;
                    intList[i].den = (rand()%20)+1;                    
            }
    
                    for(int z=0; z<arraySize; z++)
                    {
                      if (z%3!=0)
                      {
                                cout << intList[z].num << "/" << intList[z].den << "     ";
                      }
                      else if (z%3==0)
                                cout << intList[z].num << "/" << intList[z].den << "     " << endl;
                    }

system("PAUSE");	
return 0;
}

what is 0 % 3 ?

A simpler structure will simply print the fraction, then test if a newline should be output.

for(     )
{
    cout << ....the fraction
    if( (z+1) % 3 == 0 )  //test with +1 to avoid the 0 problem
         cout << endl;
}

Ah ha! I got it now. :) Thanks so much for your help.

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.