Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 11th, 2005, 3:23 PM   #1
CrAzY_J
Newbie
 
Join Date: May 2005
Posts: 29
Rep Power: 0 CrAzY_J is on a distinguished road
Send a message via MSN to CrAzY_J
A small code help

#include    <stdio.h>
#include    <string.h>

#define     mxstr     200
main(){
       char lighter[mxstr];
       strcpy(lighter,"TEST");
       printf("%s",lighter);
       system("PAUSE");
}

Ok i have no idea why that wont work.
it's suppose to write "TEST" on the screen but all i see is "Press any key to continue..." >_>

It's a C code and i use DevC++ on WindowsXP pro
just in case you needed that :p
CrAzY_J is offline   Reply With Quote
Old Jun 11th, 2005, 3:35 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
This works on my machine. It prints:

"TESTPress any key to continue."
uman is offline   Reply With Quote
Old Jun 11th, 2005, 3:37 PM   #3
CrAzY_J
Newbie
 
Join Date: May 2005
Posts: 29
Rep Power: 0 CrAzY_J is on a distinguished road
Send a message via MSN to CrAzY_J
Quote:
Originally Posted by uman
This works on my machine. It prints:

"TESTPress any key to continue."
Yeah my compiler was acting weird.
works now ^_^
thanks
CrAzY_J is offline   Reply With Quote
Old Jun 11th, 2005, 6:15 PM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
I'm glad to hear it.
uman is offline   Reply With Quote
Old Jun 11th, 2005, 8:14 PM   #5
Wraith Daquell
Programmer
 
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0 Wraith Daquell is on a distinguished road
*steps up onto soap-box*
#define should not be used, because a typed constant is better and easier to debug.
200? It's nice to have a buffer, I know, but try using malloc with the actual string size instead; this will save memory (and we all know that 200 chars will really make a new machine creep )
*steps off of soap-box*

Sorry. I'm feeling giddy tonight.
__________________
The meek will inherit the earth.

-WDaquell
Wraith Daquell is offline   Reply With Quote
Old Jun 11th, 2005, 8:44 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
While I prefer the use of a typed constant myself, it isn't always the prefered choice. Using #define will cause a textual substitution that will allow a dimension to be used for entities of differing types. I probably also wouldn't use malloc for a small buffer. If I KNEW the size (e.g. "TEST"), I would probably use something like

char myString [] = "TEST";

Whether to define a buffer on the stack, as a global, or from the free store, is a design decision and the criteria may vary from case to case. I certainly wouldn't issue a dictum to a newbie on the point without elucidating the various design considerations.

Just my own opinion, of course.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 11th, 2005, 9:47 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by CrAzY_J
Yeah my compiler was acting weird.
Your compiler was not "acting wierd". All that happened is that your output streams were not being flushed before your system(). As the system() call is non-standard, it's hardly surprising that doing things with it won't always give results you expect. One of the things that can happen is that behaviour of your program and how it interacts with the system() call can change over time as it will be affected by other things your machine is doing.

The real fix is to add a line;
[PHP]
fflush(stdout);
[/PHP]
immediately before the system() call.

Out of curiosity, why use a system("PAUSE") at all? It is non-standard, so a simpler solution would be to use (depending on your compiler)
[PHP]
#include <stdio.h>
#include <string.h>
#include <conio.h>

#define mxstr 200
main(){
char lighter[mxstr];
int byte;
strcpy(lighter,"TEST");
printf("%s",lighter);
printf("\n Hit any key to continue ....");
fflush(stdout);
byte = getch();
/* do whatever you want based on what key is hit by the user */
}
[/PHP]
getch() is non-standard, but is supported by several compilers under windows when building console mode applications. On other compilers, it may have a different name and be in a different header file --- but that's true of system() as well.

Also, I agree with Dawei that using typed constants is not always preferred over #define's. I agree that, given a choice, I will rarely use macros because they can cause lots of hard-to-find problems in code. In practice, particularly with older (pre-standard) versions of C, #define's were the only way of doing such things. And there are still lots of old compilers around.

Last edited by grumpy; Jun 12th, 2005 at 1:55 AM. Reason: Fix typos
grumpy is offline   Reply With Quote
Old Jun 11th, 2005, 10:12 PM   #8
Wraith Daquell
Programmer
 
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0 Wraith Daquell is on a distinguished road
DaWei and co.

I was just kidding around. These things seemed to me to be of slight importance, but CrAzY_J, please don't be insulted by me. I'm harmless.
__________________
The meek will inherit the earth.

-WDaquell
Wraith Daquell is offline   Reply With Quote
Old Jun 12th, 2005, 1:00 AM   #9
CrAzY_J
Newbie
 
Join Date: May 2005
Posts: 29
Rep Power: 0 CrAzY_J is on a distinguished road
Send a message via MSN to CrAzY_J
ok...........
well most of it comes from my book (the C programming language edition 4)
the only thing not from there is the system("PAUSE") which i got from devshed.
but ill try to use it like grumpy showed
CrAzY_J is offline   Reply With Quote
Old Jun 12th, 2005, 7:01 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"getch" will cause the program to wait for any keystroke before proceeding (assuming there are no unused characters buffered up). If you're willing to use ENTER, you may use a standard character input mechanism such as "getchar ()" or (for C++) "cin.get ()".

@wraith: My apologies. I, myself, have an obsession; it's trying to guard the world against self-proclaimed high-priest gurus who tell the acolytes to shut up and just do it "because we got it from on high, and we say so."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC