14 Replies - 496 Views - Last Post: 30 March 2012 - 06:29 AM Rate Topic: -----

#1 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

"cout" asterisk not showing properly[QUESTION]

Posted 29 March 2012 - 10:05 PM

Hi, This is my codes:
#include <iostream>
using namespace std;

int main()
{
	int i, number;

	for(i=1; i<=5; i++)
	{
		cout<<"Enter a number: "<<endl;
		cin>>number;
		{
		if(number==1 || number<=30)
			cout<<'*'<<endl;
			number++;
		}
	}

	return 0;
}


The problem here is that the asterisk is displaying only 1 when it should be displaying according to the numbers input and according to the statement. PLEASE HELP!

Is This A Good Question/Topic? 0
  • +

Replies To: "cout" asterisk not showing properly[QUESTION]

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 4,552
  • Joined: 08-June 10

Re: "cout" asterisk not showing properly[QUESTION]

Posted 29 March 2012 - 10:49 PM

Hey.

Try walking through the program. This is essentially what you code does, in human-readable terms:
Loop five times {
	Read in a number
	If the number is 30 or less {
		print an asterisk
		increment the number
	}
}


Nowhere in there is anything that would make it print multiple asterisks, based on the number given. If you want to do that, you'll have to add a loop to repeatedly print an asterisk. A for loop would probably be most appropriate in this situation.

By the way:
if(number==1 || number<=30)

The number==1 part of this is redundant. 1 is less than 30, so the number<=30 part covers that condition as well.
Was This Post Helpful? 0
  • +
  • -

#3 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 29 March 2012 - 11:06 PM

Thanks, but how can i add another loop to say make it print the asterisk based on he number and statement?
Was This Post Helpful? 0
  • +
  • -

#4 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 02:11 AM

Help please I've tried but I don't know how :/
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 02:26 AM

You don't have to, if you don't want to run all this 5 times then just take your user input related statements outside of your for loop and let the for loop run upto "number" you took, instead of 5.
Was This Post Helpful? 0
  • +
  • -

#6 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 02:29 AM

No but I want to run 5 times and only the numbers<30 will have asterisk. But I don't know how? Can you teach me?

This post has been edited by magicm00n: 30 March 2012 - 02:30 AM

Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 02:56 AM

in that case you need a for loop immediately after you accept the number (for the braces you have without a statement). run that for loop up to "number" and it will do the job with least possible changes.
Was This Post Helpful? 0
  • +
  • -

#8 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 03:10 AM

I added the for loop after the cin but it still not working. Can you look at my codes above and correct it for me?
Was This Post Helpful? 0
  • +
  • -

#9 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 03:16 AM

show your modified code here. it would be just 2 lines to modify I guess.
Was This Post Helpful? 0
  • +
  • -

#10 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 03:48 AM

Can you tell me the 2 lines to modify? Because I dont know what statement to add for the extra loop.
Was This Post Helpful? -1
  • +
  • -

#11 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 04:07 AM

:)
Never ask for anything until you show your efforts first.

Anyways...

It's adding for(int j=0; i<number; j++) and removing number increment.

I would also suggest to check if number is less than 1 instead of checking if it is equal to one in if statement.

There are other things to optimize as well, but let it be. Get this one running first.

This post has been edited by AmitTheInfinity: 30 March 2012 - 04:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 04:59 AM

The code
for(int j=0; i<number; j++)
you gave me was not right but thank you for giving me the wrong code so I could learn by cracking my brain :D

Its solved now thanks :))
Was This Post Helpful? 0
  • +
  • -

#13 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 05:39 AM

View Postmagicm00n, on 30 March 2012 - 05:29 PM, said:

The code
for(int j=0; i<number; j++)
you gave me was not right but thank you for giving me the wrong code so I could learn by cracking my brain :D

Its solved now thanks :))



yes. Glad you could find it. :)

btw, If you have the solution then please post your final working code here so that when someone else searches for solution to similar problem; they will get it easily.
Was This Post Helpful? 0
  • +
  • -

#14 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 01-March 12

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 06:00 AM

Here:

#include <iostream>
using namespace std;

int main()
{
	int i, j, number;

	for(i=1; i<=5; i++)
	{
		cout<<"Enter a number: "<<endl;
		cin>>number;

		for(j=1; j<=number; j++)
		
		if(number<=30)
			cout<<'*';
			cout<<endl;
		
	}

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: "cout" asterisk not showing properly[QUESTION]

Posted 30 March 2012 - 06:29 AM

You should develop the habit of beginning your counting at 0, and setting the limit at "< max_number". For example, a for loop that iterates 5 times should be
for(i = 0; i < 5; i++)


Why? Because for one thing, the elements of an array in C/C++ are numbered from 0 to "size-1". In other words, the elements of an array of 4 ints (let's name it "my_arr") are:
my_arr[0]
my_arr[1]
my_arr[2]
my_arr[3]
You don't have any choice in this; that's just the way it is. And when you start working with arrays (which will be soon) you will almost always be working in loops so it's convenient to do the loop counting the same way.

Another reason to write your loops this way is that this is the way it is "always done" by C/C++ programmmers, and if you don't comply your work will be immediately considered "novice".

It might seem a little awkward at first, but you will get accustomed to it quickly and it will soon feel completely natural.

This post has been edited by r.stiltskin: 30 March 2012 - 06:30 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1