Jump to content

c++ question

Go to solution Solved by madknight3,

 

thanks for the help :)

 

 

I did find a way to fix it ... here how I did it

 

But that doesn't print it reversed like you wanted. You want to do this to print it reversed

string(num - newa, '*');

Alternatively you can reverse the loop

for(newa = num; newa >= 0; newa--) // goes from num to zero instead of zero to num{    cout<<string(newa,'*'); // isn't changed because we reverse the loop    cout<<endl;}
#include <iostream>#include<math.h>using namespace std;int main(){int num,i,z=0,newa;cin>>num;newa=num;while(z<num){    for(i=0;i<newa;i++)    {        cout<<'*';    }    cout<<endl;    newa=newa-1;    z++;}}

Input ==>6

 

output ==> 

*

**

***

****

*****

******

 

I need it to be reversed Like this 

 

******

*****

****

***

**

*

 

any help :)

Link to comment
https://linustechtips.com/topic/270705-c-question/
Share on other sites

Link to post
Share on other sites

try this 

newa-=1 instead of newa=newa-1

i cant see any other issues in your code, i will try it out later today when I have a PC with c++

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673272
Share on other sites

Link to post
Share on other sites

try this 

newa-=1 instead of newa=newa-1

i cant see any other issues in your code, i will try it out later today when I have a PC with c++

 

Sorry the output is 

 

******

*****

****

***

**

*

 

need it to be 

 

*

**

***

****

*****

******

 

I asked the question wrongly 

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673328
Share on other sites

Link to post
Share on other sites

I worked perfectly now But why "newa-=1 instead of newa=newa-1" Fixed it !? 

 

idk why it worked and = - didnt...

I think its because -= does the - operation on a temporary value of newa, then sets the newa to that temp value, sort of like

newa=tempnewa=(temp-1)

but from my experience newa=newa-1 should have worked too...

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673330
Share on other sites

Link to post
Share on other sites

Sorry the output is 

 

******

*****

****

***

**

*

 

need it to be 

 

*

**

***

****

*****

******

 

I asked the question wrongly 

oooh

well then let me work on a solution for that :P one sec

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673350
Share on other sites

Link to post
Share on other sites

#include <iostream>#include<math.h>using namespace std;int main(){int num,i,z=0;cin>>num;while(z<=num){    for(i=0;i<=z;i++)    {        cout<<'*';    }    cout<<endl;    z++;}}

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673367
Share on other sites

Link to post
Share on other sites

Thats a pretty weird way to do that task, it would be easier to convert to your desired output if both of them were for loops, like so:

#include <iostream>#include<math.h>using namespace std;int main(){int num;cin>>num;for(int i = 0; i <= num; i++){    for(int j = 0; j <= i; j++)    {        cout<<'*';    }    cout<<endl;}}

I

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673372
Share on other sites

Link to post
Share on other sites

Thats a pretty weird way to do that task, it would be easier to convert to your desired output if both of them were for loops, like so:

#include <iostream>#include<math.h>using namespace std;int main(){int num;cin>>num;for(int i = 0; i <= num; i++){    for(int j = 0; j <= i; j++)    {        cout<<'*';    }    cout<<endl;}}

I

 

thanks for the help :)

 

 

I did find a way to fix it ... here how I did it

#include <iostream>#include<math.h>using namespace std;int main(){int num,newa;cin>>num; for(newa=0;newa<=num;newa++)  {    cout<<string(newa,'*');    cout<<endl;  }}
Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673409
Share on other sites

Link to post
Share on other sites

I don't know the answer, but from my experience of posting coding problems on the LTT forums, you'd be far better off asking this on Stack Overflow

Many of the questions asked here wouldn't be accepted by the stack overflow community. Also, basic questions like this are pretty much always answered here.

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673437
Share on other sites

Link to post
Share on other sites

 

thanks for the help :)

 

 

I did find a way to fix it ... here how I did it

 

But that doesn't print it reversed like you wanted. You want to do this to print it reversed

string(num - newa, '*');

Alternatively you can reverse the loop

for(newa = num; newa >= 0; newa--) // goes from num to zero instead of zero to num{    cout<<string(newa,'*'); // isn't changed because we reverse the loop    cout<<endl;}
Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3673460
Share on other sites

Link to post
Share on other sites

Many of the questions asked here wouldn't be accepted by the stack overflow community. Also, basic questions like this are pretty much always answered here.

 

Oh fair enough. I only say it because I've asked a couple programming questions before and not got any answers at all

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3674432
Share on other sites

Link to post
Share on other sites

Oh fair enough. I only say it because I've asked a couple programming questions before and not got any answers at all

Yeah, it's unfortunate but it's going to happen. With a (much) smaller community comes more limited knowledge. Stack overflow is an amazing resource that I use all the time, it's just beginners don't tend to know how to ask proper questions that meet their criteria and the down votes roll in.

 

I don't know what all your questions were about but I noticed one of them was on vba/access. I expect that no one who viewed it here may have known anything about working with them as they aren't as common to learn these days. For example, I've worked in VB.NET and MSSQL but not in vba or access.

 

tldr: We try but only know so much :P

Link to comment
https://linustechtips.com/topic/270705-c-question/#findComment-3674691
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×