|
You'd do better to add a line that reads input from the console. The point of a breakpoint, is that the program stops and you can examine variables and start it again.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I added a scanf before printf and added a breakpoint a step back of } .. it works great but why the console stucks there and no responding?do you think it is a Visual Studio problem ?
|
|
|
|
|
No, the console gets stuck because that is what a breakpoint is for. You need to add a line AFTER the printf, if you want the program to stop and let you see the printf output.
Regarding your assignment, what you need to do is have two loops, that's called a nested loop. One loop controls the number of lines you print, the other loop prints the number of #s in the line. Have you covered for loops in class ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
A half an hour about FOR and a statement about do while,nothing more.
I think you didnt get me.My english are not so good.
I added a breakpoint after printf just like this:
#include<stdio.h>
main()
{
int a;
scanf("%d",&a);
printf("Hello\n");
BREAKPOINT
}
I mean not responding.I have to press CTRL + ALT + DEL to close it from there.
That this happens with the breakpoint embedded.Without it it works great but i cannot see the printf.With it i can but it crushes!i finally found the word i need!
Can you please tell me the code about that program with the asterisks? with do...while at least.It is too difficult to find it out.
|
|
|
|
|
If you add a break point, the program will stop. If it crashes, then it's broken. If you did what I suggested, then you wouldn't need a breakpoint.
main()
{
int a;
printf("Hello\n");
scanf("%d",&a);
}
This will stop and wait for you to enter a number.
WakenDJ wrote: Can you please tell me the code about that program with the asterisks? with do...while at least.It is too difficult to find it out.
No - the point of your course is that you learn both C, and how to do a little research. I've told you all you should need to know. The next step is to ask yourself, what does 'do while' do ? How can it be used to create this output you're after ? What data do you need to generate that output ? Think about it in small steps. If you still can't work it out, you need to speak to your teacher for some help, so she knows your level of ability and the difficulty you're having and can tailor her efforts to helping you understand these basic concepts.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Your are right... thanks for your help!
so far i found this:
#include<stdio.h>
main()
{
int num;
for(num=0;num<=10;num++)
printf("*\n");
}
I know how to show the first numbers like 1,3,5,7,and i think it works the same way but nothing so far!
|
|
|
|
|
The output of this code will be:
*
*
*
*
*
*
so on... till ten times
Edit: it repeats itself 11 times (I didn't realize about the i <= 10)
-- modified at 3:13 Monday 14th May, 2007
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
WakenDJ wrote: int num;
for(num=0;num<=10;num++)
printf("*\n");
this is a good start. Now think, how many times does this loop fire ? The answer is 11. You want to do this:
for(num=1;num<11;num++)
to count from 1 to 10.
Now ask yourself, you want to print out 10 lines, right ? The first line has 1 * in it, the second has 2, and so on. So, how will you know on each line, how many stars to print out ? And, can you write a loop to print those stars out ? Then you just need after your loop to print a \n to go to the next line. Actually, I thought it was \r\n.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
maybe it need to FOR loops?!
|
|
|
|
|
Right, you need a second for loop, this is called a nested loop
int i, k
for (i=1;i<11;++i)
{
for(k=1;k<11;++k)
printf("*");
printf("\n");
}
This loops 10 times. Each time, the inner loop counts from 1 to 10, and prints out 10 stars. How can you make it print out once the first time, twice the second time, etc ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
i dont know... i am really comfused after this.cant find it out.i havent understand the theory very well.
thanks for your help! Really!
you cant tell me the solutions for sure? I will learn from it i am so close but nothing.In 2 hours i have to give it! please ?! and with do while?
|
|
|
|
|
Someone has given you the answer
I'm sorry, this is as simple as it gets. If you can't grasp it, you need to go and talk to your teacher, perhaps you're not cut out for this course, or perhaps you need some one on one attention from your teacher to get you started on how this all works.
In any case, C is as hard a language as you're likely to learn ( although this example is so simple that it's fundamentally the same in any language, my advice would stand even if you were learning VB )
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I have been the one who has given the answer. After all the messages and the petition of:
you cant tell me the solutions for sure? I will learn from it i am so close but nothing.In 2 hours i have to give it! please ?!
I think he has understand that should learn harder. I'm agree with you, but every beginning is difficult.
@wakenDJ
try to get some manuals from the net, there are terabytes about C and C++. Make lots of tries and error programms, and ask whenever you have problems or doubts, but don't ask for the solutions of your homeworks because you will maybe have them, but you will never learn. It's up to you to go on and learn programming, or just pass a course without profits for you.
Edited
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
But this will give:
**********
**********
**********
and so on, as output. And that was not the question.
There principle is this, but there is two ways to do it.
1)
int i = 0, j = 0;
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= i; j++)
printf ("*");
printf ("\n");
}
the first loop determines the line and the number of stars, and the second wirte all the stars from 1 till the number of the line.
2)
int i = 0, j = 0;
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
{
printf ("*");
if (j == i)
break;
}
printf ("\n");
}
This is more or less the same as the first one, but putting both limits to 10 and breaking the second loop when it reaches the number of the line (and consecuentally the number of stars that should be written)
EDIT: With the do... while is more or less the same, in my previous message (chronologically, but below this one in forum-space) I have written you the differences and structure of for, while and do.. while.
Just translate it to whatever you want to use and we correct you
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Hi, try this
#include
main()
{
int a;
scanf("%d",&a);
printf("Hello\n");
getchar ();
}
or
#include
main()
{
int a;
printf("Hello\n");
scanf("%d",&a);
}
about the question of the for...
for (int i = 0; i < nMax; i++)
//do something
is the same as
int i = 0;
while (i < nMax)
{
//Do something
i++
}
and the difference with
int i = 0;
do
{
//do something)
i++;
} while (i < nMax)
is the this last one ALWAYS execute code at least one time before checking the condition.
About your homework... its your homework, so u have to learn with your own effors. We can and will help you, but not give you the written code
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Instead of running it from within the IDE, open a command prompt first and run it from there.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i need to write these commands only and run:
#include<stdio.h>
main()
{
printf("Hello\n");
}
A step by step tutorial on Visual Studio 2005 please...
Dont need other libraries such stdafx.h and other code such :
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Just want to run the command above without any errors just i did in my lesson ,but in Visual Studio 6
Thanks
|
|
|
|
|
The code you have given is in C platform but is acceptable in C++. No more stories dear...
Open Visual C++ from your machine.
Create new source file and save it using appropriate name.
Type in the following code:
#include<iostream>
using namespace std;
void main()
{cout<<"Hello\n";
}
amets is a young talented and enthusiastic upcoming software developer in one of the kenyan public universities.Computing problems are no more with stema. Try him and you will never regret.
|
|
|
|
|
I'm trying to get my code to create a thread pool class that fires off 5 threads and prints the instantiation sequence of the threads right before terminating by raising an event from the spawned thread itself, but I'm getting the following linker errors when compiling with VC8:
1>EventHandler.obj : error LNK2019: unresolved external symbol "private: static class CSource * CThreadPool::source" (?source@CThreadPool@@0PAVCSource@@A) referenced in function "private: static unsigned int __stdcall CThreadPool::DoWork(void *)" (?DoWork@CThreadPool@@CGIPAX@Z)
1>EventHandler.obj : error LNK2019: unresolved external symbol "private: static class CReceiver * CThreadPool::receiver" (?receiver@CThreadPool@@0PAVCReceiver@@A) referenced in function "public: __thiscall CThreadPool::CThreadPool(class CSource *,class CReceiver *)" (??0CThreadPool@@QAE@PAVCSource@@PAVCReceiver@@@Z)
The below is my source code:
#include <stdio.h>
#include <process.h>
#include <windows.h>
[event_source(native)]
class CSource
{
public:
__event void MyEvent(int nValue);
};
[event_receiver(native)]
class CReceiver
{
public:
void MyHandler(int nValue)
{
printf("Thread %d terminating...\n", nValue);
}
void hookEvent(CSource* pSource)
{
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
void unhookEvent(CSource* pSource)
{
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
};
class CThreadPool
{
public:
CThreadPool(CSource* source, CReceiver* receiver)
{
CThreadPool::source = source;
CThreadPool::receiver = receiver;
receiver->hookEvent(source);
}
~CThreadPool()
{
receiver->unhookEvent(source);
}
void Initialize()
{
SpawnThreads();
}
private:
static CSource* source;
static CReceiver* receiver;
void SpawnThreads()
{
HANDLE hThread[5];
unsigned threadID[5];
for (int i = 0; i < 5; i++)
{
hThread[i] = (HANDLE)_beginthreadex(0, 0, &CThreadPool::DoWork, (void*)i, 0, &threadID[i]);
}
};
static unsigned int __stdcall DoWork(void* pArgs)
{
unsigned int x = 0;
for (int i = 0; i < 1000000; i++)
{
x++;
}
__raise CThreadPool::source->MyEvent(*(int*)pArgs);
return x;
}
};
int main() {
CSource source;
CReceiver receiver;
CThreadPool* threadPool = new CThreadPool(&source, &receiver);
threadPool->Initialize();
}
|
|
|
|
|
You have to initialize static members outside of the class declaration. So in a .cpp file you need something like:
CSource* CThreadPool::source = null;
or whatever it should be and of course the other static member as well.
led mike
|
|
|
|
|
It seems like the problem was that I didn't separate my .cpp and my .h when I should have, making things more confusing.
Thanks.
|
|
|
|
|
Hello,
Here is the function:
void CAssignment04View::OnPrint(CDC* pDC, CPrintInfo* pInfo)<br />
{<br />
int iRowCount = 1;
CString cNumeric;
CPen oPen;
CBrush oBrush;
CFont oTextFont;
CFont oHeadFont;
CFont oColFont;
LOGFONT lfFont;
CSize oFontSize;
COLORREF clrRef;
int iRowPos = 120;
int iTextHeight = 0;
CRect oDrawRect;
int iRecNumPos;
COleCurrency Salary;<br />
<br />
int iEmployeeNumPos; <br />
int iFirstNamePos; <br />
int iLastNamePos;<br />
int iAgePos;<br />
int iDepartmentPos;<br />
int iSalaryPos;<br />
<br />
BOOL EndOfRowset = false;
HRESULT hr;
<br />
<br />
oDrawRect = pInfo->m_rectDraw;<br />
clrRef = 0x00000000;<br />
<br />
oPen.CreatePen(PS_SOLID, 2, clrRef);<br />
pDC->SelectObject(&oPen);<br />
clrRef = 0x00C0C0C0;<br />
oBrush.CreateSolidBrush(clrRef);<br />
pDC->SelectObject(&oBrush);<br />
oHeadFont.CreatePointFont(240, "Arial", pDC);<br />
pDC->SelectObject(&oHeadFont);<br />
oFontSize = pDC->GetOutputTextExtent("HR DataBase");<br />
pDC->Ellipse(500,<br />
iRowPos - (oFontSize.cy / 2) - 10,<br />
oDrawRect.Width() - 500,<br />
iRowPos + (oFontSize.cy / 2) + 10);<br />
pDC->SetBkMode(TRANSPARENT);<br />
pDC->TextOut((oDrawRect.Width() - oFontSize.cx) / 2,<br />
iRowPos - (oFontSize.cy / 2) - 10,<br />
"HR DataBase");<br />
pDC->SetBkMode(OPAQUE);<br />
oHeadFont.GetLogFont(&lfFont);<br />
iRowPos = abs(lfFont.lfHeight) + 175;<br />
oTextFont.CreatePointFont(120, "Arial", pDC);<br />
oTextFont.GetLogFont(&lfFont);<br />
iTextHeight = abs(lfFont.lfHeight) + 10;<br />
lfFont.lfWeight = 700;
oColFont.CreateFontIndirect(&lfFont);<br />
pDC->SelectObject(&oColFont);<br />
<br />
iRecNumPos = int(oDrawRect.Width() / 17);<br />
iFirstNamePos = iRecNumPos + 50 + pDC->GetOutputTextExtent("##").cx;<br />
iLastNamePos = iFirstNamePos + 50 + pDC->GetOutputTextExtent("###########").cx;<br />
iAgePos = iLastNamePos + 100 + pDC->GetOutputTextExtent("###########").cx;<br />
iDepartmentPos = iAgePos + 150 + pDC->GetOutputTextExtent("##").cx;<br />
iSalaryPos = iDepartmentPos + 150 + pDC->GetOutputTextExtent("##########################").cx;<br />
<br />
<br />
pDC->TextOut(iRecNumPos, iRowPos, "#");<br />
pDC->TextOut(iFirstNamePos, iRowPos, "First Name");<br />
pDC->TextOut(iLastNamePos, iRowPos, "Last Name");<br />
pDC->TextOut(iAgePos, iRowPos, "Age");<br />
pDC->TextOut(iDepartmentPos, iRowPos, "Department");<br />
pDC->TextOut(iSalaryPos, iRowPos, "Salary");<br />
<br />
iRowPos += iTextHeight;<br />
pDC->MoveTo(iRecNumPos, iRowPos);<br />
pDC->LineTo(oDrawRect.Width() - iRecNumPos, iRowPos);<br />
iRowPos += 30;<br />
pDC->SelectObject(&oTextFont);<br />
iTextHeight = 20 + pDC->GetOutputTextExtent("Xy").cy;<br />
m_pSet->MoveFirst();<br />
while (!EndOfRowset)<br />
<br />
{<br />
itoa(iRowCount, cNumeric.GetBuffer(10), 10);<br />
cNumeric.ReleaseBuffer(-1);<br />
pDC->TextOut(iRecNumPos, iRowPos, cNumeric);<br />
pDC->TextOut(iFirstNamePos, iRowPos, CString(m_pSet->m_Employee_Name_First));<br />
pDC->TextOut(iLastNamePos, iRowPos, CString(m_pSet->m_Employee_Name_Last));<br />
<br />
itoa(m_pSet->m_Employee_Age, cNumeric.GetBuffer(10), 10);<br />
cNumeric.ReleaseBuffer(-1);<br />
pDC->TextOut(iAgePos, iRowPos, cNumeric);<br />
<br />
pDC->TextOut(iDepartmentPos, iRowPos, CString(m_pSet->m_Department));<br />
<br />
<br />
iRowPos += iTextHeight;<br />
iRowCount ++;<br />
<br />
m_pSet->MoveNext();<br />
if (m_pSet->IsEOF())<br />
{<br />
<br />
EndOfRowset = true;<br />
break;<br />
}<br />
<br />
<br />
}<br />
<br />
CRecordView::OnPrint(pDC, pInfo);<br />
}
Now the problem:
When you start the application you can navigate the records fine. You can then click print preview and it displays what you might expect. Now if you close the print preview window the recordset has navigated to the end. When you try to click either the << or < button it displays the following error message:
operation failed, no current record
Any suggestions what might be wrong. If you don't click the print preview button then you can navigate the records without problem.
Many Thanks.
|
|
|
|
|
For starters, here's the relevant code...
void CAssignment04View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
BOOL EndOfRowset = false; // End of the rowset?
while (!EndOfRowset) {
// See if this is the last row.
m_pSet->MoveNext();
if (m_pSet->IsEOF()) {
EndOfRowset = true;
break;
}
}
CRecordView::OnPrint(pDC, pInfo);
}
/*
From MSDN:
If the last record is the current record when you call MoveNext, IsEOF will subsequently return nonzero. If IsEOF returns nonzero and you call MoveNext, an error occurs.
If IsEOF returns nonzero, the current record is undefined, and any action that requires a current record will result in an error.
*/
|
|
|
|
|
Thanks the advice. I don't suppose the MSDN page provided a solution to this ?
I got the code working when using OLE as my connection method using this code:
iRowPos += iTextHeight;<br />
iRowCount ++;<br />
hr = m_pSet->MoveNext();<br />
if (hr == DB_S_ENDOFROWSET)<br />
{<br />
EndOfRowset = true;<br />
break;<br />
}
However when this code is run using ODBC I get this error which I cannot fix:
error C2440: '=' : cannot convert from 'void' to 'HRESULT'
|
|
|
|
|
Lewis01 wrote: I don't suppose the MSDN page provided a solution to this ?
Did you try resetting the position to the first record before the break or something like that to set the position to a valid record before the function exits?
CRecordset::SetAbsolutePosition
Lewis01 wrote: I got the code working when using OLE as my connection method using this code:
Honestly, that code snippet makes no sense to me. What exactly is m_pSet in the context of the code snippet you just provided. I was assuming you were using a CRecordset but HRESULT is usually used with COM so I'm a little lost.
|
|
|
|
|