C++ help please :)

Soybomb

Diamond Member
Jun 30, 2000
9,506
2
81
Okay I spent like 4 hours on this last night and its still not working out for me, so any help you guys can give me would be greatly appreciated.

I'm trying to make a program that will take a odd interget as input and through the use of nested loops make a christmas tree design. All my asterisks must be printed by a single instance of cout<<"*";

l
here is my code so far, please excust how sloppy it is now

i've got it printing out the right number of lines, but if I could just figure out how to write a loop that would make it print the right number of asterisks on each line I could figure out the spaces and formatting myself i think

#include <iostream>
using namespace std;

int main() {

int width;
int blanks;
int quotient;
int product;
int result;
int rows;
int counterw;
int ast;
int rownumber;
int nextline;
int number;

cout << "Enter the width of the base of the tree and press enter.\n";
cin >> width;

counterw = 1;
ast = 1;
number = 1;
nextline = ast;
rows = (width/2) + 1;
rownumber = 1;
blanks = width / 2;

quotient = width / 2;
product = quotient * 2;
result = width - product;

if (result == 0 || width==0 || width >= 80) {
cout << "The number entered was not odd, was 0, or was greater than 80, and a tree will not be drawn. \n";
}
else
cout << "*";
cout << "\n";
while (rows > 1) {
while (ast >=2 ) {
cout << "*";
ast--;
}
nextline = nextline +2;
rows--;
cout << "\n";


}










return EXIT_SUCCESS;
}
 

iamwiz82

Lifer
Jan 10, 2001
30,772
13
81
i had to do a program like this in java for a test. I believe you need 3 nested for loops, one to one for the left side spaces, one for the right side spaces, and one for the astericks. I cant remember how i did it though.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
for(int i = width;i>0;i--){
for(int j = 0; j<=i;<j++){
cout << "*";
}
cout << "\n";
}

Something like that? that's all it looks like you're trying to do.
 

crystal

Platinum Member
Nov 5, 1999
2,424
0
0
I think there is a command in c/c++ where you in a number and letter, symbol, etc. and it will make that many copies. It is been ages since I touch c/c++.
 

Soybomb

Diamond Member
Jun 30, 2000
9,506
2
81
With the input of 5 it give me a
*
blank
blank

Okay notfred, I just checked out that chunk of code and get syntax errors on the second for line for "missing '(' before '<'". To my knowledge that looks like the correct structure
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
oops, typo

there should be no '<' before the j++

edit:

I changed my code to:

for(int i = 0;i<width;i++){
for(int j = 0; j<=i;j++){
cout << "*";
}
cout << "\n";
}

cause the tree was printing upside down.

when I had a tree base width of 15, it printed out the tree 7 times... dont know what's up with that, but it worked.
 

Soybomb

Diamond Member
Jun 30, 2000
9,506
2
81
Nevermind I see you beat me to it

I'm gonna try to tweak it now for it prints the right number of rows and stars per row....i hate nested stuff
 

iamwiz82

Lifer
Jan 10, 2001
30,772
13
81
now, make one that is the exact opposite of fred's and place it in front of the code you already have.
 

Soybomb

Diamond Member
Jun 30, 2000
9,506
2
81
Lol I know this is about all the prgramming fun I can stand for a while

Alright, I think I'm still a little lost, how can I convince it to print the number of rows I have defined, or for that matter add two asterisks to each line it prints instead of one? I'm coming out with some really nasty looking stufff

edit: WAIT WAIT!! !I Got the right number of rows...now to get asterisks...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
if you want it to print two asteriksa instead of one, change: cout << "*"; to: cout << "**";

the number of rows it prints is defined by the number of values in (int i = 0;i<width;i++)

if you want it to print more rows, make width bigger, if you want it to print fewer rows, make width smaller.
 

Soybomb

Diamond Member
Jun 30, 2000
9,506
2
81
Actually I changed it to
for(int i = 0;i<rows;i++){
for(int j = 0; j<=i;j++){
cout << "*";
}
cout << "\n";
}
for the right number of rows, but I'm still fighting asterisks.

I also can't put in cout<<"**"; because we can only use cout <<"*"; and it would also ruin the first line

I think this homework has taken too long..perhpas I shall just skip
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
.globl main
.text
main: addi $v0, $zero, 4
la $a0, ask
syscall
addi $v0, $zero, 5
syscall
add $s1, $zero, $v0

addi $v0, $zero, 4
la $a0, ask2
syscall
addi $v0, $zero, 5
syscall
add $s2, $zero, $v0

addi $v0, $zero, 4
la $a0, ask3
syscall
addi $v0, $zero, 5
syscall
add $s3, $zero, $v0


slt $t1, $s1, $s2
beq $t1, $zero, next
slt $t2, $s2, $s3
beq $t2, $zero, next
add $s1, $zero, $zero
j end

next: slt $t1, $s2, $s1
beq $t1, $zero, next2
slt $t2, $s2, $s3
beq $t2, $zero, next2
add $s2, $zero, $zero
j end

next2: slt $t1, $s3, $s2
beq $t1, $zero, end
slt $t2, $s3, $s1
beq $t2, $zero, end
add $s3, $zero, $zero
j end

end: addi $v0, $zero, 4
la $a0, res
syscall

add $a0, $s1, $s2
add $a0, $a0, $s3

addi $v0, $zero, 1
syscall

.data
ask: .asciiz "\nenter 1st number:"
ask2: .asciiz "\nenter 2nd number:"
ask3: .asciiz "\nenter 3rd number:"
res: .asciiz "\nresult: "





MIPS assembly - it takes 3 user inputted vales (integers) and adds up the two biggest ones... fun.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0


<< yeah, assembly language blows >>



assembly can be great, but... what you posted isn't assembly language...

[edit]correction, it could be a form of assembly language, but most likely w/ some higher level constructs used by your assembler, so it can still technically be assembly I suppose[/edit]
 

hominid skull

Senior member
Nov 13, 1999
971
0
0
Try this for size Soyobomb..

// tree2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

void main(void)
{

int i;
int width;
int blanks;
int ast;

ast = 1;
width = 0;

cout << "Enter the width of the base of the tree and press enter.\n";
cin >> width;

blanks = (width - 1) / 2;


while (ast != width)
{
for (i=0; i<blanks; i++)
{
cout<<" ";
}
for (i=0; i<ast; i++)
{
cout<<"*";
}
for (i=0; i<blanks; i++)
{
cout<<" ";
}
cout<<"\n";
blanks = blanks - 1;
ast = ast + 2;
}
}


works a treat!

good luck
 

hominid skull

Senior member
Nov 13, 1999
971
0
0
This is my revised version as the first had a bug that excluded the last line of asterisks, it also contains error checking of the input width.


#include "stdafx.h"
#include <iostream>

using namespace std;

void main(void)
{

int i;
int width;
int blanks;
int ast;

ast = 1;
width = 0;

cout << "Enter the width of the base of the tree and press enter.\n";
cin >> width;

blanks = (width - 1) / 2;


if (width <=79 && width % 2 == 1 && width >0)
{
while (ast <= width)
{
for (i=0; i<blanks; i++)
{
cout<<" ";
}
for (i=0; i<ast; i++)
{
cout<<"*";
}
for (i=0; i<blanks; i++)
{
cout<<" ";
}
cout<<"\n";
blanks = blanks - 1;
ast = ast + 2;
}
}
else
{
if (width >79)
{
cout<<"the number was greater than 79\n";
}
if (width % 2 ==0)
{
cout<<"the number was even\n";
}
if (width < 0)
{
cout<<"the number was lest than 0\n";
}
}
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |