PDA

View Full Version : Please help debug this C++ program


amazingme001
07-25-2005, 04:59 AM
I have been trying to figure it out for the past 3 hours, but it just seems not to work. It prints all the odd numbers as it is asked in the first part. Then it can not add all the even numbers and display the sum. I know something is wrong with my 2nd while loop, but can'f figure it out!!!

Please help!! :confused:
//*************************************************************
// The following program uses while loops to perform
// several steps. It prompts user to enter two numbers,
//it ouputs the odd numbers between those numbers, outputs
// the sum of all even numbers found between the two numbers
// and finally it outputs all uppercase letters.
//*************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>

#include "PROG05.h"
using namespace std;
int main()
{
// initialize variables
int firstNum, secondNum;
int sum;
//sum = 0;
// get input from the user
cout << "Please enter two numbers, first no. must be smaller than the second: ";
// read (input)values into variables
cin >> firstNum >> secondNum;
// show what the input is
cout << "You have entered " << firstNum << " and " << secondNum << ".";
cout << endl << endl << endl;

if((firstNum % 2) == 0) firstNum++;
while(firstNum < secondNum)
{
cout << firstNum << " ";
firstNum += 2;

}
cout << endl << endl << endl;
// find the sum of all even numbers
//if (( firstNum % 2) != 0 ); //firstNum++;
//while (firstNum < secondNum)
sum = 0;
while((firstNum % 2 )!=0) firstNum++;
{

sum = sum + firstNum;
cout << sum << endl;
}
//sum = sum + firstNum;
cout << endl << endl << endl;

// << endl << endl << endl;


//system ("pause");
return 0;
}

i have commented out some parts that I was trying to figure out, that's why there are few things repeated

Jason
07-25-2005, 05:49 AM
your probelm is that you are changing the "firstnum" variable in your first loop so by the time you need to figure out the sum of all the even numbers you have already made the "firstNum" either equal or 1 number off of "secondNum" so the answer would be 0. Also there is no printing of that second sums numbers.... does that help? you might try making new variables for the seperate sums by making copies of firstNum and secondNum..


Jason

amazingme001
07-25-2005, 06:15 AM
Still no luck.
On the second loop even when i get all the even numbers, I fail when i want to save those values "somewhere" so I can use them to add them together later. I just don't know how!! I tried everything!!

Aradon
07-25-2005, 04:30 PM
A couple things.

1) you can use more then the variables you've created. Maybe have a starting number and an ending number that doesn't change. That way you can create a temporary variable in order to count, get the even numbers and the sum?

So have like an int begin, end, sum, temp;

Use the temp varible to go inbetween the begin and end variable.

Or...

2) My favorite idea, do it all at once! Why not increment through the number one at a time, if the number is odd, print, if it's even then add it and I'm not sure about the capital letters thing..

But #2 seems eaiser.. Just my opinion.

amazingme001
07-25-2005, 07:04 PM
Thanx everyone for your help. I will work on the problem! :)