Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

lines 11 and 12: text is an array of char, not an array of structures, it should be consoletext.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The function is used to write a 2 dimensional array of bytes to the screen, so the second parameter is a 2d array of structures.

Example program is given here. This example reads the text that's on the screen then changes the coordinates so that the text will be written in a different position on the screen. If you want to write all new text you don't have to read from the screen but instead create a new array of structures and fill them in with the text/attribute info

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ntnkmr: Your version does too much work -- it's not necessary to use a temporary buffer. Once you find the first non-space character move all remaining characters to the beginning of the line.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It's really not all that complex. You already posted most of code, just expand it a little. Add another parameter to SplitLine() that contains the ID that it should look for.

For each line read in the file
    call SplitLine() to split the line into words
    compare the ID from the line with the ID you want
    if the two are the same then print the contents of the line
end of loop 
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes, you could do that, but that function is used to change the color of existing text. If you want to write new text in a specific color then call SetTextColor()

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to read the remarks for that function:

If the number of attributes to be written to extends beyond the end of the specified row in the console screen buffer, attributes are written to the next row. If the number of attributes to be written to extends beyond the end of the console screen buffer, the attributes are written up to the end of the console screen buffer.

The last parameter to that function is a pointer to a DWORD object that is created in your program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Since && and || are the same precedence, technically there is no need for the parentheses. But for human eyes you are right in that the code is more understandable with the parentheses. The two functions should produce the same identical results.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Leap year occurs every 4 years. If the year is evenly divisible by 100 then it is not a leap year, unless it is evenly divisible by 400. For example: the year 2000 was a leap year because it is evenly divisible by 400, but the year 1800 was not a leap year because it is evenly divisible by 100 but not 400. So 1600, 2000 and 2400 are leap years but 1700, 1800, 1900, 2100, 2200 and 2300 are not.

Now, in the function you posted, the && and || operators have the same precedence, so just read from left to right.

rubberman commented: Good explanation AD. :-) +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no such operator as "--". You probably misundertood your instructor, he/she probably meant "==", which is the text to see if both sides of the operator are identical (called the equal operator).

The code you posted already contains examples of operator overloading, just follow those examples to create the new operators.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is "--" ? do you mean "+=" since you also specified "-="?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Nice idea, but they'd better not be based in USA or the US government will most likely shut them down for national security reasons.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is that all the instructions you were given? Have you ever seen a telephone or cell bill? Among other things it will contain a detailed list of all the chargeable phone calls you made, how long the calls were, the cost per minute, and the total cost for each call. Your program will most likely have to contain similar information. so, you can create a class that allows you to enter the info for a single call and save it to a file. At some time later your program will prepare a bill.

Now, think about what your program needs to do, and create a menu that allows you to do them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

CreateFont() or CreateFontIndirect(). You are already calling that in the code you postged in your other thread.

Also -- you shouldn't have two threads for the same question, it just causes confusion. This is my last post in this thread.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

because letters are wider than spaces. Use a fixed-width font and it should work.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You might find some interesting suggestions here at CodeProject, the world's largest repositary of free MS-Windows programs and tutorials on the net.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Doesn't blink, using VC++ 2012. This version blinks. I marked the lines I changed with //<<<<<<<<<<<<<<

//blinktext.h
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>
#include <iostream>

using namespace std;

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    size_t tlen = p->Text.length()+1; //<<<<<<<<<<<<<<
    char *EmptyText = new char[tlen];
    memset(EmptyText, 'd', tlen);

    HDC hDC=GetDC(GetConsoleWindow());

    while (true)
    {

        SetBkMode(hDC,TRANSPARENT);
        SetTextColor(hDC,RGB(0,255,0));
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length());//<<<<<<<<<<<<<<
        Sleep(500);
        SetTextColor(hDC,RGB(0,0,0));
        SetBkMode(hDC,OPAQUE);
        SetBkColor(hDC,RGB(0,0,0));
        TextOut(hDC,p->x,p->y,EmptyText,(int)p->Text.length());//<<<<<<<<<<<<<<
        Sleep(500);
    }
    return 0;
}

void TextBlink(string text, int x, int y, int TextColor, int BackColor)
{
    Blink *b=new Blink;
    b->Text=text;
    b->BackColor=BackColor;
    b->TextColor=TextColor;
    b->x=x;
    b->y =y;
    _beginthreadex(NULL, 0, BlinkLoop  ,b, 0, NULL);
}



int main()
{
    TextBlink("Hello world", 20,20,3,3);
    cout << "hello world";//like you see, the caret(text cursor) isn't losed


    cin.get();
    return 0;
}
kal_crazy commented: Yup this works as well +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 23: failed to allocate memory for the null string terminator.

Lines 37 and 39: remove +1 from Text.Length().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

But you gave me this:

