|
This is the first time I write a program after learning C++ for 1 week ago.
I use Dev-C++ and I want my program to do function such as:
Ask person to type two number : 1st number ; 2nd number.
such as: 2 ; 5;
and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14
but I try many times and it always gives me : 2 + 5 = 7
This is my code :
using namespace std;
#include <iostream>
long result(long d , long e)
{
if (d >= 1 , d < e)
{
return (result(d + 1,0));
}
else
return (0);
}
int main()
{
long a,b;
long c;
cout << "1st number : ";
cin >> a;
cout << "2nd number : ";
cin >> b;
c = a + result(a , b) + b;
cout << c;
return 0;
}
Can someone tell what 's wrong with my code, and give me a hint to do it.
Thankz.
|
|
|
|
|
simply write -
int main()
{
long a,b;
long c = 0;
cout << "1st number : ";
cin >> a;
cout << "2nd number : ";
cin >> b;
for (int i = a; i <= b; ++i)
c += i;
cout << c;
return 0;
}
your result() function is wrong which always returns zero.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
can you tell me why it is wrong and if i follow my way then what is the solution ?
By the way, thank you for your reply. This is my first time, seem that i have a lot of work to do.
|
|
|
|
|
long result(long d , long e)
{
if (d >= 1 , d < e)
{
return (result(d + 1,0));
}
else
return (0);
}
this function is wrong because
result(d + 1,0) = 0 bacause d+1 > 0
if you replace e for 0: result(d + 1,0)->result(d + 1,e), this function still wrong because
result(d,e)=result(d+1,e)=.....=result(e,e)=0
-----------------
conglt
|
|
|
|
|
if (d >= 1 , d < e) is legal syntax, but doesn't do what you want. Use && for "and" in boolean tests.
Look at your exit condition of result() - it only ever returns 0. Recursion is something you should really save for later, when you have more experience with the language.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
long result(long d , long e)
{
if (d >= 1 , d <= e)
{
return d + result(d+1,e);
}
else
return (0);
}
Instead of calling
c = a + result(a , b) + b;
only call
c = result(a,b);
Evaluation of recursive call is -
return 2 + result(2+1,5);
return 2 + 3 + result(3+1,5);
return 2 + 3 + 4 + result(4+1,5);
return 2 + 3 + 4 + 5 + result(6,5);
After if condition became false evaluation is
return 2 + 3 + 4 + 5 + 0;
return 2 + 3 + 4 + 5 ;
return 2 + 3 + 9;
return 2 + 12 ;
return 14 ;
In your code
long result(long d , long e)
{
if (d >= 1 , d < e)// After first iteration e became 0 and condition false and return 0;
{
return (result(d + 1,0));
}
else
return (0);
}
c = a+ result(a,b) + b = 2 + 0 + 5
Write recursive function carefully.
Regards
Amar
|
|
|
|
|
are you student from Viet Nam ???
In your program :
you see again if statement:
if (d >= 1 , d < e)//or (d>=1 && d
|
|
|
|
|
Thankz for all your reply.
I know my problem now.
|
|
|
|
|
I am using the BackColor function, and all the documentation I have seen indicates that you have to let the user choose the color. I am wondering if there is a way to specify which color is used without the user choosing a color?
Thanks
|
|
|
|
|
I'm not sure what "BackColor" function you're referring to or what parameter type it expects, but if it takes a COLORREF, you can make any color with the RGB macro or use any system colors with the GetSysColor() function.
COLORREF clr = RGB(255, 0, 0); // bright red
COLORREF clrWindow = ::GetSysColor(COLOR_WINDOW);
You might also need to use OleTranslateColor, if you're doing COM/OLE_COLOR related stuff.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Tried what you mentioned and it created many more problems that it solved.
The program began acting up and creating many errors with the MessageBox::Show function I am using.
Anyways, on second thought this should have gone in the managed forum but...
I am doing a System::Windows::Forms project, and changing the backcolor with this->BackColor . I have only found ways to let the user selectively change the color, but I would like to specify the color change myself. Do you know of a way to do this?
|
|
|
|
|
Ahh, yeah, the code I mentioned probably wouldn't work in .NET, it's Win32 api.
I think you might need to look up the System::Drawing::Color class and do something like:
BackColor = Color::Red; (or whatever your color choice)
I'm not a .net expert, so hopefully the Color class has a constructor like Color(R, G, B) if you need to do an arbitrary color.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Steve Echols wrote: I think you might need to look up the System::Drawing::Color class and do something like:
BackColor = Color::Red; (or whatever your color choice)
Thanks. That worked.
|
|
|
|
|
Hello
I have a little problem with my app.
I can double click on the assotiated file type (*.app) an the MainFraim of my Prog is started. But to display the information in the file there should also be a ChildFrame opened. And this doesn't happen!
Now how to add this function ?
And/od how to debug this (if Possible)?
THX
|
|
|
|
|
it is ur responsbility to open the file clicked.
check command-line of CWinApp then open a new Doc for the file.
A special image tool for C++ programmers, don't miss it!
The world unique Software Label Maker is here for you and me ...
A nice hyper tool for optimizing your MS html-help contents.
|
|
|
|
|
hi guyss
I am in a grt problem. when i use installshield to build the setup file of my project, i follow all the steps required properly. after that the setup file is also created but when i install it neither it comes in the "all program". also when i install it in any other computer not having vc++ after installation when i click the exe file to run it gives an error of a missing dll file but i have included this dll file while making the setup. please help as soon as possible as i have to submit the project by a week.
Thanksss
|
|
|
|
|
simanto_iem wrote: I am in a grt problem. when i use installshield to build the setup file of my project, i follow all the steps required properly. after that the setup file is also created but when i install it neither it comes in the "all program". also when i install it in any other computer not having vc++ after installation when i click the exe file to run it gives an error of a missing dll file but i have included this dll file while making the setup. please help as soon as possible as i have to submit the project by a week.Thankss
What exact is teh error Message.. if your Application is build in release mode!, as the debug mode dll may or may not be installed on destination computer
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Hi,
Does anyone know of a function that will allow me to distinguish between normal shell items like a text file or a folder & special items like MyComputer, shared documents folder, C drive, CD drive etc. In other words how do I get the attributes of these special folders.
Many thanks.
|
|
|
|
|
Please refer to the shell programing section here in code project:
http://www.codeproject.com/shell/[^]
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
How to make shell extension to monitor appliacation starting? I mean, before application startitng, I need to do something.
Thank you.
|
|
|
|
|
|
I am struggling with an issue. I have a client on who's machine, when a CFrameView derived class comes up, (3 do this) shuts down the app without a message of any kind. The code is now using VC++.net 2003 and the problem surfaced in an older version originally built in VC++ 6
I think I know what is happening. In the base classes if Create returns -1, it puts a message in the message que sutting down the app.
Initially the app started shutting down in a CWin derived splash window due to the same issue, I got that straightened out, but the situation seems to be a bit more pervasive
Does anyone have a clue, what causes -1 to be returned by window creation code?
This app runs fine on many other machines, and has been reliable for quite a while, two of these classes are those that I draw custom on, the other uses the CHTMLView and uses a locally created html file.
Christopher J. Noyes
|
|
|
|
|
Ive got a self made class called Vessel which is a base class for the classes Surface Vessel and Submarine.
The base class holds the data needed for both submarines and surface vessels and then the submarines and surface vessels have thier own data and functions.
Now i have tried to do a linked list where the data in the node is a "Vessel". I can pass a surface vessel or a submarine into this but when i try to get the data back out it loses the inherited class specificities (is that even a word? lol) so i can only access the Vessel data for that particular surface vessel or sub and not the particulars of that class. How can i put a surface vessel or sub into a type "vessel" and get the specific data out again or perhaps change the node data to a sv or sub on the fly?
Cheers
Bill
|
|
|
|
|
Add (pure) virtual functions that return desired information to Vessel, and override them in SurfaceVessel and Submarine.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
so when i assign a sub to the variable of type Vessel its still holding it as a sub but just cant see the functions / data?
-- modified at 13:31 Sunday 23rd April, 2006
heres the vessel class so how could i impliment the pure functions in them because vessel would have to be totally abstract would it not?
Cheers
#ifndef VESSEL_H
#define VESSEL_H
#include <string>
#include "afxwin.h"
using namespace std;
class Vessel
{
protected:
int _prKey;
int _maxSpeed;
CString _name;
int _length;
int _maxRange;
int _maxDisplacement;
int _crewAmount;
CString _vesselType;
public:
int getKey();
void setKey(int pKey);
int getSpeed();
void setSpeed(int pSpeed);
CString getName();
void setName(CString pName);
int getLength();
void setLength(int pLength);
int getRange();
void setRange(int pRange);
int getDisplacement();
void setDisplacement(int pDisplacement);
int getCrew();
void setCrew(int pCrew);
CString getType();
void setType(CString pType);
};
class SurfaceVessel : public Vessel
{
protected:
int _helicopters;
public:
int getHeli();
void setHeli(int pHeli);
};
class AircraftCarrier : public SurfaceVessel
{
int _aircraft;
public:
int getAircraft();
void setAircraft(int pAircraft);
};
class Destroyer : public SurfaceVessel
{
bool _gunSystem;
bool _guidedMissileSystem;
public:
bool getGunSystem();
bool getGuidedSystem();
void setGunSystem(bool pGunSystem);
void setGuidedSystem(bool pGuidedSystem);
};
class FleetAux : public SurfaceVessel
{
protected:
int _deckSpace;
public:
int getDeckSpace();
void setDeckSpace(int pDeckSpace);
};
class Submarine : public Vessel
{
protected:
int _maxDiveDepth;
int _maxSubmergedSpeed;
int _displacementSubmerged;
public:
int getMaxDiveDepth();
int getSubSpeed();
int getDisplacementSub();
void setMaxDiveDepth(int pMDD);
void setSubSpeed(int pSubSpeed);
void setDisplacementSub(int pDisSub);
};
class SSB : public Submarine
{
int _slbms;
public:
int getSLBMS();
void setSLBMS(int pSLBMS);
};
class SSK : public Submarine
{
int _torpedoes;
public:
int getTorps();
void setTorps(int pTorps);
};
class Tanker : public FleetAux
{
int _storageCapacity; //Litres
public:
int getStorCap();
void setStorCap(int pStorCap);
};
class LandingPlat : public FleetAux
{
int _storageCapacity; // M^3
int _aircraftLand;
public:
int getStorCap();
void setStorCap(int pStorCap);
int getAircraftLand();
void setAircraftLand(int pAirAmount);
};
#endif
-- modified at 13:56 Sunday 23rd April, 2006
|
|
|
|