Computing.Net > Forums > Programming > Help with debugging C++ code

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with debugging C++ code

Reply to Message Icon

Name: SheGeek
Date: July 24, 2005 at 18:40:22 Pacific
OS: WindowsXP
CPU/Ram: 000
Comment:

My program will not output the second part : where it has to add the even numbers and show the sum. I did not code the third part with letters yet...
Please help with this...
//*************************************************************
// 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;

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

//cout << "Following is the sum of all even no.s between the numbers you entered...";
//cout << endl << endl << endl;

if (( firstNum % 2) != 0 ) firstNum ++;
while (firstNum < secondNum)
{
cout << sum;
sum = sum + firstNum;
}
// << endl << endl << endl;

system ("pause");
return 0;
}



Sponsored Link
Ads by Google

Response Number 1
Name: Fozzie
Date: July 24, 2005 at 19:38:19 Pacific
Reply:

In this section of code...

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

}
cout << endl << endl << endl;

...you are incrementing firstNum until it becomes greater than or equal to secondNum.

Remember, firstNum++ and firstNum+=2 both CHANGE the value of firstNum.

Therefore the condition being evaluated in your next while loop...

if (( firstNum % 2) != 0 ) firstNum ++;
while (firstNum < secondNum)//False! No loop!
{
cout << sum;
sum = sum + firstNum;
}
// << endl << endl << endl;

...will always be false.

A simple fix would be to create a variable and assign the value of firstNum to it before the first loop. Then use the new variable in your loop in place of firstNum. When the second loop comes around you can reassign the value of firstNum to your loop variable and use it again in the second loop.


0

Response Number 2
Name: SheGeek
Date: July 24, 2005 at 19:58:27 Pacific
Reply:

Thank you, I will try this!


0

Response Number 3
Name: SheGeek
Date: July 24, 2005 at 20:21:11 Pacific
Reply:

IT doesn't work :( . I am still doing something wrong. Please advise :

I THINK I AM SEEING THAT 2ND LOOP IS NOT LOOPING FOR SOME REASON !!!!

#include "PROG05.h"
using namespace std;
int main()
{
// initialize variables
int firstNum, secondNum;
int sum;
int number;
int value;
//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;
number = firstNum;
if((number % 2) == 0) number++;
while(number < secondNum)
{
cout << number << " ";
number += 2;

}
cout << endl << endl << endl;

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

//sum = 0;
// while((firstNum % 2 )!=0) firstNum++;
{

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

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

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


0

Response Number 4
Name: Fozzie
Date: July 25, 2005 at 20:25:22 Pacific
Reply:

I stripped out your commented statements to see the code better. This is what I got:

// find the sum of all even numbers
value = firstNum;
sum = 0;
if (( value % 2) != 0 )value++;

while (value < secondNum)
{
cout << sum << endl;
sum = sum + value;
}
cout << endl << endl << endl;

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

It looks to me like the program should go into an infinite loop because you never increment value in your loop (except sometimes in your 'if' statement.)

I know it's painful to delete code you have written (I do the commenting thing too) but I'd recommend deleting the commented statements and having another look at what's left. I'll bet you can get it. You made the first loop work right? :)

If not, tell me what the ouput is and I'll take another stab at it.

p.s. It's considered amateurish to do spot "reality checks" when troubleshooting code, but I don't let that stop me from inserting a statement to cout the value of a variable with a message similar to "about to loop: value = " when my code does not perform as expected. When the problem is fixed, I delete the statement and recompile. Later on as you progress you will learn more elegant ways of debugging. Maybe someday I will too!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with debugging C++ code

!!! help to Debug C code programmin www.computing.net/answers/programming/-help-to-debug-c-code-programmin/7888.html

Help with my C++ program www.computing.net/answers/programming/help-with-my-c-program/1813.html

Need Help With C++ cin command www.computing.net/answers/programming/need-help-with-c-cin-command/12174.html