Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > writing program from a book

Reply
Thread Tools

writing program from a book

 
 
hpy_awad@yahoo.com
Guest
Posts: n/a
 
      01-28-2004
I wrote that program from a book that my compile that program
correctly under C++ compiler but I got those errors for compiling
under unix !!

Errors-
--------
part10_iti_r01_ch10_verbo_menu_driven_programs.c: In function `main':
part10_iti_r01_ch10_verbo_menu_driven_programs.c:6 : warning:
declaration of `argc' shadows a parameter
part10_iti_r01_ch10_verbo_menu_driven_programs.c:7 : warning:
declaration of `argv' shadows a parameter
part10_iti_r01_ch10_verbo_menu_driven_programs.c:2 2: syntax error
before '{' token

Program-
--------
#include <stdio.h>
//part10_iti_r01_ch10_verbo_menu_driven_programs.c

main (argc,argv)
{
int argc;
char *argv[50];
{
int option;
do
{
//Display menu
display_menu();

//Initite appropriate program or exit
if (option!=7) //exit
call_program(option,argv);
}
while (option!=7);
}
display_menu()
{
system("clear");
printf ("\n TV Rental system");
printf ("\n ----------------");
printf("\n\n 1 Set up new customer");
printf("\n\n 2 Change existing customer record");
printf("\n\n 3 Add new customer record");
printf("\n\n 4 Delete customer record");
printf("\n\n 5 Print customer bills");
printf("\n\n 6 display a customer record");
printf("\n\n 7 Exit");
}


user_selection()
{
int opt;
printf("\n\n Enter required option number (1-7) ");
scanf("%d",&opt);
return(opt);
}

call_program(opt,argv)
int opt;
char *argv[];
{
switch(opt)
{
case 1: spawnvp(0,"a.obj",argv);
delay();
break;

defaultrintf("\nError");
delay();
}
}


delay()
{
int i;
for (i=0;i<=20000;++i);
}
 
Reply With Quote
 
 
 
 
Sean Kenwrick
Guest
Posts: n/a
 
      01-28-2004

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I wrote that program from a book that my compile that program
> correctly under C++ compiler but I got those errors for compiling
> under unix !!
>
> Errors-
> --------
> part10_iti_r01_ch10_verbo_menu_driven_programs.c: In function `main':
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:6 : warning:
> declaration of `argc' shadows a parameter
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:7 : warning:
> declaration of `argv' shadows a parameter
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:2 2: syntax error
> before '{' token
>
> Program-
> --------
> #include <stdio.h>
> file://part10_iti_r01_ch10_verbo_menu_driven_programs.c
>
> main (argc,argv)
> {
> int argc;
> char *argv[50];
> {


What language is this?? I can't imagine that this would compile under C++
or C. I think that you are trying to use old style function declarations
of the type:

foo(p1,p1)
int p1;
int p2;
{

...
}

You should change these to the newer style function declarations:

int foo(int p1,int p2)
{
...
}


So this would become:

int main(int argc, char ** argv)
{
int option;
....

}

> int option;
> do
> {
> file://Display menu
> display_menu();
>
> file://Initite appropriate program or exit
> if (option!=7) file://exit
> call_program(option,argv);
> }
> while (option!=7);
> }
> display_menu()
> {
> system("clear");
> printf ("\n TV Rental system");
> printf ("\n ----------------");
> printf("\n\n 1 Set up new customer");
> printf("\n\n 2 Change existing customer record");
> printf("\n\n 3 Add new customer record");
> printf("\n\n 4 Delete customer record");
> printf("\n\n 5 Print customer bills");
> printf("\n\n 6 display a customer record");
> printf("\n\n 7 Exit");
> }
>
>
> user_selection()
> {
> int opt;
> printf("\n\n Enter required option number (1-7) ");
> scanf("%d",&opt);
> return(opt);
> }
>
> call_program(opt,argv)
> int opt;
> char *argv[];
> {
> switch(opt)
> {
> case 1: spawnvp(0,"a.obj",argv);
> delay();
> break;
>
> defaultrintf("\nError");
> delay();
> }
> }
>
>
> delay()
> {
> int i;
> for (i=0;i<=20000;++i);
> }


Same for call_program:

int call_program(int opt,char ** argv)
{
...
}

Sean




 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=
Guest
Posts: n/a
 
      01-28-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) ((E-Mail Removed)) writes:

