0

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.

4
Contributors
8
Replies
10
Views
6 Years
Discussion Span
Last Post by theoryforlife
Featured 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 [code] for(int i = 0; i < num; i++) { for( int j = 0; j <= … Read More

  • 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 [CODE]while (num!=-1) { program }[/CODE] Read More

1

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++)
    {

    }
}
Comments
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?

0

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
}
1

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
}
0

double posted by accident*

how i can delete posts?

Edited by ntrncx: n/a

0

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

0

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?

Edited by ben25x: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.