0

Ok so i have trouble with looping with while and for statments
heres the questions: 1. Write a program that gets a positive integer from the user and prints all the numbers from one to that integer. If the inputted number is zero or negative, then the program will print an error and quit.

I dont have much, but this is all i have:

#include <iostream>

using namespace std;

int main()
{
 int num=1, i;

 cout<<"please enter a number"<<endl;
 cin>>i>>

 while num<i;

 cout<< num+1<<;




return 0;

}

Can someone please to me what to add and the correct format

Edited by Narue: added code tags

6
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by cherrymae.calma
0

Wow,

while (true) {
cin >> number;
if (num > 0) break;
else {cout << "Incorrect";}
}
for (int i = 0; i < number; i++) {
cout << i << "\n";

I wrote it in about 30 seconds...

0

This took a bit more than 30 secs but it isn't full of errors and uses whitespace so you can actually see what is going on.

#include <iostream>

using namespace std;

int main()
{
	int num;

	cout << "please enter a number" << endl;
	cin >> num;
	if( num > 0 )
	{
		for( int i = 0; i < i; i++ )
		{
			cout << i << endl;
		}
	}
	else
	{
		cout << "number is less than or equal to 0" << endl;
	}

	return 0;
}
0

I don't see the point in him posting that at all then. He is just showing someone, that doesn't know what they are doing, code that doesn't work.

@Adam Ma
I would highly recommend you go over this tutoral. I learned C++ from here and I use the reference section all the time.

0

And to add to the above, here's a tut specified on loops.

You also need to pay more attention to your code-syntax. If you miss just one semicolon, your program won't compile. C++ is very unforgiven :)

I wrote it in about 30 seconds...

That's nice. :icon_wink:

Edited by Nick Evan: n/a

-1

in using while looping statement your need the following the initialization,logical condition,incrementation or decrementation.

0
#include <iostream>
 
using namespace std;
 
int main()
{
	int num;
 
	cout << "please enter a number" << endl;
	cin >> num;
	if( num > 0 )
	{
		for(num=num;num>0;num--)
		{
			cout <<num<<" "<<"positive"<<endl;
		}
	}
	else
	{
		cout<<num<<" "<<"negative"<<endl;
	//	cout << "number is greater  than or equal to 0" << endl;
	}
 
	return 0;
}

just try run this program... and gathered information,

Edited by Nick Evan: Added CODE tags

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.