Jump to content
  • Advertisement
Sign in to follow this  
duHOSSval

help with nested loops

This topic is 3958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help with using nested loops. How do I go about to print an inputed character the number of times a user inputs. For example, if the user inputs 5, it will print the character they choose 5 times, but also printing a row of 1, then 2, then 3, then 4, then 5, and then back down. I would appreciate any help i could get with this.

Share this post


Link to post
Share on other sites
Advertisement
May I assume C++?


#include <iostream>
#include <string>

int main()
{
char c = '*';
int n = 5;

for (int i = 1; i <= n; ++i)
std::cout << std::string(i, c) << std::endl;
for (int i = n-1; i >= 1; --i)
std::cout << std::string(i, c) << std::endl;
}

Share this post


Link to post
Share on other sites
Sounds like homework. We cannot do homework for you, but if you are having difficulties with your own code we might be able to help you.

Share this post


Link to post
Share on other sites
string stringInput;
char charInput;
double peakInput;
int count = 0;
int count1;


Console.WriteLine("Enter a character to be used: ");
stringInput = Console.ReadLine();
charInput = Convert.ToChar(stringInput);

Console.Clear();

Console.WriteLine("Enter the size of the peak: ");
stringInput = Console.ReadLine();
peakInput = Convert.ToDouble(stringInput);

Console.Clear();





for (count = 0; count < peakInput; ++count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput + "\n");
}
{
Console.Write(charInput);
}
}




Console.Read();


}
}
}





I'm having trouble getting i to make the rows of the characters. And no it isn't homework, im trying to refresh my skills.

Share this post


Link to post
Share on other sites
well im not really into c# so ill use c++(but its really almost the same,just follow the logic)


char c ='*';
int n=5;
for(int i=0;i<=n;i++){
for(int j=0;j<i;j++){
cout << c;
}
cout<<endl;
}
for(int i=n-1;i>0;i--){
for(int j=0;j<i;j++){
cout << c;
}
cout<<endl;
}



if my translation is right it this should be almost correct...

for (count = 0; count <= peakInput; ++count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput);
}
Console.Write("\n");
}
for (count = peakInput-1; count > 0; --count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput);
}
Console.Write("\n");
}


Share this post


Link to post
Share on other sites
well im not really into c# so ill use c++(but its really almost the same,just follow the logic)


char c ='*';
int n=5;
for(int i=0;i<=n;i++){
for(int j=0;j<i;j++){
cout << c;
}
cout<<endl;
}
for(int i=n-1;i>0;i--){
for(int j=0;j<i;j++){
cout << c;
}
cout<<endl;
}




if my translation is right it this should be almost correct...

for (count = 0; count <= peakInput; ++count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput);
}
Console.Write("\n");
}
for (count = peakInput-1; count > 0; --count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput);
}
Console.Write("\n");
}





hope that helps

EDIT: sorry for the double post..

Share this post


Link to post
Share on other sites
Original post by duHOSSval

for (count = 0; count < peakInput; ++count)
{
for (count1 = 0; count1 < count; count1++)
{
Console.Write(charInput + "\n");
}
{
Console.Write(charInput);
}
}


The entire inner loop - i.e. a single iteration of the outer loop - is supposed to produce one line of the output. Therefore, don't write a return character inside the inner loop, because that will put each letter on its own line, when you want them together on one line.

Then, after you've written all the characters for one line, you want to put a return, not the character again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!