For Loops

please do not hate me for asking this again,
it was just the ones i could find on here were not the same to the problem i was asked.
every ones changing variable is there rows where mine is the base number of symbols.
Write a C++ program that gets two inputs an integer and a character. Use repetition control structures better known as "loops" to output a triangle shape composed of the character and the width specified by the integer. If the input is an even number, it should be increased to the next odd number. Use meaningful variable names, nested loop statements with proper indentation, appropriate comments, and good prompting messages.

For example, if the integer is 11 and the character is an asterisk (*), the triangle shape would look like this:

Sample screen output 1:


Enter a value to represent the base of a triangle shape (not to exceed 80): 11
Enter the character to be used to generate the triangle shape (for eg., #, * $): *
-----*
----***
---*****
--*******
-*********
***********
Do you want to quit the program? (type n for no or q to quit): q



now my understanding of this question was that he wanted us to keep it at 6 rows
but the max amount of symbols is 80, which seems it would get really crammed and not work out too well.

if any one is willing to help me with this id really appreciate it, sorry for asking again.
here's my code so far
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Assignment 4.2
//Triangle
#include <iostream>

    using namespace std;

    int main()
{ 

	const int NUM_ROWS = 6;          
	const int MAX_symbols = 80;	                  

	int baseNum;	
    int numSpaces;                    
	int	numsymbols;                   
    char symbol;
    
        cout <<"Enter a value to represent the base of a triangle shape" 
             <<"(not to exceed 80): "<<endl;
          cin>>baseNum;
              if (baseNum>=80 || baseNum<=0)
   {  
        cout<<"Please enter a positive integer less than 80"<<endl;
   }
   else
        cout <<"Enter the character to be used to generate the triangle shape"
             <<" (for eg., #, * $): "<<endl;
          cin>>symbol;
 if(!baseNum%2==1)
 {
     baseNum=baseNum+1;
}
  
	for (int row = 0; row < NUM_ROWS; row++)//num of rows
	{
		
		numsymbols = 2 * row - 1;     
        numSpaces = (baseNum - numsymbols) / 2; 

	
		for (int space = 0; space <(numSpaces-row); space++)
		{
            	cout << ' ';
             }

		
		for (int plus = 0; plus < baseNum; plus++)
{
        for (int temp = 0; temp < plus+1; temp++)
{
       cout << symbol;
}
}
		
       
          cout<<endl;
}
        system("PAUSE");
	return 0;
}



im using dev C++
Last edited on
OUT PUT
Enter a value to represent the base of a triangle shape(not to exceed 80):
11
Enter the character to be used to generate the triangle shape (for eg., #, * $):

*
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
Press any key to continue . . .
closed account (S6k9GNh0)
Using some tools, I formatted the code and added some different functionality. However, it's still broken but much closer. If you can figure out the bug here, you'll be good:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Assignment 4.2
//Triangle
#include <iostream>
#include <sstream>
#include <string>

#define NUM_ROWS 6
#define MAX_SYMBOLS 80
//In my opinion, pre-compiled defines are cleaner than constant integers.


int main() 
{
    std::string input;
    std::stringstream myStream;
    unsigned short numBase;
    char symbol;
    
    //Btw, if you use these for loops, please be sure and learn what they do. If not, change it back to what you had. 
    //It will be difficult to explain how you made this code yourself. This code was actually provided by someone else on the forum.
    for (;;)
    {
        std::cout << "Enter a value for the size of the base of the pyramid: ";
        std::getline(std::cin, input);

        std::stringstream myStream(input);

        if (myStream >> numBase && numBase <= MAX_SYMBOLS)
            break;
        std::cout << std::endl << "Invalid number. Number must be positive and below " << MAX_SYMBOLS << " . Please try again." << std::endl;
    }
    
    for(;;)
    {
        std::cout << "Please enter a symbol to build the pyramid with: ";
        std::getline(std::cin, input);

        if (input.length() == 1 )
        {
            symbol = input[0];
            break;
        }
        std::cout << std::endl << "Invalid character, please try again." << std::endl;
    }

    for (int row = 0; row < NUM_ROWS; row++)//num of rows
    {
        int numSymbols = 2 * row - 1;
        int numSpaces = (numBase - numSymbols) / 2;


        for (int i = 0; i < numSpaces; ++i)
            std::cout << ' ';

        for (int i = 0; i < numSymbols; ++i)
            std::cout << symbol;

        std::cout << std::endl;
    }

    std::cin.get();
    return 0;
}


The key to solving the bug is simply stepping through the logic. You can do this mentally or perhaps learning the basics of a debugger would be of use to you.
Also, stop using Dev-C++: http://cplusplus.com/forum/articles/36896/
Last edited on
Topic archived. No new replies allowed.