0

Rather than using setw why not just print the number of spaces you need:

+  (3 spaces, 1 character)
    +  (3 spaces, 1 character)
    +
  +++++ (4th line down print across no spaces)
    +
    +
    +

Adjust accordingly (it's not quite balanced but you get the idea)

Edited by jonsca: n/a

0

There are many ways to do the plus sign...

If you chose to use the setw() function, be sure to #include<iomanip>

You could make a char[] array of spaces (c-string)

If you chose not to use setw(), you could use a string literal consisting of a bunch of spaces....

Or you can make a string object and assign it a bunch of spaces.

You could use nested loops.. but in this case, for me personally, I would focus on constructing each part of the plus sign individually:

#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

int main()
{

     //using a null-terminated c-string
     char spaces[10] = {'','','','','','','','','','\0'};

     //Build the vertial line (top half)
     for(int i=0; i<10; i++)
     {
          cout << spaces << '*' << endl;
     }

     //Now draw the horizontal line
     cout << "**********";

     //Draw the bottom half of plus
     //Just do the same thing as the top half

     return 0;
}

I know this isn't exactly what ye' are looking for, but it should give you an idea of just one way (out of infinite possibilites) of how to draw this thing...

You have many ways to do the same thing.. you could use multiple loops, you could use a nested loop.. you could use no loops at all. So how do you know what to use? Use what you think would be the easiest and most effective methods to accomplish the goals of your assignment.

Edited by Clinton Portis: n/a

0

im trying to put a verticle line in the middle of this line, but the verticle line must correspond with the number the user chooses. i know i would have to nest it but every time i nest it the verticle line does not go through the horizontal line

horizontal line is this one it corresponds with the number the user chooses

{
      for(int h=1; h<=number; h++)
          cout << symbol;
 }

this is the verticle line, yeah i used ur method mr.portis. the only thing is it doesnt correspond with the number the user chooses.

char spaces[10] = {' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
for(int i=0; i<10; i++)
     {
          cout << spaces << '*' << endl;
     }
0

but the verticle line must correspond with the number the user chooses

A loop is an excellent way to control dos console output based on user input. Additionally, use of setw() will allow you to scale the plus sign based on user specified dimensions.

Compile, run, and experiment with the following code:

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
     int size = 0;

     cout << "Enter size of plus sign: ";
     cin >> size;

     //Draw vertical line (top half)
     for(int i=0; i<size/2; i++)
     {
          cout << setw(size/2) << '*'  << endl;
     }

     //Now draw the horizontal line
     for(int i=0; i<size; i++)
     {
          cout << '*';
     }

     //Now draw the bottom 1/2 of the plus

     return 0;
}
0

this plus sign i cant get it to move from the side of the screen anyone got any ideas that can help me figure out wat i am doing wrong.

for(int i=0; i<number/2; i++)
                        {
                            cout << symbol << endl;
                        }
                             for(int i=0; i<number; i++)
                             {
                              cout << symbol;
                             }
                                    for(int i=0; i<number/2; i++)
                                    {
                                      cout << symbol << endl;

}

0

Your plus is almost done.. except you need to move the top 1/2 veritcal line and the bottom 1/2 vertical line off the left edge of the screen.

One way you can get the spaces you need, is to create an array of spaces:

int size = 0;

//Declare the array 
char spaces[80];

//Initialize the array to all spaces
for(int i=0; i<80; i++)
{
     spaces[i] = ' ';
}

cout << "Enter size of plus sign: ";
cin >> size;

//Put a null terminating delimiter in the space array so we will have the correct amount of padding
spaces[size/2] = '\0';

//Now you have a set number of spaces to use to pad your plus sign :)
 for(int i=0; i<number/2; i++)
{
     cout << spaces << symbol << endl;
}

Like I said before.. many ways to do this thing. Seems like you are pretty much done with ye' assignment. Let us know how it goes.

Edited by Clinton Portis: n/a

0

i cant get this to error trap or loop again after the user chooses y or quit after the user chooses n .

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int letter, number =1, triangle, upside_down_triangle, rectangle, plus_sign, answer,m,k,i,y,p,t,s,h,g,width=0,;
char a,b,c,d,e, symbol = '\0',;
cout << "Please choose a letter that corresponds to the shape of your choosing \n";
cout << " A. triangle " << endl;
cout << " B. upside down triangle " << endl;
cout << " C. rectangle " << endl;
cout << " D. plus sign " << endl;
cout << " E. quit " << endl;
cin >> symbol;
cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
cin >> number;

do 
	{
		do 
		{
switch (symbol)
                {
                    case 'a':
                    case 'A':
                    cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
                    cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                        for(int i=0; i<number; i++)
                            {
                                for(int y=-1; y<i; y++)
                                    cout << symbol;
                                    cout << "\n";
                            }
						cout << " Do you wish to continue: (y/n) " << endl;
						cin >> answer;
                    break;
case 'd':
                    case 'D':
                    cout << " Thank you, you have choosen plus sign and " << number << " for height and width" << endl;
                    cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
				     for(int i=0; i<number/2; i++)
                        {
                            cout << symbol << endl;
                        }
                          for(int i=0; i<number; i++)
                             {
                              cout << symbol;
                             }
                               for(int i=0; i<number/2; i++)
                                    {
                                      cout << symbol << endl;
									}
				        cout << " Do you wish to continue: (y/n) " << endl;
						cin >> answer;
					break;

case 'e':
case 'E':
cout << " you choose e, so you must want to quit " << endl;
default:
cout << " please enter a valid response. press y for yes and n for no " << endl;
}

}while (( number!=3) || ( number!=5) ||  (number!=7) || ( number!=9));
			{
				cout << " please enter a valid response. press y for yes and n for no  " << endl;
				cin >> answer;
			}
	}while (( answer!= 'n') ||(answer != 'N'));
		{
			cout << " thank you for your time " << endl;
		}

return 0;
}

