This is my first C++ course, My assignment is about prompting a user to enter height and character as inputs, and output should be a diamond shape. I think I finished the program, but my problem is that something seems to be off with my top triangle spacing. Theoritically impliment code should implement the correct spacing(I thiink), but the promt shows otherwise. Any suggestions would be appreciated. Thanks in advance.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int w; //spacing
int ccount=1; //character count
int z; //#characters/line limiter
int h; //height input
int counter=1; //counter
char c; //character input
while(cin)
//user prompt section
{
cout<<"Enter Print Character:\n\n";
cin>> c;
cout<<"Enter an odd number for height:"<<"\n\n";
cin>>h;
while(h%2==0)
{
cout<<"You have entered an even number for height, Please enter an odd number: ";
cin>>h;
}
h=abs(h);
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int limit;
int fill=1;
int spaces;
cout<<"Enter an uneven number: ";
cin>>limit;
while(limit<0 || !(limit%2))
{
cout<<"\nError.Try again\n";
cout<<"\nEnter an uneven number: ";
cin>>limit;
}
cout<<'\n';
spaces=((limit-1)/2);
for(int i=1;i<=((limit/2)+1);i++)
{
cout<<' ';
for(int j=1;j<=spaces;j++)
cout<<' ';
for(int k=1;k<=fill;k++)
cout<<'*';
fill+=2;
--spaces;
cout<<'\n';
}
fill-=4;
spaces=1;
for(int i=1;i<=(limit/2);i++)
{
cout<<' ';
for(int j=1;j<=spaces;j++)
cout<<' ';
for(int k=1;k<=fill;k++)
cout<<'*';
fill-=2;
++spaces;
cout<<'\n';
}
cout<<endl;
return 0;
}
I guess that s' what you are trying to do? By the way,everyone have a unique style for coding but i think there is no point of writing something like int h=2 //height when you can use height as an identifier directly.This makes your program auto-documented and easier to read.If you feel lazy to type long identifiers you can also use your IDE to find and replace text once you have finished your program.Cheers.
First I would like to thank you for taking the time to respond. I was, however, trying to understand what was going wrong with my spacing using a simple setw, since we havent got to the 2-dim sections of the course I couldnt really use your suggested approach.
I wanted to update you guys on my progress though;
here is how I fixed the problem , using this as my final code;
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int width; //spacing
int charactercount=1; //character count
int z; //#characters/line limiter
int height; //height input
int counter=0; //counter
char printcharacter; //character input
while(cin)
//user prompt section
{
cout<<"Enter Print Character:\n\n";
cin>> printcharacter;
cout<<"Enter an odd number for height:"<<"\n\n";
cin>>height;
while(height%2==0)
{
cout<<"You have entered an even number for height, Please enter an odd number: ";
cin>>height;
}
height=abs(height);
//reset before reprocess............................................................
counter=0;
charactercount=1;
}
return 0;
}
Although my understanding of setw, was the assigned spaces for characters + remaining empty spaces to the left of the characters. then it seems that its only the case when u use it in the form cout<<setw(x)<<printcharacter; and when used in separate commands like, cout<<setw(x); cout<<printcharacter; the setw(x) will assign x spaces to the left and then print the character to the right of the assigned spaces.
Would appreciate if you can confirm whether im positive about the setw now or I still got the wrong concept about it... Thanks again guys, c++ turns out to be more fun than hardware coding.
You have to use setw right before an I/O instruction,unlike setprecision,fixed,showbase,showpoint,etc.Sometimes you may want to do this:
cout<<setw(width)<<' ';
just as a trick but it is error prone.You can also use cout.width(width) but again it needs to be used before any I/O instruction intended to use it.You can also use the stream manipulators left and right to adjust the text,right is the value by default.