C/C++ Thread

Cromewell

Administrator
Staff member
Sure, it's basically what you already had with a few modifications.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int intCheck(char []);

int intCheck(char value[])
{
    int length = strlen(value);
    int count, num;
    
    //printf("length: %d, value: %s\n", length, value);
    
    for (count = 0; count < length; count++)
    {
        //printf("value[count]: %c, count: %d\n",value[count], count);
        if (value[count] < 48 || value[count] > 57)
        { return 0; }
    }

    num = atoi(value);
    
    return num;
}

int main(int argc, char *argv[])
{
    int count;
    char num[256];
    int total = 1, hold = 0, add;
    printf("Enter a value: ");
    scanf("%s", &num);
    
    while (intCheck(num) == 0)
    {
          printf("Invalid input. Please enter a whole number: ");
          scanf("%s", &num);
    }
    
    system("pause");
}
 

Troncoso

VIP Member
Dude. Thanks so much for your help. I'm still comparing our 2 functions to find what exactly you did differently, but after some of my own tweaks, it works perfectly. Now that's one thing I don't have to worry about for my next several projects. thanks again.
 

Troncoso

VIP Member
What compiler/OS are you using? I've got the function on a separate file but in dev-c++ I just can't get them to compile and link. Though in Ubuntu, gcc does it just fine.
 

G3N1US!

Member
speaking of C++ in general, does anyone have problems with Visual Studio 2010? For some reason, mine crashes whenever i debug...So i just go back to using Dev-C++, but i would prefer using VS if it didn't crash so often.
 

G3N1US!

Member
this is the current mini-project i'm making, but i can't seem to figure out how to get one of the buttons to open google chrome or any program.

code:
Code:
#include <windows.h>



/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
   wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); 

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Create a Button",       /* Title Text */
           WS_SYSMENU, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        
        case WM_CREATE:
             
             CreateWindow(TEXT("button"), TEXT("Click to Continue...."), 
                WS_VISIBLE | WS_CHILD, 
                100,100,300,50,
                hwnd, (HMENU) 1, NULL, NULL
             );
             CreateWindow(TEXT("button"), TEXT("Click to Stop...."),
                WS_VISIBLE | WS_CHILD,
                100,150,300,50,
                hwnd, (HMENU) 3, NULL, NULL
             );
             CreateWindow(TEXT("button"), TEXT("Click To Open Google Chrome"),
                WS_VISIBLE | WS_CHILD,
                100,50,300,50,
                hwnd, (HMENU) 2, NULL, NULL
             );
             
        break;
        
        case WM_COMMAND:
             
             if (LOWORD(wParam) == 1){
                MessageBox(hwnd, "Test Two", "Program1", MB_OK | MB_ICONINFORMATION);
             }
             if (LOWORD(wParam) ==2) {
                MessageBox(hwnd, "Test One" , "Program1", MB_OK | MB_ICONINFORMATION);
             }               
             if (LOWORD(wParam) ==3) {        
                MessageBox(hwnd, "Test Three", "Program1", MB_OK | MB_ICONINFORMATION);                         
             }

        break;
        
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
 
Last edited:

Cromewell

Administrator
Staff member
I'm using dev c++ on windows. To get them to link you tend to have to include the cpp/h file with your seperate code in it.

I haven't used VS2010, can't help you there.

G3n1us!, I can't get your code to build. I'm assuming you're using VS for it?
 

Cromewell

Administrator
Staff member
Strange, my linker is erroring out on this line: wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
saying undefined reference to GetStockObject@4. The really strange thing is the type ahead recognizes the function and tells me what parameters it expects.

I've changed it to wincl.hbrBackground = (HBRUSH) (COLOR_BACKGROUND); to make it build.

I think the code you want to use to launch a program is CreateProcess (http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx)
 

Troncoso

VIP Member
I included a separate header file but it still wouldn't compile. Then again, I'm using the newest beta version of dev so that may be way there are problems.

G3n1US, I recommend going with visual studio 2008. Have had no provlems with it and you can get the full version free at dreamspark if you are a student
 

Cromewell

Administrator
Staff member
I included a separate header file but it still wouldn't compile. Then again, I'm using the newest beta version of dev so that may be way there are problems.

G3n1US, I recommend going with visual studio 2008. Have had no provlems with it and you can get the full version free at dreamspark if you are a student

I'm using 4.9.9.2, I think that's the newest version as well.

You can also get Visual xxx Express Edition from MSDN for free as long as it's not for building commercial software.
 

G3N1US!

Member
@Troncoso
I cant seem to find VS 2008 though, i looked everywhere.
I do however, have VS 2010 Express, and like i said before, it keeps crashing whenever i compile/debug
 

mihir

VIP Member
Finally we have this thread.NICE :D


Thanks Cromwell.


I am an engineering student and in the first semester we were taught ANSI C and C++.
Anyways I use Python for Object Oriented Programming.

We used the gcc compiler in any GNU/Linux OS.
The gcc is a really advanced compiler if anyone has ubuntu installed they can try it for themselves you actually get a proper feel of coding if you use the VI or VIM text editor in linux.


Anyways no use of iostream in gcc since all the functions of iostream and fstream are automatically included in that.

And for C++ graphics purposes the only comiler you can use is the turbo C++.
which is a really backward and sucky compiler.
but still graphics.h
is fun to explore if anyone wants i can tell a few functions and programs on it.


PS as for the books.

I would recomend the kernighan and ritchie c book its the best book ever and also written by the creators of c language.
C_book_by_Kernighan_and_Ritchie.gif



and for data structures I would go with the

Horowitz and Sahani The Fundamentals of Data Structures
 
Last edited:

Troncoso

VIP Member
^^^I very much enjoy coding in ubuntu, though I use gedit to write my code. It feels apropriate and it compiles faster as well.
 

mihir

VIP Member
^^^I very much enjoy coding in ubuntu, though I use gedit to write my code. It feels apropriate and it compiles faster as well.

It doesnt matter what text editor you use the code will compile at the same speed.Because gedit is just a text editor and even vim is just a text editor(a more advanced one but still).

Try vim. You wont be able to fully exploit its power without a tutorial but once you get used to it everyother text editor will be useless and obsolete for you.

sudo apt-get install vim

Even shell programming is fun.

I have a few really good programming problems from my class if you want i can give them to you or post it on this thread and we can discuss the alternative solutions.
 

Troncoso

VIP Member
programming problems like simply "make a program that does this"? That'd be cool. I'm actually doing that now. with a couple problems. I've got a factorial generator, a fibonacci series generator, a simple calulator, which I have a question about:

It technically works write, but because of the null terminator that writes to the scanf90 buffer, I always get a minor undesired result. Is there a way to clear the buffer before continuing with the program?


and also, yeah I need tutorials. I tried vi and I can't stand it at the moment.
 
Top