C++ programming help

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
How would I go about doing this. Could somebody kind of get me started. I already know like how to prompt for the value and stuff. I basically just need help with the second bullet. But how do I go about having it print the right number of dollar signs in the right row?
Thanks for any help.

Write a program that prints a user-specified number of dollar signs ($) arranged in a triangle.

- Prompt the user to enter the total number of rows to print.

- The triangle array should be printed out with the first row with a single $, the second row (when the number of rows is 2 or bigger) with $$, etc.

-Row requests with zero or a negative number of rows should be rejected with a firm, but polite error message.


This is what I have so far:

#include <stdio.h>

/* function main begins program*/
int main()

{
int x;
int N;


printf("Please enter the number of rows you would like:"); /*prompt for number of rows*/
scanf("%d",&N); /*read number of rows*/

if(N<0) {printf("The number you entered is invalid, please enter a number greater than zero.\n");

return 0;
}


for(x=0; x<=N; x++) {printf("$");
}

for(N=0; x=N; N++) {printf("\n");
}




return 0;
}

 

itachi

Senior member
Aug 17, 2004
390
0
0
push N $'s on a stack..
while the stack isn't empty
print the contents -- (N - size) leading spaces and a space between each $.
pop a value from the stack
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: itachi
push N $'s on a stack..
while the stack isn't empty
print the contents -- (N - size) leading spaces and a space between each $.
pop a value from the stack


Sorry I am confused. How would I do that. I am just a beginner so I am not familiar with too much.

Thanks for helping.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Here's your ultra-high-tech algorithm:

for(i from 0 to N){
for(0 to i){
print "$";
}
}

You can turn it into proper C.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Still trying to figure that program out. Have one more question.
EDIT:Nevermind figured it out.
Still need help with this program.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: Spydermag68
This is so simple...It takes two loops...I let you figure the rest out.


Yes, I realize it is simple. But I am a noob.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Still need help with this, please help. This is what I have so far. I just cant figure out what to put in, tried a few things but nothig is working.

#include <stdio.h>

/* function main begins program*/
int main()

{
int counter;

int x;
int N;

x=0;

printf("Please enter the number of rows you would like:"); /*prompt for number of rows*/
scanf("%d",&N); /*read number of rows*/

if(x<=N) {printf("$");
}

while(x!=N) {
x++;

printf("$");
}



return 0;
}
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
I seriously need help. I cant figure this out, I have tried so many different things, I can get the right number of dollar signs, but I cant get it to go to the next line once there are enough $'s in the row.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
CR+LF carriage return + line feed

in C/C++ this is usally the character '\n' or pair '\r\n' or some silly C++ version of it ("endline"?)
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: DaveSimmons
CR+LF carriage return + line feed

in C/C++ this is usally the character '\n' or pair '\r\n' or some silly C++ version of it ("endline"?)


?
This is supposed to be really simple. This is like a intro C++ class, but for some reason the prof thinks we all already know the basics. All I know is that is contains two loops.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: saahmed
I seriously need help. I cant figure this out, I have tried so many different things, I can get the right number of dollar signs, but I cant get it to go to the next line once there are enough $'s in the row.
\r\n or \n, or endl for cout, are the characters you write out to move to the next line.

Try writing out this: "line 1\nline 2\nLine 3\n"

 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: DaveSimmons
Originally posted by: saahmed
I seriously need help. I cant figure this out, I have tried so many different things, I can get the right number of dollar signs, but I cant get it to go to the next line once there are enough $'s in the row.
\r\n or \n, or endl for cout, are the characters you write out to move to the next line.

Try writing out this: "line 1\nline 2\nLine 3\n"


Yeah, I know how to go to the next line, but how do I it to comply with the program requirements. It has to look like this:

$
$$
$$$
$$$$
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I don't understand.

cout "here is line 1" << endl << "here is line 2";

That will do it, right?
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: xtknight
I don't understand.

cout "here is line 1" << endl << "here is line 2";

That will do it, right?


is that just to write out:

here is line 1
here is line 2

because I know how to do that, I dont know how to get the above program working (in the original post).
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: itachi


Looks like it would work. I am getting a lot of errors, did you get it to work? Probably because I just pasted your stuff into mine to keep the other fluff like the prompt and stuff. But I will work at it. Thanks a lot.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: itachi


OK I got it to work. Thanks so much. Give me your paypal address if you want the $3 I promised. Not much, but I needed to provide an incentive to get somebody to help me out.