Hello, I'm taking a 100-class (beginner) in C++
Currently I'm stuck in one of the assignments due where it asks for the program to have the user return to the beginning if there's an error, as well as create an out put of:
+
++
+++
(...)

The current code I have right now is:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main (int argc, char *argv[])
{

	int num;

	cout << "Enter a positive integer between 0 and 1000: \n";
	cin >> num;

	if ((num < 0) || (num > 1000))
	{
		cout << "Error. Please use a positive number between 0 and 1000\n";
	}
	
	
	else
	{
		for (int i = 0; i <= num; i++)
		{
			cout << "+";
		}
		return EXIT_SUCCESS;
	}

}

The output is a straight line, but I know the reason to that.

Recommended Answers

All 8 Replies

You need two nested loops, not just one. The first loop counts from 0 to num then the inner loop counts from 0 to the value of the outer loop, something like this

for(int i = 0; i < num; i++)
{
    for( int j = 0; j <= i; j++)
    {

    }
}
commented: Great for explaining why to include the function +0

You need two nested loops, not just one. The first loop counts from 0 to num then the inner loop counts from 0 to the value of the outer loop, something like this

for(int i = 0; i < num; i++)
{
    for( int j = 0; j <= i; j++)
    {

    }
}

Okay, I understand your explanation, can you tell me how I would loop the error back to the beginning for a new input?

You could just use a simple while loop.

while(true)
{
   cout << "Enter a positive integer between 0 and 1000: \n";
   // rest of code goes here
}

you can use another loop like while covering all the program and checks "num" so when check if or else will loop to the start until the sentinel fails. example

while (num!=-1)
{
program
}

double posted by accident*

how i can delete posts?

The assignment I'm doing has two parts to it, the first part requires the program to use "for" loops and the second part would to create the same program using "while", right now I'm trying to get the "for" part done

i'd say just use logic and what you know about c++ right now. part of the daniweb thing is we cant help you on homework assignments. but, so, you cant use ANY for() loops in the while() one, & vice versa?

I just managed to get it to work, thanks everyone for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.