This is what I need

1) I also need to use a for loop to prompt user to input two intergers: firstNumber and secondNumber (firstNumber must be less then secondNumber)
2) Outputs all odd numbers between firstNumber and secondNumber
3) output the sum of all even numbers between firstNumber and secondNumber
4) output the sum of the square of the off numbers between firstNumber and secondNumber (take each one, and then add them together)

This is what I have

``````int  firstNumber, secondNumber, i;

cin>>firstNumber;
cout<<endl;

cout<<"please enter a number greater than the first number. ";
cin>>secondNumber;
cout<<endl;

for (i=firstNumber; i=secondNumber)
{

}
``````

Edited by happygeek: fixed formatting

5
Contributors
13
Replies
14
Views
10 Years
Discussion Span
Last Post by Rajith Cherian

your loop should look like this:

``````// loop while i is between first and second input
for (i=firstNumber; i <= secondNumber; i++)
{
// now using an if statement check if the number is odd or even
}``````

You should have a look at the modulus % operator to find out if a number is odd or even.

The code could look like

``````int nFirstNumber = 0;
int nSecondNumber = 0;

vector<int> vOddNumber;
int nEvenSum = 0;
int nSumOddSquare = 0;

cout<<"Enter the first Number: ";
cin>>nFirstNumber;
cout<<"Enter the second Number greater than the first number:";
cin>>nSecondNumber;

while ( nSecondNumber <= nFirstNumber )
{
cout<<"Enter the second Number greater than the first number:";
cin>>nSecondNumber;
}

for ( int i = nFirstNumber ; i <= nSecondNumber ; i ++ )
{
if (  ( i%2 ) == 0 )
{
nEvenSum += i; //Sum of even Numbers
}
else
{
vOddNumber.push_back(i);//Storing all Odd Numbers
nSumOddSquare  += ( i*i ); // Sum of square of odd numbers
}
}``````

I was not tought how to use vector, or vOddNumber.push_back and can't use it

The code could look like

``````if (  ( i%2 ) == 0 )
{
nEvenSum += i; //Sum of even Numbers
}
else
{
vOddNumber.push_back(i);//Storing all Odd Numbers
nSumOddSquare  += ( i*i ); // Sum of square of odd numbers
}``````

incase you didnt read the post by niek_e, he mentioned that % operator needs to be used. The idea was to give a direction to the OP, so that he can then find things on his own. No use giving the entire solution on a platter. No need for spoon feeding.

what does this mean
G:\c++\chapter5\timmy.cpp(603) : error C2228: left of '.push_back' must have class/struct/union type

what does this mean
G:\c++\chapter5\timmy.cpp(603) : error C2228: left of '.push_back' must have class/struct/union type

Pretend you didn't see post #3

what was wrong with post 3

It confused you...

Change this line: `vOddNumber.push_back(i);//Storing all Odd Numbers` to : `cout << "Odd: " << i << "\n";` That will solve one of your problems. Now try to solve the other problems yourself. This shouldn't be to difficult with the code given.

``````error C2228: left of '.push_back' must have class/struct/union type
Error executing cl.exe.
``````

whats wrong

``````#include <string>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

int nFirstNumber = 0;
int nSecondNumber = 0;

int nOddNumber;
int nEvenSum = 0;
int nSumOddSquare = 0;

cout<<"Enter the first Number: ";
cin>>nFirstNumber;
cout<<"Enter the second Number greater than the first number:";
cin>>nSecondNumber;

while ( nSecondNumber <= nFirstNumber )
{
cout<<"Enter the second Number greater than the first number:";
cin>>nSecondNumber;
}

for ( int i = nFirstNumber ; i <= nSecondNumber ; i ++ )
{
if (  ( i%2 ) == 0 )
{
nEvenSum += i; //Sum of even Numbers
}
else
{
nOddNumber.push_back(i);//Storing all Odd Numbers
nSumOddSquare  += ( i*i ); // Sum of square of odd numbers
}
}

cout<<nOddNumber<<" "<<endl;
cout<<nEvenSum<<" "<<"is the sum of the evens."<<endl;
cout<<nSumOddSquare<<" ";

return 0;
}
``````

Edited by mike_2000_17: Fixed formatting

If you keep refusing to read my posts, I'll just stop helping you.

Read my previous post, try it, and come back.

``````#include <vector>