• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Program Debugging Thread

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Rabid Snail

Member
Joined
Sep 20, 2002
Location
New York
In an attempt to:
a: get my program to work
b: get one of my threads stickied

I have decided that this forum needs a thread where all the expert programmers here, can help debug setions of code and stuff.

To start this (amazing :)) thread off, I'll post my program I need help fixing. (if this thread is dumb, and not needed, can some one at least help me debug this program?)

#include <iostream.h>


int add(int x, int y);
int mult(int x, int y);




int main()

{

int x, y;
int a;
int m;

cout<<"Press one(1) to multiply, and two(2) to add." cin<<m;

if(m=1)
{
cout<<"Please input two numbers to be multiplied: ";

cin>>x>>y;

cout<<"The product of your two numbers is "<<mult(x, y);

cin>>a;

return 0;
}

else
{
cout<<"Please input two numbers to be added: ";

cin>>x>>y;

cout<<"The sum of the two numbers is "<<add(x,y);

cin>>a;

return 0;
}

int mult(int x, int y)

{
return x*y;

}

int add(int x, int y)

{

return x+y;

}
 

naif

Member
Joined
Feb 17, 2002
Firstly, If you were getting a compile problem, it was on this line

cout<<"Press one(1) to multiply, and two(2) to add." cin<<m;

You didnt not terminate the first statement with a semicolon.

You could had used this approach

And yeah, to compare something, you would use == not =. = is used for assignment, where as == is used for comparing

int main()

{

int x, y, a;
int choice

cout<<"Press one(1) to multiply, and two(2) to add.";
cin<<choice;

if(choice == 1)
{
cout<<"Please input two numbers to be multiplied: ";

cin>>x>>y;

cout<<"The product of your two numbers is "<<mult(x, y);

cin>>a; // Are you using this statement as a sort of a pause
return 0;
}

else

{
cout<<"Please input two numbers to be added: ";

cin>>x>>y;

cout<<"The sum of the two numbers is "<<add(x,y);

cin>>a;

return 0;
}

}
 

Thelemac

Administratively Deficient
Joined
Mar 15, 2001
Yeah, as a point of style if you're using that statement for a pause (which is the only thing I can think of for it) I would put a prompt, such as "Press any key to continue/end this program."

:)
 
OP
Rabid Snail

Rabid Snail

Member
Joined
Sep 20, 2002
Location
New York
Well, this program is just to test out the if statments and that stuff, and isnt really any use, since I already have a better calculator on my comp. There is no point in saying sanything, since I know waht it means and all. BTW, the program works. I forgot to add a } somewhere, and end the line, like you said.
Thanks
 

Thelemac

Administratively Deficient
Joined
Mar 15, 2001
It's better to always follow good programming practices or you'll form bad habits. :)
 

Krieger

Member
Joined
Jan 31, 2002
Location
North of Boston, MA
Thelemac is right, the better the practices you follow in general your debuggin times will go way down. simple problems take me 5 seconds to find. more complex ones... well that's why there are debuggers... Oh and if you can find one there are books on programming practices. I got one a few years ago that I finially got around to reading when I left my last job. IT was from the microsoft press. It was a really good place to pick up some great ideas on how to program effecently. Alot of programming is habit, learn the right ones first. it takes forever to retrain yourself after a bad one.