|
Can someone take a look at my code to see what I'm doing wrong.
int score;<br />
char grade =0;<br />
<br />
<br />
cout << endl;<br />
cout << "Enter total score (float, must be <= 100): "; <br />
cin >> score; <br />
<br />
if (score >= 85); <br />
grade = 'A';<br />
else if (score >= 75); <br />
grade = 'B';<br />
else if (score >= 65); <br />
grade = 'C';<br />
else if (score >= 55);<br />
grade = 'D';<br />
else <br />
grade = 'F';<br />
End if
End if
End if
End if
<br />
<br />
<br />
<br />
cout << endl; <br />
cout << "Your grade for CMSC 101 is: " << grade << endl; <br />
return (0);
|
|
|
|
|
dtaylor01 wrote: if (score >= 85);
else if (score >= 75);
else if (score >= 65);
else if (score >= 55);
remove the semi colon's at the end of if statement and else if statement...
|
|
|
|
|
In addition to Naveen reply:
dtaylor01 wrote: End if
There's not an End if keyword in C/C++ .
Bottom line: Good C/C++ tutorial required.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You've been given answers to the problems I can see (and I didn't catch the ; first).
But you didn't need us to tell you about them - any compiler would have choked on your code, and treated you to a barrage of bad language. Those errors are your (sarcastic picky) friend. Apart from anything else, people who come here to help expect you to have done some of the heavy lifting already.
1/ "else"? whatdaya mean "else". There should be an if first!
would have given you a hint towards the ; Not an easy hint one though.
I personally dislike using the style for very trivial ifs, but many people insist on putting squiggly brackets round every if case, which will make it easier for you while starting. It also makes it harder to make an error when you add more than one line to and if case.
eg:
if (score > 85)
{
grade = 'A';
}
else if (score >= 75)
{
2/ "end"? Never heard of it.
Easy hint.
|
|
|
|
|
Hi.I'm trying to compile a project with Visual Studio 2008, but I need to modify my linker settings to include wsock32.lib. How can I do this?
|
|
|
|
|
Go to Project > Properties in the main window, then in the dialog that pops up, Configuration Properties > Linker > Input. Type the library name into the Additional Dependencies box.
Regards,
--Perspx
"I've got my kids brainwashed: You don't use Google, and you don't use an iPod." - Steve Ballmer
"Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen an angry penguin charging at them in excess of 100mph." - Linus Torvalds
|
|
|
|
|
I have this class:
class CPROXY
{
public:
CPROXY(void);
~CPROXY(void);
int CreateConnector();
int CreateAcceptor(int nPort);
:
:
:
private:
ACE_SOCK_Acceptor client_acceptor;
ACE_SOCK_Connector server_connector;
std::list <spair*> queue;
}
Now the implementation is like this:
int CPROXY::ServerThreadRunner()
{
:
DWORD thid;
HANDLE hServerThread = CreateThread(NULL, 0, ServerThread, this, 0, &thid);
:
:
}
DWORD WINAPI CPROXY::ServerThread(LPVOID param)
{
CPROXY *newobj = (CPROXY *)param;
newobj->CreateConnector();
return 0;
}
int CPROXY::CreateConnector()
{
EnterCriticalSection(&guard);
SPAIR *sPair = queue.front();
queue.pop_front();
LeaveCriticalSection(&guard);
SetEvent(wait_server);
:
:
:
Take a look at queue. Queue is declared as a list of SPAIR. Do you think it is necessary for me to guard it with critical section every time is accessed by a thread?
I mean, before i run the thread i did put it into a newobj, so meaning that every newobj has its own member so no need for me to put critical section to guard it, right?
PLEASE, correct me if I'm wrong! i need to optimize this code.
|
|
|
|
|
auralius wrote: before i run the thread i did put it into a newobj, so meaning that every newobj has its own member
I'm not sure what you mean by that.
Your code doesn't show how many CPROXY objects are created.
Just because you do this:
CPROXY *newobj = (CPROXY *)param;
doesn't mean there's a new CPROXY object - it's just a pointer to an object
that is passed to the thread.
If every thread has its own CPROXY object, then no, you don't need synchronization.
If every thread is using the same CPROXY object, and there's any chance more than
one thread can access this object at the same time, then yes you need synchronization.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank for your reply.
I mean there is a main function that will call int ServerThreadRunner(). Meaning that for every thread will be handled by new CPROXY newobj pointer.
So, if it is only pointer pointing the same location, does it mean that i need to put synchronization for every data member that accessed by the thread function?
How about this:
std::list <spair> queue;
vs
std::list <spair*> queue;
which one needs synchronization, and which one doesn't need?
What came to my mind is: there is no need for synchronization unless i put the queue as a static variable. am i right?
static std::list <spair*> queue;
sorry if my question is not really good. i hope you understand. i can't just solve that problem by trial & error because both of them work. I need strong concept.
|
|
|
|
|
auralius wrote: eaning that for every thread will be handled by new CPROXY newobj pointer.
Different pointers to CPROXY objects is not the same as different CPROXY objects.
All the threads will have their own pointers but they could all point to the same
object, in which case you'd need synchronization.
If there's a separate CPROXY object for every thread, then no synchronization is necessary.
auralius wrote: there is a main function that will call int ServerThreadRunner()
Can you show how you do that?
auralius wrote: What came to my mind is: there is no need for synchronization unless i put the queue as a static variable. am i right?
No. It all comes down to actual object instances. Multiple threads can access
a single object instance no matter how it's stored. If multiple threads can
change the same object, then that object should be protected with synchronization.
You haven't shown relevant code so it's hard to tell if you need it there or not.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
class CPROXY
{
public:
CPROXY(void);
~CPROXY(void);
int Run(int nPort);
static DWORD WINAPI MotherThread(LPVOID param);
static DWORD WINAPI DaughterThread(LPVOID param);
int MotherThreadWorker();
int DaughterThreadWorker();
int GetAddressAndPort(char * cStr, char *cAddress, int * nPort);
private:
ACE_SOCK_Acceptor client_acceptor;
ACE_SOCK_Connector server_connector;
CRITICAL_SECTION guard;
HANDLE wait;
bool isMotherThreadRunning;
bool isDaughterThreadRunning;
static std::list<ACE_SOCK_Stream> queue;
};
The implementation is like this:
CPROXY::CPROXY(void)
{
InitializeCriticalSection(&guard);
wait = CreateEvent(NULL, FALSE, FALSE, NULL);
}
CPROXY::~CPROXY(void)
{
}
std::list<ACE_SOCK_Stream> CPROXY::queue;
int CPROXY::Run(int nPort)
{
DWORD thid;
HANDLE hMotherThread = CreateThread(NULL, 0, MotherThread, this, 0, &thid);
if (!hMotherThread)
return -1;
ACE_SOCK_Stream client_stream;
ACE_INET_Addr addr;
addr.set(nPort, addr.get_ip_address());
int e = client_acceptor.open(addr);
if (e == INVALID_SOCKET)
return -1;
while(true)
{
int e = client_acceptor.accept(client_stream);
if (e == INVALID_SOCKET)
continue;
EnterCriticalSection(&guard);
queue.push_back(client_stream);
LeaveCriticalSection(&guard);
}
return 0;
}
DWORD WINAPI CPROXY::MotherThread(LPVOID param)
{
CPROXY *newobj = (CPROXY *)param;
newobj->MotherThreadWorker();
return 0;
}
int CPROXY::MotherThreadWorker()
{
isMotherThreadRunning = true;
while (isMotherThreadRunning)
{
EnterCriticalSection(&guard);
bool isEmpty = queue.empty();
LeaveCriticalSection(&guard);
if (!isEmpty){
DWORD thid;
HANDLE hDaughterThread = CreateThread(NULL, 0, DaughterThread, this, 0, &thid);
if (!hDaughterThread)
continue;
printf("\nWAITING!\n");
WaitForSingleObject(wait, INFINITE);
printf("\nFINISHED!\n");
}
}
return 0;
}
DWORD WINAPI CPROXY::DaughterThread(LPVOID param)
{
CPROXY *newobj = (CPROXY *)param;
newobj->DaughterThreadWorker();
:
:
return 0;
}
int CPROXY::DaughterThreadWorker()
{
char buf[BUFSIZE];
char cServerAddress[256];
int nServerPort;
EnterCriticalSection(&guard);
ACE_SOCK_Stream client_stream = queue.front();
queue.pop_front();
LeaveCriticalSection(&guard);
SetEvent(wait);
:
:
:
return 0;
}
Here is the main file:
int main(int argc, char *argv[])
{
CPROXY *MyProxy = new CPROXY;
MyProxy->Run(ACCEPTOR_PORT);
while(1){}
return 0;
}
I've modified the code, now i put the list queue as a static. So what do you think? How if I change it into non-static. Does it still need synchronization? I also change the way i declare it.
From:
static std::list<ACE_SOCK_Stream> *queue;
To
static std::list<ACE_SOCK_Stream> queue;
So it is no longer a dynamic allocation. I just wanna do something with reason. If i use synchronization, i must be able to state clearly why i need it.
thank you vey2 much for your help...
|
|
|
|
|
auralius wrote: Does it still need synchronization?
Yes.
Based on the code shown (if I read it correctly) you always have
the possibility of two threads simultaneously accessing the one queue -
either the main thread and the MotherThread or the main thread and the
DaughterThread.
There's only one queue object because there's only one CPROXY object, so
it doesn't make any difference where/how the one object is allocated.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
modified on Wednesday, October 22, 2008 12:59 AM
|
|
|
|
|
Thank you very much for your help... i think i need to learn more and more...
|
|
|
|
|
You're welcome!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have got dwmapi.dll and got the dwmapi.h from the windows sdk, which seems to include find but
i added this line which should work
HRESULT result = ::DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
and i get this error
1>------ Build started: Project: glasstrans, Configuration: Debug Win32 ------
1>Linking...
1>Main.obj : error LNK2019: unresolved external symbol __imp__DwmEnableComposition@4 referenced in function _WinMain@16
1>C:\Users\Sarriss\Desktop\Glass_blur transparent\glasstrans\Debug\glasstrans.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Sarriss\Desktop\Glass_blur transparent\glasstrans\glasstrans\Debug\BuildLog.htm"
1>glasstrans - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
can anyone tell me whats going on there
|
|
|
|
|
Did you add the dwmapi.lib to your list of dependencies in project properties?
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
ahh silly me that would be the problem solved, um i did a pure win32 api window, and as it created it naturally put a client area
to work with, but when i use the dwmextendintoclientarea command it extends behind this area :S any way to stop that
|
|
|
|
|
I've never used that library, so I'm afraid I can't help further than I have already.
“Cannot find REALITY.SYS...Universe Halted.”
~ God on phone with Microsoft Customer Support
|
|
|
|
|
its alrigh, got it i originally had it set so it tried to proceed into the client area when it activated
stuck it in wm_paint and hey presto
|
|
|
|
|
Good morning,
I need help in designing a class in C++. For experimental purposes I started with something simple.
// Header File Planets.h Declares class Planets.
class Planets
{
public:
double Mercury;
private:
double weight;
};
//Implementation file Planets.cpp implements the member
//functions of Class Planets.
#include "stdafx.h"
#include "Planets.h"
double Planets::Mercury
// Pre: Earth weight.
// Post: Earth weight times the gravitation constant
// of Mercury - 0.4155.
{
return wgh * 0.4155;
}
// MT_Planets.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include "Planets.h"
using namespace std;
int main()
{
double iWgh = 235.6;
double MyWgh;
MyWgh = Planets.Mercury(iWgh);
cout << "My weight on Mercury is: " << MyWgh << endl;
system("pause");
return 0;
}
When I try to compile the project I get the following error:
Error 1 error C2063: 'Planets::Mercury' : not a function c:\Documents and Settings\Mark McCumber\My Documents\Visual Studio 2005\Projects\Visual C++\MT_Planets\MT_Planets\Planets.cpp 10
Can someone explain to me what I'm doing wrong?
Quecumber256
|
|
|
|
|
Quecumber256 wrote: class Planets
{
public:
double Mercury;
double Mercury();
Quecumber256 wrote: double Planets::Mercury
double Planets::Mercury()
{
return wgh * 0.4155;
}
Mercury - It is a function? Then you missed the ()
Quecumber256 wrote: system("pause");
You MUST not do this. This is bad in several ways. Something like a getch(); or std::cin.get(); will do instead.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Rajesh,
Thank you for the reply, but I'm still getting a problem. I'm trying to reprogram my brain to use OOP instead of procedural programming.
The problem is simple in concept. Take a person's Earth weight and calcultate their weight on the other planets of our Solar system. The trick here is it has got to be done using classes and constructors.
For example: MyWgh = Planets.Mercury(235.5) would give me their weight on the planet Mercury. According to the instructions the default constructor should return the weight enter for Earth.
Can you help me grasp this concept?
Quecumber256
|
|
|
|
|
Something similar to:
#include <iostream>
using namespace std;
class Planet
{
private:
double _dFactor;
public:
Planet(double dFactor = 1.0) : _dFactor(dFactor) {}
double localWeight(double dWeightOnEarth)
{
return dWeightOnEarth * _dFactor;
}
};
void main()
{
Planet planetEarth, planetMercury(.38);
double dYourWeight = 235.6;
cout << "If your weight on Earth is " << planetEarth.localWeight(dYourWeight) << endl;
cout << "then weight on Mercury is " << planetMercury.localWeight(dYourWeight) << endl;
}
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks, this example helped me with my problem.
Quecumber256
|
|
|
|
|
I'm glad of.
BTW did you notice .38 instead of 0.4155 ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|