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

for loop help please!!!

Okay here's what I'm trying to do use a for loop get 5 numbers from input ONE number at a time. Output the sum and average of the 5 numbers.

I am only allowed to use a maximum of 2 variables to solve this problem. Heres my code so far, please help me to find error? it compiles but doesnt compute

#include <iostream>
using namespace std;
int main()
{
int startNum;
int nextNum;

	cout<<"Please Input a number " <<endl;
	cin >> startNum;


for(startNum=0; startNum + nextNum<=5; nextNum++)
{
	cout<< startNum+nextNum+nextNum+nextNum+nextNum<< " is the sum of all numbers " <<endl;
	cout<<endl;
	cout<< startNum+nextNum+nextNum+nextNum+nextNum/5 << "is the avg of your numbers " <<endl;
}
	return 0;
}
cortez716
Light Poster
34 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Any help is appreciated!!

cortez716
Light Poster
34 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Shouldn't you be prompting, for the numbers, inside the for loop?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

I've edited code to say this, however it does not allow user to enter 4 more numbers after original input, what am i doing wrong

#include <iostream>
using namespace std;
int main()
{
int startNum;


	cout<<"Please Input a number " <<endl;
	cin >> startNum;

int nextNum;
nextNum=startNum;
for(startNum=0; startNum + nextNum<=5; nextNum++);
{
	cout<< startNum+nextNum+nextNum+nextNum+nextNum<< " is the sum of all numbers " <<endl;
	cout<<endl;
	cout<< (startNum+nextNum+nextNum+nextNum+nextNum)/5 << "is the avg of your numbers " <<endl;
}
	return 0;
}
cortez716
Light Poster
34 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
Shouldn't you be prompting, for the numbers, inside the for loop?

gerald thanks for your help so far, I've changed code to following but still wont allow user to input 4 more numbers what am i doign wrong?

#include <iostream>
using namespace std;
int main()
{
int startNum;


	cout<<"Please Input a number " <<endl;
	cin >> startNum;

int nextNum;
nextNum=startNum;
for(startNum=0; startNum + nextNum<=5; nextNum++);
{
	cout<< startNum+nextNum+nextNum+nextNum+nextNum<< " is the sum of all numbers " <<endl;
	cout<<endl;
	cout<< (startNum+nextNum+nextNum+nextNum+nextNum)/5 << "is the avg of your numbers " <<endl;
}
	return 0;
}
cortez716
Light Poster
34 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Maybe you should reread what Gerard said. He's onto something.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

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