473,761 Members | 5,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help. Where is my error?

I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery.
Why does the computer refuse to execute my scanf ("%c",&q);
On input 3 4 1 (for a,b and c) I had real roots OK
On input 1 8 16 I had same real roots OK.

However on 4 2 5, (for imaginary roots ) the computer cannot see the scanf ("%c",&q); statement. It just jumps over it.
How can I make the computer not to ignore this statement? I am on Visual C++ platform.
Thanks
Khoon.

/* Roots of a Quadratic Equation.
12.10.05 */

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

int main (void)

{
int a; int b; int c; float x1; float x2; int E; int E1; float R; float I;float S;
char p; char q; char y;

printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
printf ("equation ax%c+bx+c=0 :",253);
scanf ("%d%d%d", &a,&b,&c);

E =(b*b)-(4*a*c);

if ( E > 0)
{
x1 = (float)(-b+sqrt(E))/(2*a);
x2 = (float)(-b-sqrt(E))/(2*a);

printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x 2);
}

else if (E == 0)
{

x1 = (float)(-b+sqrt(E))/(2*a);

printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n", x1);
}

else
{

p = 'y';

printf ("Your quadratic equation has two distinct imaginary roots. Do you want to know\n");
printf ("the values of the imaginary roots (Y/N)?");

scanf ("%c",&q);

printf ("\nq = %c\n",q);/* Test statement*/

if (p==q)
printf ("OK I will show your the imaginary roots tomorrow.\n");

else
printf ("Good bye\n");

return 0;

}
}

Nov 15 '05
38 2548
Keith Thompson wrote:
"Red Dragon" <ts*****@stream yx.com> writes:

Sorry. What is Google Groups? I am using Outlook Express and
Visual C++ platform.


Sorry, my mistake.

If you can get your newsreader to display custom message headers
(XanaNews does) then adding the User-Agent will help differentiate
Google from other posts.

Example:
User-Agent: G2/0.2


Brian
Nov 15 '05 #31
"Red Dragon" <ts*****@stream yx.com> writes:
[...]
I was responding to the suggestion by Kevin Bagust to try using getchar()
to solve the problem of scanf() being unable to read input character.
As I learnt from Mark McIntyre, it could not read my character input
because it was reading "newline' and jumped. This problem was
demonstrated in my exhibit program as Section 1 using getchar() and
section 2 using single scanf(), both failed to execute. Only when I
used double scanf() was the problem solved.


I think this is a major misconception that you need to correct. The
getchar() and scanf() calls are *not* failing to execute. The calls
are being executed, and they're almost certainly doing exactly what
they're supposed to do. They're just not doing what you expect them
to do.

Ok, I was starting to write an explanation of what this code does when
I noticed this call:

getchar("%c",&b );

getchar() takes no arguments and returns an int value representing
the value of the input character or EOF. You have the required
"#include <stdio.h>" at the top of the program, so the compiler
knows this. Any working C compiler should give you an error message
on that line, or at least a warning.


I started C programing a month ago, self study on a book " A Structured
Programming Approach Using C by Forouzan and Gilberg. I think it is
very good.
Now I am on into Looping in Chapter 6, and I see getchar() is a topic in
Chapter 7.
Either you're running the compiler in a mode that causes it not to
display the error message (don't do that), or you're getting a warning
and ignoring it (don't do that), or the code you posted isn't the same
as the code you compiled (once again, don't do that).

When I compiled my code as shown in my previous program, I got 0 errors
and 0 warnings.


Ok, that's bizarre. What compiler are you using, and with what
command-line arguments?

Try compiling the following:

#include <stdio.h>
int main (void)
{
char b;
getchar("%c",&b );
return 0;
}

Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Does your compiler have an option that enable additional warnings?
If so, use it. Once you get your compiler to complain about the
getchar() call, fix that and any other errors it flags, and re-post it.

BTW, you seem to be snipping attributions on quoted text, the lines
that say something like

"John Doe" <so******@somew here.com> writes:

You need to leave those in so readers can tell who wrote what.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #32
"Default User" <de***********@ yahoo.com> writes:
Keith Thompson wrote:
"Red Dragon" <ts*****@stream yx.com> writes:
> Sorry. What is Google Groups? I am using Outlook Express and
> Visual C++ platform.


Sorry, my mistake.


If you can get your newsreader to display custom message headers
(XanaNews does) then adding the User-Agent will help differentiate
Google from other posts.

Example:
User-Agent: G2/0.2


The header was right in front of me. I use
Organization: http://groups.google.com
to tell me that something was posted through Google. The article in
question had
Organization: TMnet Malaysia
My brain just decided to take a little nap at that moment.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #33
Keith Thompson wrote:

Ok, that's bizarre. What compiler are you using, and with what
command-line arguments?

Try compiling the following:

#include <stdio.h>
int main (void)
{
char b;
getchar("%c",&b );
return 0;
}

Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Does your compiler have an option that enable additional warnings?
If so, use it. Once you get your compiler to complain about the
getchar() call, fix that and any other errors it flags, and re-post it.


Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example:

temp(1625)$ cat foo.c
#include <stdio.h>
int foo()
{
puts("got foo!");
return 0;
}
int main (void)
{
foo("a", 1, 2);
return 0;
}
temp(1626)$ gcc -ansi -pedantic -Wall -o foo foo.c
temp(1627)$

You get no warnings. Just wondering if OP somehow
has headers without the void...

-David

Nov 15 '05 #34
Keith Thompson wrote:
"Default User" <de***********@ yahoo.com> writes:
If you can get your newsreader to display custom message headers
(XanaNews does) then adding the User-Agent will help differentiate
Google from other posts.

Example:
User-Agent: G2/0.2


The header was right in front of me. I use
Organization: http://groups.google.com
to tell me that something was posted through Google. The article in
question had
Organization: TMnet Malaysia

That'll work too.
My brain just decided to take a little nap at that moment.


It IS a Friday (at least where I am). You can't much blame the brain.
Brian

Nov 15 '05 #35

David Resnick wrote:
Keith Thompson wrote:

Ok, that's bizarre. What compiler are you using, and with what
command-line arguments?

Try compiling the following:

#include <stdio.h>
int main (void)
{
char b;
getchar("%c",&b );
return 0;
}

Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Does your compiler have an option that enable additional warnings?
If so, use it. Once you get your compiler to complain about the
getchar() call, fix that and any other errors it flags, and re-post it.


Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example:

temp(1625)$ cat foo.c
#include <stdio.h>
int foo()
{
puts("got foo!");
return 0;
}
int main (void)
{
foo("a", 1, 2);
return 0;
}
temp(1626)$ gcc -ansi -pedantic -Wall -o foo foo.c
temp(1627)$

You get no warnings. Just wondering if OP somehow
has headers without the void...

-David


In answer to my own question, you can get this
behavior with getchar on solaris/gcc with
-traditional argument:

temp(593)$ cat foo.c
#include <stdio.h>
#undef getchar
int main (void)
{
int a = getchar("a", 1, 2);
printf("a = %c\n", a);
return 0;
}
temp(594)$ gcc -o foo -traditional -Wall foo.c
temp(595)$

Need to undef getchar, because macro version of it DOES care
about number of arguments...

-David

Nov 15 '05 #36
On Wed, 19 Oct 2005 07:02:44 +0800, "Red Dragon"
<ts*****@stream yx.com> wrote:

<snip sample of HTML from OE's multipart/alternative>
Thank you Tim,
I absolutely have no idea of the problem until I saw your post to me. Not
even I dont like to read it, I am unable to read it.
I had purposely sent myself a returned copy of the mail and it was not like
this. The returned copy had not a single line of HTML code.
You probably mean you didn't _see_ any HTML code. It was almost
certainly there. OE when it finds (correctly labelled) HTML in email
or netnews will silently _render_ (that is, execute) it. There used to
be a menu operation "View Source" which shows the actual "raw"
message, although the last time I tried to use it on a relatively
recent version (about 2002 or 2003) it had either been removed or
hidden too well for me to find it. This is (was?) similar to the (more
sensible) operation of the web browser Internet Explorer which by
default when it fetches an HTML page executes it and displays the
formatted result, but you can use View Source to see the actual code.
Alternatively I think you can File / Save As to a file in .txt format
and then open in notepad or any "not overly clever" editor.
I suppose why this problem arises is because only readers with Outlook
Express get the mail in its perfect state. Others with different platform
will get it all in HTML.
There are some other newsreaders that do render HTML, at least
optionally. But not all, which is why in many newsgroups including
here it is preferred to use plain text. As you seem to be doing
correctly at least some times.
Thanks for enlightening me.
Regards,
Khoon.


- David.Thompson1 at worldnet.att.ne t
Nov 15 '05 #37
>
There are some other newsreaders that do render HTML, at least
optionally. But not all, which is why in many newsgroups including
here it is preferred to use plain text. As you seem to be doing
correctly at least some times.

- David.Thompson1 at worldnet.att.ne t


Thanks for the information.
Regards,
Khoon.
Nov 15 '05 #38
On 21 Oct 2005 11:10:18 -0700, "David Resnick" <ln********@gma il.com>
wrote:
Keith Thompson wrote: <snips>
getchar("%c",&b ); Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example: <snip>

It is quite likely and certainly permitted to go undiagnosed.

It's less likely and certainly not guaranteed to work; that depends on
the implementation and in particular the calling convention(s), which
in turn usually depends on the platform. For some systems including
x86 Windows with its de-facto standard cdecl, it does work; on other
systems including some I've used it crashes or destroys data.

- David.Thompson1 at worldnet.att.ne t
Nov 15 '05 #39

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4347
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
5
2148
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
9
3039
by: YZK | last post by:
Hello. I'm not a Web developer, just a user, and I think I may have somehow messed myself up majorly. I'm not quite sure how. Right now, javascript used by websites I go to either does not work at all, or works sporadically. I'm talking about things like Hotmail, Yahoo Address Book, buttons on various sites, etc.. I'm a computer person, but not a Web OR Javascript person. Is there any way I can "start over again?" Can I, a Windows 98...
8
5479
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
1
4348
by: Dave | last post by:
I am having problems accessing DTS after install SP4 and was wondering if someone could offer some advice. I installed SP4 and got the following error after it competed. Unable to write to response file 'U:\WINDOWS\setup.iss' during recording. Please ensure enough space is available on target drive. I got the error 3 times (3 pop-ups).
6
1762
by: Jax | last post by:
I have Visual Studio 2002 Standard Edition. It has been working fine up to a point and now i'm at that point. Due to the limitations of the edition i am not using any of my own .dll's and instead have a range of .cs classes doing that role for me. Does that matter? I have four errors that I just dont understand, I really need some help on this or pointers to where I can find the
5
1800
by: Marc Violette | last post by:
<Reply-To: veejunk@sympatico.ca> Hello, I'm hoping someone can help me out here... I'm a beginner ASP.NET developper, and am trying to follow a series of exercises in the book entitled "Microsoft ASP.NET Step By Step" by Microsoft Press. When I try to display *any* ASP.NET page with a Sub() somewhere, I get the following error:
6
4996
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
1
3718
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
12
2478
by: =?Utf-8?B?ZGdvdw==?= | last post by:
I designed a "contact_us" page in visual web developer 2005 express along with EW2 after viewing tutorials on asp.net's help page. Features work like they should, but I cannot figure out how to send contact info to email or data base when the "send" button is pressed. I've watched the tutorials over & over again. I just can't get it. Link: http://www.syfloristonline.zipa.com/contact_us.aspx. Need help
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.