> I wrote that program from a book that my compile that program
> correctly under C++ compiler but I got those errors for compiling
> under unix !!
>
> Errors-
> --------
> part10_iti_r01_ch10_verbo_menu_driven_programs.c: In function `main':
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:6 : warning:
> declaration of `argc' shadows a parameter
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:7 : warning:
> declaration of `argv' shadows a parameter
> part10_iti_r01_ch10_verbo_menu_driven_programs.c:2 2: syntax error
> before '{' token
>
> Program-
> --------
> #include <stdio.h>
> //part10_iti_r01_ch10_verbo_menu_driven_programs.c
>
> main (argc,argv)
> {


Get rid of that brace...

> int argc;
> char *argv[50];


.... and drop the old K&R style.

> {
> int option;
> do
> {
> //Display menu
> display_menu();
>
> //Initite appropriate program or exit
> if (option!=7) //exit
> call_program(option,argv);
> }
> while (option!=7);
> }
> display_menu()
> {
> system("clear");
> printf ("\n TV Rental system");
> printf ("\n ----------------");
> printf("\n\n 1 Set up new customer");
> printf("\n\n 2 Change existing customer record");
> printf("\n\n 3 Add new customer record");
> printf("\n\n 4 Delete customer record");
> printf("\n\n 5 Print customer bills");
> printf("\n\n 6 display a customer record");
> printf("\n\n 7 Exit");
> }
>
> user_selection()
> {
> int opt;
> printf("\n\n Enter required option number (1-7) ");
> scanf("%d",&opt);
> return(opt);
> }
>
> call_program(opt,argv)
> int opt;
> char *argv[];
> {
> switch(opt)
> {
> case 1: spawnvp(0,"a.obj",argv);


What does spawnvp do?

> delay();
> break;
>
> defaultrintf("\nError");
> delay();
> }
> }
>
> delay()
> {
> int i;
> for (i=0;i<=20000;++i);
> }


How ancient is this. That loop takes no time at all to run here.

--
Måns Rullgård
(E-Mail Removed)
 
Reply With Quote
 
Alan Coopersmith
Guest
Posts: n/a
 
      01-28-2004
(E-Mail Removed) ((E-Mail Removed)) writes in comp.unix.solaris:
|I wrote that program from a book that my compile that program
|correctly under C++ compiler but I got those errors for compiling
|under unix !!
|
|Errors-
|--------
|part10_iti_r01_ch10_verbo_menu_driven_programs.c: In function `main':
|part10_iti_r01_ch10_verbo_menu_driven_programs.c: 6: warning:
|declaration of `argc' shadows a parameter
|part10_iti_r01_ch10_verbo_menu_driven_programs.c: 7: warning:
|declaration of `argv' shadows a parameter
|part10_iti_r01_ch10_verbo_menu_driven_programs.c: 22: syntax error
|before '{' token
|main (argc,argv)
|{
|int argc;
|char *argv[50];
|{

This should be
int main (int argc, char *argv[])
{

You've got too many {'s along with obsolete syntax. (As has been
mentioned before, it appears whatever book you're using should be
burned.)

--
__________________________________________________ ______________________
Alan Coopersmith (E-Mail Removed)
http://www.CSUA.Berkeley.EDU/~alanc/ aka: (E-Mail Removed)
Working for, but definitely not speaking for, Sun Microsystems, Inc.
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      01-28-2004
In comp.unix.programmer Måns Rullgård <(E-Mail Removed)> wrote:
> (E-Mail Removed) ((E-Mail Removed)) writes:


>> I wrote that program from a book that my compile that program
>> correctly under C++ compiler but I got those errors for compiling
>> under unix !!
>>
>> delay()
>> {
>> int i;
>> for (i=0;i<=20000;++i);
>> }


> How ancient is this. That loop takes no time at all to run here.


And probably every self-respecting compiler will optimize out that
loop (i.e. throw it away because there's no result that's ever going
to be used) unless you declare 'i' as volatile. And even then the
time that delay will take is probably absolutely negligible. With
any modern computer the maximum delay you'll be getting is probably
somewhere in the order of a micro-second.

But how you got to compile this program with a C++ compiler is
completely beyond me. And even if you did, how is 'option' ever
to be changed? Shouldn'd there be a call of the user_selection()
function somewhere in main()s loop? And what's the switch in
call_program() good for? Is only the first menu option usable?
Or have you accidentally skipped a few pages in the book while
copying the program? I guess the best you can do is throw that
book away, all you can learn from it is either completely out-
dated or plain wrong.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      01-28-2004
(E-Mail Removed) wrote:

> I wrote that program from a book that my compile that program
> correctly under C++ compiler


Somehow, I doubt that.

> but I got those errors for compiling
> under unix !!


The code you transcribed here is /so/ wrong.

You are compiling bad K&R (pre-ANSI) C with ANSI C99 / C++ comments using a
C++ compiler, and expecting it to compile properly and execute.

1) This is not C++, so stop using a C++ compiler on it
2) This is not C99, so lose the C++style comments
3) There are logic errors in your code. Fix them
(for instance, you never assign a value to your 'options' variable,
you test the 'options' variable for a value that can never occur,
etc.)
4) You use platform-specific functions; either lose them or submit your
corrected code to a forum that has knowledge of them.


