Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2018
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question New to C++ and need some help with proper formatting

    I am new to C++ and currently am working on a project for square triangular numbers. I have the logic sorted but my program wont display anything from the cout operator, any help would be appreciated. Source is below for reference, indentation is correctly done.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	// Variable and constant declarations...
    	int triangle_start = 0;			//start value for triangular number
    	int triangle_next = 1;			//next sequential value added to 
    									//triangular number
    	int distance = 1;				//length of each side of the square
    	int area;						//value of distance * distance
    	const int HOW_MANY = 4;			//how many square triangular numbers printed
    	int count = 1;					//loop counter initialized at 1
    	
    	//Determining the square triangular numbers
    	while (count <= HOW_MANY)
    	{
    
    		area = distance * distance;
    
    		if (triangle_next < area) // triangular number < square
    
    		{
    			triangle_start += triangle_next;
    			triangle_next++;
    		}
    
    		else if (triangle_start > area) // triangular number > square
    
    		{
    			distance++;
    		}
    
    		else if (triangle_start == area) // triangular == square
    
    		{
    			cout << "Square triangular number" << count << "is" << area << endl;
    			count++;
    			distance++;
    		}
    
    		else
    
    		{
    			return 0;
    		}
    		
    	}
    	
    	
    	//exit
    	return 0;
    }
    Last edited by vinyl-junkie; Feb 4th, 2019 at 06:18 PM. Reason: added code tags

  2. #2
    Regular Coder
    Join Date
    Oct 2015
    Posts
    427
    Thanks
    1
    Thanked 54 Times in 51 Posts
    Non of your if conditions are true on first pass, so the program skips to the last else statement and exits the program.

    Run the program through a debugger with single stepping and you'll see what happens.

  3. Users who have thanked wha for this post:

    Ouroborous (Feb 5th, 2019)

  4. #3
    New to the CF scene
    Join Date
    Oct 2018
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for your assistance, I should have caught that error during the build phase.


 

Tags for this Thread

Posting Permissions

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