Re: Simple Welding of two small C/C++ Programs, But How??
From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 03/19/05
- Next message: Jeff Schwab: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Previous message: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- In reply to: Basil Fawlty: "Simple Welding of two small C/C++ Programs, But How??"
- Next in thread: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Reply: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 19 Mar 2005 11:00:53 -0800
On Sat, 19 Mar 2005 10:51:20 -0600, "Basil Fawlty"
<Basil_Fawlty_2004@NOSPAMyahoo.com> wrote:
I deleted most of your vertical white space. Unless you are using a
72 inch monitor, it helps to be able to see a couple of dozen lines of
text.
>Hi everyone, I'm a newbie to C/C++ and I need some help with a minor C
There is no C/C++. There is C and there is C++. The similarity in
the names is much stronger than any similarity in fact and only serves
to confuse newbies. Pick one and study it.
>program. My assignment is to take two programs and mesh them together. The
>1st program I wrote compiles and works fine. The 2nd program is to add to
>the 1st program with an if_then_else statement, and it's code is also
>correct as well; I just don't know how to or where to put it into the rest
>of the 1st program to make it work as one. I've tried to modify it but I
Where you put new code depends on what the new code should do. You
haven't told us what the original code did or how the new code should
change that.
>get compiler errors. I look forward to any help some kind soul can provide.
What compiler errors? We are not mind readers.
>The code with both pieces is below. Thanks, Basil Fawlty of Fawlty Towers
>
>
>
>FYI - I'm using Notepad to edit the text and a free C/C++ compiler yanked of
>the Internet via Download.com called Digital Mars.
>
>
>
>#include <stdio.h>
>
>main()
int main(void)
While still tolerated by many compilers, implied return type is no
longer in the language standard.
Your code says the parameters to main are unspecified and could be
anything. Mine says there are no parameters which seems to be exactly
what you intended.
>{
> int age;
I recommend changing your indent style from 16 characters to something
like 4 or 5. Otherwise, you will run off the right margin after just
a few levels of indenting. If notepad does net let you change the
tabs, then use spaces instead.
> float weight;
While there is nothing wrong with using float, most here recommend
using double as a more natural type for the language. For example, a
float passed to printf is converted to double as part of the call and
then converted back in printf.
> char first[15], last[15]; /* 2 char arrays */
While not incorrect, most here recommend one definition per statement,
simply because it becomes easier to find when you have many dozens.
>
> printf("\nWhat is your first name? ");
The absence of a \n at the end of your string allows the possibility
that the text will be buffered and not actually presented to the user.
He would just se the screen waiting for input. If your intent is to
have the input data on the same line as the prompt, then the usual
solution is to add
fflush(stdout);
to force the system to flush the buffer.
> scanf(" %s", first); /* No ampersand on
>character strings */
You should be aware that, when using scanf() this way and gets(), you
cannot prevent the user from entering more characters than your buffer
has room for. If that happens, you have entered the realm of
undefined behavior.
One recommended solution is to use a length modifier for the %s.
Another is to use fgets() instead of scanf().
> printf("What is your last name? ");
> scanf(" %s", last); /* No apersand on character strings */
> printf("How old are you? ");
> scanf(" %d", &age); /* Ampersand required */
> printf("How do you weight? ");
> scanf(" %f", &weight); /* Ampersand required
>*/
While obviously intended as a reminder for yourself, don't you think
only one of these comments would suffice.
> printf("\nHere is the information you entered:\n");
> printf("Name: %s %s\n", first, last);
> printf("Weight: %3.0f\n", weight);
> printf("Age: %d", age);
>
> if (age < 18)
> { printf("You cannnot vote yet\n");
While consistency may be hobgoblin of small minds (as is often
misquoted), it also makes your code easier to read. Everywhere else
in your program, opening braces are on a line by themselves. Why not
here also?
> yrs = 18 - age;
Where was yrs defined? What part of the error message did you not
understand?
> printf("You can vote in d% years.\n",
>yrs);
> }
> else
> {
> printf("You can vote.\n);
> }
>
> return 0; /* Always best to do this */
>}
<<Remove the del for email>>
- Next message: Jeff Schwab: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Previous message: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- In reply to: Basil Fawlty: "Simple Welding of two small C/C++ Programs, But How??"
- Next in thread: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Reply: Basil Fawlty: "Re: Simple Welding of two small C/C++ Programs, But How??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|