954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help on an assignment in C++ (beginner)

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.

theoryforlife
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 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++)
    {

    }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

theoryforlife
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 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
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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
}
ntrncx
Junior Poster
116 posts since Jan 2011
Reputation Points: 29
Solved Threads: 7
 

double posted by accident*

how i can delete posts?

ntrncx
Junior Poster
116 posts since Jan 2011
Reputation Points: 29
Solved Threads: 7
 

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

theoryforlife
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 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?

ben25x
Light Poster
41 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

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

theoryforlife
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: