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

P

problem.

#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?
 
J

jacob navia

Richard said:
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.
 
R

Richard Bos

problem. said:
#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
 
P

problem.

Oh i'm soory, i know why this source code error!

i wrote printf -> Printf

thank you
 
C

CBFalconer

problem. said:
.... snip code ...

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

why?

No, the message is "Printf is undeclared". Which is correct.
 
R

Richard

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".
 
B

Ben Bacarisse

problem. said:
#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;
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?
 
K

Kenneth Brody

problem. said:
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:[email protected]>
 
L

lovecreatesbea...

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.
 
C

Chris Torek

[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]
[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".
 
P

Peter 'Shaggy' Haywood

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.

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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top