| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Can anyone help me with this C++ problem?
#include<iostream>
using namespace std; void main() { int userInput; char userChar; char mySpace = ' '; cout << "Enter a number:" << endl; cin >> userInput; cout << "Enter any character:" << endl; cin >> userChar; if (userInput % 2 == 0) { userInput++; } for(int i=1; i <= userInput; i=i+2) { for(int j=1; j <= (userInput-i)/2; j++) { cout << mySpace; } for(int k=1; k <= i; k++) { cout << userChar; } cout << endl; } } This prints a diamond, well, half of it. I can get the top part to work fine but cant get the bottom to "close" ____*____ ___***___ __*****__ thats as far as it gets. I dont know how to write the loop to decrement it to make it close. Can anyone help? |
|
#2
|
|||
|
|||
|
Hehe, kool.... I've not seen anything like this before, so if I get a chance later, I'll take a look at it, k?
|
|
#3
|
|||
|
|||
|
Alright, I *think* I got what our looking for... Add this loop just after the first for loop:
for(int i=userInput; i >= 0; i=i-2) { for(int j=1; j <= (userInput-i)/2; j++) { cout << mySpace; } for(int k=1; k <= i; k++) { cout << userChar; } cout << endl; } |
|
#4
|
|||
|
|||
|
This should work:
Code:
#include<iostream.h>
int userInput;
char userChar;
char mySpace = ' ';
void input()
{
cout << "Enter a number:" << endl;
cin >> userInput;
cout << "Enter any character:" << endl;
cin >> userChar;
if (userInput % 2 == 0)
{
userInput++;
}
}
void print1()
{
for(int i=1; i <= userInput; i=i+2)
{
for(int j=1; j <= (userInput-i)/2; j++)
{
cout << mySpace;
}
for(int k=1; k <= i; k++)
{
cout << userChar;
}
cout << endl;
}
}
void print2()
{
for(int i=userInput; i >= 0; i=i-2)
{
for(int j=1; j <= (userInput-i)/2; j++)
{
cout << mySpace;
}
for(int k=1; k <= i; k++)
{
cout << userChar;
}
cout << endl;
}
}
int main()
{
input();
print1();
print2();
return 0;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Can anyone help me with this C++ problem? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|