Help with while loop

Please help. My while loop is the problem. I want to use a non-recursive function to make the triangle number the sum of all whole numbers from 1 to N.


#include "stdafx.h"
#include <iostream>
using namespace std;

int triangle(int t);

int main()
{

int i;
cout << "Enter a number please.";
cin >> i;
cout << endl;
cout << "triangle(" << i << "); " << triangle(i) << "\n\n";

system("PAUSE");
return 0;
}
int triangle(int t);
int i;

{
while (t >= 1)
{
cout << i << end;
i = i - 1;
}
}
Line 19: Remove the ;

Line 21: The { needs to be before int i

Line 24-25: i is uninitialized. You're going to print and decrement garbage.

Line 24: This should be endl

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
using namespace std;

int triangle(int t);

int main()
{

int i;
cout << "Enter a number please.";
cin >> i;
cout << endl;
cout << "triangle(" << i << "); " << triangle(i) << "\n\n";

system("PAUSE");
return 0;
}
int triangle(int t)
{
int i;

while (t >= 1)
cout<< end;
}
}


Thanks AbstractionAnon! Now I feel like you guys. I was wondering how you put it in <> and make it c++ codes .
Last edited on
Topic archived. No new replies allowed.