Output within a for loop rectangle

Hey guys this is my very first post here looks like a nice site,it helped me a bit when doing my project.anyway my problem is that i have created a rectangle or box with nested for loops and if else statements,i have a next program written that is working but i need to print the out put of this program to out put the calculations within a frame of asterisk's..the program uses switch statements (case) in calculating different answers....finally how can i make all the outputs in each case has the for_loop box around it? Any advice would be greatly appreciated! here is the for_loop code:

1
2
3
4
5
6
7
8
9
10
11
12
13
for(int down = 0; down < 20; down++)
{
for(int across = 0; across < 40; across++)
  {
if ( down == 0 || down == 19)
  printf("*");
 else if (across == 0 || across ==39)
 printf ( "*");
 else printf(" ");
 
  }
  printf(" \n");
}
anyone
Why not just insert this code into your other program?
where would that code go jjjunk?,the program ask's for input then clears the screen for the output (3 possible results) each of which are within a switch case structure.
 
if ( down == 0 || down == 19)


maybe should be:
1
2
3
4
for(int down = 1; down <= 20; down++) {
     if ( down <= 19)
     //...
}


 
else if (across == 0 || across ==39)


maybe should be:
 
if (across <= 39)


CMIIW
i dont quite understand what your saying chipp
Try making your own print function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iomanip>
#include <iostream>

void PrintNum(double d, int width, int rows)
{
  // Top line
  for (int i = 0; i < width; ++i)
    std::cout << '*';
  std::cout << std::endl;
  
  // Middle lines
  for (int i = 0; i < rows; ++i)
  {
    std::cout << '*'; // First character
  
    if (i == rows/2) // If in the middle of rows, print the number
      std::cout << std::setw(width-2) << d;
    else // Otherwise print blank spaces
      for (int j = 0; j < width-2; ++j)
        std::cout << ' ';
  
    std::cout << '*' << std::endl; // Last character
  }
  
  // Bottom line
  for (int i = 0; i < width; ++i)
    std::cout << '*';
  std::cout << std::endl;
}


Then use it like so:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	double pi = 3.14159;
	PrintNum(pi, 40, 9);
}
****************************************
*                                      *
*                                      *
*                                      *
*                                      *
*                               3.14159*
*                                      *
*                                      *
*                                      *
*                                      *
****************************************
Last edited on
Topic archived. No new replies allowed.