1

Force total user compliance: (you call it error trapping)

do{

     //stuff
     //suff
     //and more stuff

     if(is user input correct?)
     {
          //Display error message
     }

}while(user input is incorrect);

To run your program multiple times, do this:

int main()
{
     char choice = '\0';

     do{
  
          //stuff
          //stuff
          //stuff

          cout << "\nWould ye' like to try again?  (Y/N): ";
          cin >> choice;

     }while(choice == 'y' || choice == 'Y');
 
     return 0;
}

Follow these simple topologies and ye' will be good to go.

Edited by Clinton Portis: this isn't rocket science.

Votes + Comments
Mad rep for using the word "topology" in your post
0

can i do something like this and could someone answer my questions as i put as comments in my program. its only 3 or 4

int  number =0, answer,h=0,width=0, setw=0;
char symbol = '\0',spaces[80] = {' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'} ;
 while(1)
    {
        cout << "Please choose a letter that corresponds to the shape of your choosing \n";
        cout << " A. triangle " << endl;
        cout << " B. upside down triangle " << endl;
        cout << " C. rectangle " << endl;
        cout << " D. plus sign " << endl;
        cout << " E. quit " << endl;
        cout << " press q at any time to quit game " << endl;        cin >> symbol;
        cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
        cin >> number;
           switch (symbol)
                {
                    case 'a':
                    case 'A':
                    while(number!=3 && number!=5 && number!=7 && number!=9)/* how can i make this so if the user wants to type char it will say this */                                {                                                      /*could i do this, while(number || char !=3)*/                                 cout << "You have chosen Invalid Number\n";
                                 cout << number << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                 cin >> number;
                                }
                    cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
                    cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                    cout << '\n';
                    for(int i=0; i<number; i++)
                            {
                                for(int y=-1; y<i; y++)
                                    cout << symbol;
                                    cout << "\n";
                            }
                    cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                    cin >> answer;

can i do this ?

    while(1)
    {
        cout << '\n' << "Do you wish to continue: (y/n) " << endl;
        cin >> answer;
        if(answer=='y' || answer=='Y')
        break;/* can you do this , and wats a better way to do it*/
            else if(answer=='n'||answer=='n')
                {
                    cout << "Thank You For Your Time " << endl;
                    return 0;/* can you do this , and wats a better way to do it*/
                }
                    else
                    cout << "Please choose a valid option.Either y or n.";
                }
    }

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.