--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

 
Reply With Quote
 
Rich Teer
Guest
Posts: n/a
 
      01-28-2004
On Wed, 28 Jan 2004, Alan Coopersmith wrote:

> You've got too many {'s along with obsolete syntax. (As has been
> mentioned before, it appears whatever book you're using should be
> burned.)


Agreed. To the OP: Don't bother with "C for Dummies" either,
or any other book that thinks that "void main (...)" is valid.

I'm reading in this in comp.unix.solaris: I humbly submit that
my forthcoming book, Solaris Systems Prorgamming, should be on
your shopping list (in addition to a good C tutorial - my book
assumes some C knowledge).

One more tip: try to ask your questions in a more focussed group
of newsgroups.

--
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-28-2004
Lew Pitcher wrote:

> You are compiling bad K&R (pre-ANSI) C with ANSI C99 / C++ comments using a
> C++ compiler, and expecting it to compile properly and execute.



It's not even old-style C, because he has an opening brace before the
argc and argv declarations, which then turns them into local variables
(hence the error message).

The OP needs to go back to square one, get a good book and start again
with a few simple programs.




Brian Rodenborn
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-28-2004
(E-Mail Removed) wrote:

> I wrote that program from a book that my compile that program
> correctly under C++ compiler


No, it didn't. Some other program did, probably one without the trivial
error below.

> but I got those errors for compiling
> under unix !!


Unix is irrelevant. Your code is broken.

> #include <stdio.h>
> //part10_iti_r01_ch10_verbo_menu_driven_programs.c
>
> main (argc,argv)
> {
> int argc;
> char *argv[50];
> {


You are incorrectly using an outmoded style of specifying the arguments. If
you insist on using this style, it should be

int main(argc, argv)
int argc;
char *argv[];
{

Notice that the extra brace '{' is missing and the bogus '50' is missing.

The modern way to do this, if you consider the last 15 years to to be the
modern era, is
int main(int argc, char *argv[])
{

Since it is normal etiquette to check the FAQs and follow the traffic on a
newsgroup before posting, I suspect that you really knew these things. It
is impossible to read the FAQs for and follow all of comp.lang.c,
comp.unix.solaris, comp.unix.programmer, gnu.g++.help, and gnu.gcc.help
without seeing these simple things done correctly.


--
Martin Ambuhl
 
Reply With Quote
 
Dragan Cvetkovic
Guest
Posts: n/a
 
      01-28-2004
Martin Ambuhl <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>
>> I wrote that program from a book that my compile that program
>> correctly under C++ compiler


[snip]

> You are incorrectly using an outmoded style of specifying the arguments. If
> you insist on using this style, it should be
>
> int main(argc, argv)
> int argc;
> char *argv[];
> {
>


Actually, even that one won't work with C++ (remember, OP is using a C++
compiler).

E.g. the following program (saved in /tmp/p.cc file)

int main(argc, argv)
int argc;
char *argv[];
{
return 0;
}

produces the following errors:
% g++ -c /tmp/p.cc
/tmp/p.cc:1: error: `argc' was not declared in this scope
/tmp/p.cc:1: error: `argv' was not declared in this scope
/tmp/p.cc:2: error: initializer list being treated as compound expression
/tmp/p.cc:2: error: syntax error before `int'
/tmp/p.cc:3: error: storage size of `argv' isn't known
/tmp/p.cc:3: error: storage size of `argv' isn't known
/tmp/p.cc:4: error: parse error before `{' token

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a book on writing ASP.NET Controls with VB.NET Nathan Sokalski ASP .Net 2 02-19-2006 04:44 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Why Im Writing My Book - OT in a way Tracker Computer Security 45 10-28-2003 04:53 AM
Writing solid code book post400 Python 40 09-16-2003 11:05 AM
Re: Writing solid code book Jeremy Dillworth Python 0 09-03-2003 01:03 PM



Advertisments