Then correct it in your program.

Does this take into account the fact that I have four lines in the Employee.txt file?

No -- strtok() only works on one line. Notice that SplitLine() is called repeatedly for each line in the file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

do you know these code?

Read all about it in this MSDN article

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I just ran your program with VC++ 2012 console and it did nothing.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Nobody says you have to upgrade -- you are free to use that 20-year-old software and hardware if you wish.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Your first mistake was creating a console program -- you should have created a win32 program which uses WinMain() instead of main(). Here is a good tutorial to get you started.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The question was whether or not they can share the code snippets.

Isn't that what I said? But maybe the OP needs to clarify what he means instead of us just guessing.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

pragmas are not portable from one compiler to the other. Apparently Code::Blocks knows nothing about that pragma. CB has project settings for that purpose.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

did you include windows.h?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Dani I think you misunderstood -- the question is can people use the code posted on DaniWeb in their own programs?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is on line 17 and 20, not with TextOut(). Neither of those two lines are needed, and they are both wrong anyway. The whole function is just too complicated.

unsigned __stdcall BlinkLoop(string Text, int x, int y)
{
    char EmptyText[Text.length()+1];
    memset(EmptyText,' ', sizeof(EmptyText));
    EmptyText[ sizeof(EmptyText)-1) ] ='\0';
    HDC hDC=GetDC(GetForegroundWindow());
    while (true)
    {
        TextOut(hDC,x,y,Text.c_str(),Text.length);
        Sleep(1000);
        TextOut(hDC,y,x,EmptyText,Text.length);
        Sleep(500);
    }
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why does result() have two parameters when only the first parameter is needed?

int result (int number)
{
    int number_rev = 0;
    while (number != 0)
    {
        number_rev = (number_rev * 10) + (number % 10); 
        number = number/10;
    }

    return number_rev;
}
ddnpresident commented: Thank you so much for the help, I don't know what I was thinking when I did the code initially +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

did you google fgets() to find out what the parameters are? If you did then you would find the answer (correction) to the error.

SplitLine() needs to save the strings in an array instead of just printing them to the screen. Then after SplitLine() returns the program can compare the strings to see if the row just read has the ID you want. It's just a simple strcmp() call.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

As always, Mike is correct; I stand corrected. The idom "You can't teach an old dog new tricks" is wrong.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

scanf_s("%i", &a[*counter]);

scanf() expects the first parameter to be a pointer, so you have to add the pointer operator &.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

this is David's first post, so he hasn't posted any code at all yet.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Mike and I are actually saying the same thing, just in slightly different ways. The get_first_value() function he posted is not initializing x, it is just simply setting x to the value of the parameter. That isn't the same thing as initializing x. That function could have been written like below, where x is an unitialized variable until it is set to the parameter.

int get_first_value(int value) {
  static int x;
  x = value;
  return x;
};

Now had the function been written like below, the value of x would be 10 even before get_first_value() is called. There is no executable code line that sets the value of x to 10, the variable is set to 10 at the time memory is reserved for the variable.

    int get_first_value(int value) {
      static int x = 10;
      x = value;
      return x;
    };
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

and how do I switch from lowest to highest to highest to lowest

Are you talking about the sort order? e.g. switch from ascending to descending order? Study the link I gave you to find out how to code the bubble sort, it even gives you the code for it. One of the lines compares two values, to switch from ascending to descending order all you have to do is change the comparison from > to <.

After the line printf( "Do you want to average a student's grade? (y/n)\n "); you need to add a big if statement to test if 'y' was entered, something like this:

char answer;
printf( "Do you want to average a student's grade? (y/n)\n ");
scanf("%c", &answer);
if( answer == 'Y' )
{
   // put all existing code here

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Which part(s) of the assignment do you want help with? I see you haven't yet added code to sort the array. There are a lot of sorting algorithms, the simplest one top code is the bubble sort.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

As usual, AD humbles us all! :-)

Nope -- that honor goes to Mike.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Can't help you until you post the code.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I see Chevy has a diesel car, I don't know anything about it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

all on lovely cheap diesel

Not in USA you wouldn't -- diesel here is more expensive then unleaded gas. My Prius gets about 52 MPG, and on occasion (such as going down hill) it gets in excess of 100 MPG. It has a gauge that indicates current fuel consumption. I know of no US made autos that run on diesel, just pickups, large trucks, farm equipment, and heavy earth moving equipment. But I have never seen anyone drive a bulldozer down the freeway :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Interesting, but this discuscussion is going way beyond the scope of the original question, which is how static variables are initialized with constant values.

I produced an assembly listing of the program that contains get_first_value() function, VS 2012 optimized out that function entirely and inlined the code. In this case, as expected, the static variable is not initialized with anything. The static variable is just simply set to the value of the parameter each time the function is called making it impossible to determine the initial value of x before the function is first called without checking the assembly code. Just like any other ordinary local variable the initial value of x is undefined.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

static variables are initialized on the first pass through the function they are local to. T

No they are not -- static POD variables are initialized just like all other global POD variables. I posted assembly code to prove it. It would be ridiculous and unnecessarily time consuming to put if statements inside functions to initialize static variables the first time the function is entered.

The fact that they are global memory doesn't change the fact that the scope of the variable is local to the function,

I said nothing about the scope of the variables. That is the ONLY difference between static local variables and global variables.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is "argument"? It has to be a string literal, can not be the name of a variable. And did you include tchar.h? For edxample

#include <Windows.h>
#include <tchar.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << _T("Hello World\n");
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

about that ellipsis, what am I supposed to put there? The filename and… ( ,"r")?

The same thing you posted, I was just too lazy to retype it :)

And could I see what the results would be?

Yes, just code it up and test it on your computer.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

size_t j = pon - &string1[0];

you forgot to make string1[0] a pointer.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And it prints out 42 and 42, as expected. And, of course, the above code would be impossible if local static variables were "just like all other global variables".

No -- that would be impossible if the static variable were on the stack just like any other non-static local variable. Static variables actually reside in global data segment, Visual Studio puts them in COMDAT data segment (lines 15-17 of the assembly code).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The only way to know one way or the other is to look at the assembly code that the compiler produces. Below is from Visual Studio 2012. Nowhere in there does it show the static variable being initialized when foo() is entered the first time, because the initialization is performed when the variable is declared -- see the line ?x@?1??foo@@YAAAHXZ@4HA DD 0aH ;foo'::2'::x. The 0aH is initializing the variable

This is only true for static variables of POD (Plain Old Data) types.

The program is compiled in Release mode to prevent generation of debug info.

; Listing generated by Microsoft (R) Optimizing Compiler Version 17.00.60610.1 

    TITLE   C:\dvlp\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp
    .686P
    .XMM
    include listing.inc
    .model  flat

INCLUDELIB OLDNAMES

PUBLIC  ??_C@_0BA@NLKDPCHC@Hello?5World?5?$CFd?6?$AA@   ; `string'
EXTRN   __imp__printf:PROC
EXTRN   @__security_check_cookie@4:PROC
;   COMDAT ?x@?1??foo@@YAAAHXZ@4HA
_DATA   SEGMENT
?x@?1??foo@@YAAAHXZ@4HA DD 0aH              ; `foo'::`2'::x
_DATA   ENDS
;   COMDAT ??_C@_0BA@NLKDPCHC@Hello?5World?5?$CFd?6?$AA@
CONST   SEGMENT
??_C@_0BA@NLKDPCHC@Hello?5World?5?$CFd?6?$AA@ DB 'Hello World %d', 0aH, 00H ; `string'
CONST   ENDS
PUBLIC  _main
PUBLIC  ?foo@@YAAAHXZ                   ; foo
; Function compile flags: /Ogtp
; File c:\dvlp\consoleapplication5\consoleapplication5\consoleapplication5.cpp
;   COMDAT ?foo@@YAAAHXZ
_TEXT   SEGMENT
?foo@@YAAAHXZ PROC                  ; foo, COMDAT

; 8    :    static int x = 10;
; 9    :    printf("Hello World %d\n", x);

    push    DWORD PTR ?x@?1??foo@@YAAAHXZ@4HA
    push    OFFSET ??_C@_0BA@NLKDPCHC@Hello?5World?5?$CFd?6?$AA@
    call    DWORD PTR __imp__printf
    add esp, 8

; 10   :    return x;

    mov eax, OFFSET ?x@?1??foo@@YAAAHXZ@4HA

; 11   : }

    ret 0
?foo@@YAAAHXZ ENDP                  ; foo
_TEXT   ENDS
; Function compile flags: /Ogtp
; File c:\dvlp\consoleapplication5\consoleapplication5\consoleapplication5.cpp
;   COMDAT _main
_TEXT   SEGMENT
_main   PROC                        ; COMDAT

; 9    :    printf("Hello World %d\n", x); …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

windows.h should be sufficient. There are a lot of other macros in tchar.h. My preference is to (almost) never use L because L is not portable between UNICODE and non-UNICODE programs, while the _T() and _TEXT() macros are. If you use _T() you can turn UNICODE on and off without changing the program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want to just search for string2 as a whole word then after getting the pointer from strstr() check the character just before the pointer and the character just following the end of the word in string2. For example is string2 = "Hello" then check the character immediately before the 'H' and the character after the 'o'. Hint: you do not need a loop to do these checks.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can't. Those are macros that work only on string literals -- text within quotes such as "Hello World". You have to call a conversion function to convert char* to wchar_t*. Study the link I gave you.