469,917 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Hi guys~ i wonder that why this simple code error!!

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

int a, b;
float result = 0.0f;
char symbol = '\0';
int loop = 0;

printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
scanf("%f", &a);
printf("Please, Enter second Number : \n");
scanf("%f", &b);
printf("Please, Select the menu (+, -, *, /, % .\n");
scanf("%c", &symbol);
printf("*********************************\n");

do{
if (symbol == '+')
result = a + b;
else if (symbol == '*')
result = a * b;
else if (symbol == '/')
if (b == 0)
{
printf("Error!!, You reinsert second value\n");
loop = 1;
}
result = a/b;
if (symbol == '%')
if (b == 0)
{
printf("Error!!, You reinsert second
value\n");
loop = 1;
}
result = a % b;
}while(loop);
printf("Result : %d %c %d = %f\n", a, symbol, b,
result);
system("PAUSE");
return 0;
}

the error message is printf is undeclared.(first use this function)

why?

Sep 19 '07 #1
13 4498
problem. wrote:
Printf("Please, Enter first Number : \n");
It is really undeclared ))
--
s/.../.gotovchits/g for email.
Sep 19 '07 #2
On Sep 19, 1:45 pm, "problem." <kjy1...@icu.ac.krwrote:
Printf("Please, Enter first Number : \n");
P in caps. C is case-sensitive. I think this is causing the error.

Regards,
Prayag Narula
Sep 19 '07 #3
"problem." <kj*****@icu.ac.krwrote:
#include <stdio.h>
printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
the error message is printf is undeclared.(first use this function)
No, it isn't. If you cannot be bothered to read your error messages with
attention, you are not fit to be a programmer.

Richard
Sep 19 '07 #4
Richard Bos wrote:
"problem." <kj*****@icu.ac.krwrote:
>#include <stdio.h>
> printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
>the error message is printf is undeclared.(first use this function)

No, it isn't. If you cannot be bothered to read your error messages with
attention, you are not fit to be a programmer.

Richard
And you are fit for WHAT Mr?

The only thing you do here is attack people
because they try to learn or ask questions?

Obviously you were born with C skills...

You never learned it, you always knew everything.

Sep 19 '07 #5
Oh i'm soory, i know why this source code error!

i wrote printf -Printf

thank you

Sep 19 '07 #6
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"problem." <kj*****@icu.ac.krwrote:
>#include <stdio.h>
> printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
>the error message is printf is undeclared.(first use this function)

No, it isn't. If you cannot be bothered to read your error messages with
attention, you are not fit to be a programmer.

Richard
*Blink*. maybe you should consider giving up helping people. Did you
never miss something so obvious? How many times over the years have I
seen people up all night over a mis-capitalized word or a 0 instead of a
O or an "l" instead of a "1".

Sep 19 '07 #7
"problem." <kj*****@icu.ac.krwrites:
#include <stdio.h>
#include <stdlib.h>

