[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Check??

Can someone check if this program is right??

Use nested for loops to generate this output
 Enter size of square (or 0 to exit): 5
*****
*****
*****
*****
*****

handle errors, enter o to exit program.

#include <iostream.h>
#include <stdlib.h>


int main()
{
     int rows, col;
     int size;

     cout<<"Enter size of square (or 0 to exit): ";
     cin >>size;

     for (rows = 0; rows < size; rows++)
{
     for (col = 0; col < size; col++)
     {
          cout << '*' ;
     }

     cout<< "\n";
}



      system("PAUSE");
      return 0;
}

by the way, how to write code in order to enter 0 for exit, and handle error
0
cuong5985
Asked:
cuong5985
  • 3
  • 2
  • 2
  • +1
1 Solution
 
AxterCommented:
Hi cuong5985,
> >#include <iostream.h>
<iostream.h> is not part of the C++ standard.
Use <iostream>  instead.

David Maisonave :-)
Cheers!
0
 
InteractiveMindCommented:
Just about there... give this a go:

#include <iostream.h>
#include <stdlib.h>


int main()
{
     int rows, col;
     int size;

     cout<<"Enter size of square (or 0 to exit): ";
     cin >>size;
     
     if ( size <= 0 )
     {
          return -1;
     }
     
     for (rows = 0; rows < size; rows++)
{
     for (col = 0; col < size; col++)
     {
          cout << '*' ;
     }

     cout<< "\n";
}



      system("PAUSE");
      return 0;
}


Regards;
0
 
InteractiveMindCommented:
..ah, didn't notice that, David. :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mactep13Commented:
cout<<"Enter size of square (or 0 to exit): ";
     cin >>size;

     for (rows = 0; rows < size; rows++)
0
 
cuong5985Author Commented:
Thanks for all your help
0
 
mactep13Commented:
Sorry about above comment...
Use this...

cout<<"Enter size of square (or 0 to exit): ";
     cin >>size;
     if (0 == size)
     {
           cout << "Exiting" << endl;
           // Handle errors how you see fit...
          exit(0);   // Will exit your program
     }
     for (rows = 0; rows < size; rows++)
0
 
cuong5985Author Commented:
Thanks mactep13 for helping me out
0
 
cuong5985Author Commented:
By the way, help me out with this also, i was lost for this.

Use nested loops to generate this output.
Enter a positive number for the height of the triangle: 5
*
**
***
****
*****
****
***
**
*
User enters 0 to exit the program.


#include <iostream.h>
#include <stdlib.h>

int main()
{
     char outer, inner;
     int size;

     cout<<"Enter a positive number for the height of the triangle (or 0 to exit): ";
     cin >>size;
     for (outer = '*' ; outer >= '*' ; outer--)
     {
     for (inner = '*' ; inner <= outer; inner++)
     {
          cout <<inner;
     }
     cout<< "\n";
}
     cout<< "\n";
}
 
      system("PAUSE");
      return 0;
}

With my program, i just can do the following output
*****
****
***
**
*

How can i do with this in order to complete the solution just like the output i wrote above.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now