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;


cout<<"please enter a number. ";
cin>>firstNumber;
cout<<endl;


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


for (i=firstNumber; i=secondNumber)
{


}

Recommended Answers

All 13 Replies

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
       }
}

Hope this is your requirement

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
       }

Hope this is your requirement

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

Member Avatar for iamthwee

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

Member Avatar for iamthwee

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.

Please help, I have this error with this program

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;
}

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

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

Also please: use code-tags. It will make your code a lot easier to read.

sorry, I went back and am looking over your posts again

Hey timdog345
include the following headers

#include <vector>
using namespace std;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.