int main(void)
Way to go! Give yourself a cookie! You would need to have been a
long-time reader of comp.lang.c to see why seeing a correct
declaration for main should make me happy.
{

int a, b;
float result = 0.0f;
char symbol = '\0';
int loop = 0;
<snip>
scanf("%f", &a);
a format of %f requires a pointer to a float. You need %d to scan an
integer.
printf("Please, Enter second Number : \n");
scanf("%f", &b);
Ditto. Maybe, since the result is a float, you intended a and b to be
float too?

--
Ben.
Sep 19 '07 #8
"problem." wrote:
>
Oh i'm soory, i know why this source code error!

i wrote printf -Printf
And the error message probably included the capital P as well.
This is an excellent example of why cut-and-paste is much more
accurate in tracking down such errors than retyping. (At first,
I didn't notice the capital P in the source, so I was wondering
if your stdio.h was broken.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Sep 19 '07 #9
On Sep 19, 11:49 pm, Richard <rgr...@gmail.comwrote:
How many times over the years have I
seen people up all night over a mis-capitalized word or a 0 instead of a
O or an "l" instead of a "1".
The are clearly distinguished under Courier 10 point font, if this
font is obtained on your system.

Sep 19 '07 #10
In article <11**********************@y27g2000pre.googlegroups .com>
problem. <kj*****@icu.ac.krwrote:
[snippage]

Others have noted your immediate problem (that Printf and printf are
different symbols, at least on most C89 implementations). One
followup also noted that scanf's "%f" directive requires a pointer
to a "float" variable, and you have:
int a, b;
so:
scanf("%f", &a);
is wrong. You must either make a and b both "float" (which then
requires more changes) or use scanf's "%d" directive here.

There are several even-worse problems lurking, however:

- scanf() is *extremely* difficult to use correctly. The
best approach for most C programmers, in my opinion, is
never to use scanf() at all. Instead, use something to
read a line of input (such as fgets(), or Chuck Falconer's
ggets(), or any number of similar functions -- just not
plain gets()), then use sscanf() on the resulting buffer.

- In any case, scanf() (or sscanf(), after switching to a much
safer version) has a return value, which you *must* test
to see whether the scanf() actually did anything.
printf("Please, Enter second Number : \n");
scanf("%f", &b);
printf("Please, Select the menu (+, -, *, /, % .\n");
scanf("%c", &symbol);
Here is the worst problem of all. Even if you change the two
scanf()s to use "%d", and check that they both return 1, this last
scanf() will read the next available character from the input
stream. Because you will have pressed the ENTER or RETURN key to
enter a number, the "next available character" will almost certainly
be a newline ('\n') -- so this scanf() will return 1, but will set
the variable "symbol" to '\n'.

The reason is that scanf()'s "%c" directive does *not* skip leading
white space. The "%f" and "%d" directives (and a number of similar
other ones) mean:

- First, consume (skip over and "eat up") any and all white space
(characters for which isspace() returns true).

- Then, read and convert a number ("int" for %d, "float" for
%f, "double" for %lf, and so on).

- If the input at this point is not syntactically valid, stop
with a matching failure, so that the scanf call returns a
smaller number. (For instance, scanf() might return 0 if
the input stream yields " \n\t\t\v\foops".)

- If the conversion overflows, do anything you feel like doing,
possibly including aborting the program run ("you" means
"the implementation" here).

- Otherwise, all went well; store the converted number and
increase the return value (unless assignment is suppressed
with the "*" modifier).

Note that this does *not* consume trailing white-space.

Two directives -- the "%c" and "%[" directives -- omit the "consume
leading white space" sequence. This means they will "see" any
*trailing* white space left behind by any earlier directives.

Using scanf() is thus like planting various time bombs. If you
use fgets() (or similar) to read entire input lines, then apply
sscanf() to those lines, these "time bombs" have much more limited
potential action, because the input line gets handled separately,
putting the input characters into a buffer where you can inspect
them, modify them, discard them, or do whatever you like with them,
*before* letting the underlying scanf engine see them. Any characters
the scanf engine ignores are still there in your buffer, before
and after the call to scanf, and are no longer in the input stream.

[variable "loop" is initially zero here]
do{
[various things where errors set "loop" to 1]
}while(loop);
If you ever get an error, this sets the variable "loop" to 1, so
that the do-loop repeats. The repeated do-loop starts with the
variable "loop" set to 1, and nothing will ever set it to zero,
so if the do-loop repeats, it will never terminate.

The simplest fix is to move the initialization of the variable
"loop" to the top of the loop:

do {
loop = 0;
... various things that may do loop=1 ...
} while (loop);

although there are other solutions that some people might find
"better".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40░39.22'N, 111░50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sep 19 '07 #11
"problem." wrote:
>
.... snip code ...
>
the error message is printf is undeclared.(first use this function)

why?
No, the message is "Printf is undeclared". Which is correct.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 20 '07 #12
Groovy hepcat problem. was jivin' in comp.lang.c on Wed, 19 Sep 2007
9:45 pm. It's a cool scene! Dig it.
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

int a, b;
float result = 0.0f;
char symbol = '\0';
int loop = 0;

printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
scanf("%f", &a);
printf("Please, Enter second Number : \n");
scanf("%f", &b);
printf("Please, Select the menu (+, -, *, /, % .\n");
scanf("%c", &symbol);
printf("*********************************\n");

do{
if (symbol == '+')
result = a + b;
else if (symbol == '*')
result = a * b;
else if (symbol == '/')
if (b == 0)
{
printf("Error!!, You reinsert second value\n");
loop = 1;
}
result = a/b;
if (symbol == '%')
if (b == 0)
{
printf("Error!!, You reinsert second
value\n");
loop = 1;
}
result = a % b;
}while(loop);
printf("Result : %d %c %d = %f\n", a, symbol, b,
result);
system("PAUSE");
return 0;
}

the error message is printf is undeclared.(first use this function)
That's not what it says at all, I'll bet.
why?
Because your code is broken. Here's what I get:

gcc -ansi -pedantic -Wall -o testing testing.c
testing.c: In function `main':
testing.c:14: warning: implicit declaration of function `Printf'
testing.c:15: warning: float format, different type arg (arg 2)
testing.c:17: warning: float format, different type arg (arg 2)
testing.c:18: warning: unknown conversion type character 0xa in format
testing.c:37: error: missing terminating " character
testing.c:38: error: stray '\' in program
testing.c:38: error: `value' undeclared (first use in this function)
testing.c:38: error: (Each undeclared identifier is reported only once
testing.c:38: error: for each function it appears in.)
testing.c:38: error: syntax error before "n"
testing.c:38: error: missing terminating " character

Other people have told you about your Printf() and scanf() errors, as
indicated in the above diagnostic output, and even one not indicated
there. But they seem to have missed a couple of other problems.
First, you have a printf() format string with an embedded '%'. This
character introduces a conversion specifier. But you have an invalid
conversion specifier composed of a '%' followed by a space. To
represent a literal '%' in a printf() format string, use two '%'
characters. For example:

printf("%%\n");

Next, you have a line wrapped within a string literal. This is causing
the remaining cascade of diagnostic messages.
I must also add that your code is hard to understand. Your indentation
is all over the place, making it difficult to follow. This has also led
to the line wrap problem mentioned above. You should try to format any
code you post here so that lines are no more than about 70 characters
long. Indent consistently. Long string literals can be divided, if need
be, either by using a line concatenation sequence (a backslash at the
end of a line) or (better still) by taking advantage of the fact that
adjacent string literals are automatically concatenated. For example,
this:

printf("A string "
"literal.\n");

is interpreted as this:

printf("A string literal.\n");

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
Sep 24 '07 #13
In data Wed, 19 Sep 2007 15:56:44 +0400, Ivan Gotovchits scrisse:

i like your surname
>problem. wrote:
>Printf("Please, Enter first Number : \n");
It is really undeclared ))
Oct 16 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Polar | last post: by
3 posts views Thread by Davݭ ١risson | last post: by
10 posts views Thread by Harsh_forC | last post: by
10 posts views Thread by Sourcerer | last post: by
30 posts views Thread by galiorenye | last post: by
8 posts views Thread by autodidact | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.