473,738 Members | 4,774 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 #1
38 2538

"Red Dragon" <ts*****@stream yx.com> wrote in message
news:43******* *@news.tm.net.m y...
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)?");
/*************** *************** *************** ***********
fflush (stdin );
/*************** *************** *************** *********** scanf ("%c",&q); printf ("\nq = %c\n",q);/* Test statement*/

/snip>
Nov 15 '05 #2
In article <dj*********@bl ackmamba.itd.rl .ac.uk>,
Geoff Turner <g.********@rl. ac.uk> wrote:
fflush (stdin );


No! fflush() has no defined behaviour on an input stream!!

--
I am spammed, therefore I am.
Nov 15 '05 #3
In article <43********@new s.tm.net.my>,
Red Dragon <ts*****@stream yx.com> wrote:
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);


We go through this about every second day, and it is surely
in the FAQ.

My explanation of a week ago can be found at
http://groups.google.ca/group/comp.l...65f0744a89f300

--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Nov 15 '05 #4
Red Dragon wrote:
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.

<snip!>

You have been repeatedly asked not to post using HTML.
Please stop it. This is usenet, not some mailing list or forum.

Read the FAQ on input using scanf() -- it is not advisable.
At all.

Apart from that: You did not look at the return value of scanf()
so how do you know what happened? You also seem not to have used
the debugger which comes with your IDE.

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5
"Red Dragon" <ts*****@stream yx.com> writes:
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);


It doesn't. Your call to scanf("%c", &q) is doing exactly what it's
supposed to do.

To find out what it's supposed to do, read the documentation for
scanf().

You're almost certainly better off using a different method to read
input. A common technique is to use fgets() to read a line at a time,
then use sscanf() to parse the string. (This can have its own
drawbacks; fgets() either leaves the newline character in the input
string, or truncates the line if it's too long.)

--
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 #6
Red Dragon wrote:
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);


You posted this program a couple of weeks ago and got lots
of advice. But you seem to have ignored most of this advice.
Here it is again:

1) x1, x2, R, I, S should be "double", not "float"
2) You MUST check the return value of scanf() and take appropriate
action if it is wrong
3) Don't do any casting (eg. in the line "x1 = (float)FOO")
4) The "return 0" goes AFTER the "else" block, not inside it.

Hint: If you printf the result of your scanf("%c") using
%d, you might understand what is going on.

Nov 15 '05 #7
> We go through this about every second day, and it is surely
in the FAQ.

My explanation of a week ago can be found at
http://groups.google.ca/group/comp.l...65f0744a89f300

--
Quote from Google: Is there any thing whereof it may be said, See, this is new? It hath

scanf() with a %c format element reads *exactly* one character. You
are entering two characters, the input you want and the newline to
terminate the line. Thus after the first scanf(), you still have
a character in the input buffer waiting to be read by the second
scanf().

Dear Walter,
Yes Mr. Walter. This is the exact problem I am having. I dont quite understand the phrase "newline to terminate the line". What is it? It is the "\n" thing? Or a "space"? Can you explain what is the "newline to terminate the line"? How shall I solve the problem? I can use two scanf() line and it will solve the problem. The first scanf() reads the "newline... " and the second scanf() will read my input. Is this the normal way to solve the problem? What is the proper way? Sorry for asking you so many questions.
Thank you.
Khoon.



Nov 15 '05 #8
>
You have been repeatedly asked not to post using HTML.
Please stop it. This is usenet, not some mailing list or forum.

Read the FAQ on input using scanf() -- it is not advisable.
At all.

Apart from that: You did not look at the return value of scanf()
so how do you know what happened? You also seem not to have used
the debugger which comes with your IDE.

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Mr. Michael,
I wish to apologize for causing you problem. I did not know I was causing a
problem.
1. The dont understand the HTML thing. I did not use HTML. What I did was
to copy from my Visual C++ platform and paste on the Outlook Express
screen. With Mr. Skarmander's instruction, I have set the radio button to
"Plain Text" on the Send Tab and thought the problem is solved. I have
actually sent a copy of the outgoing mail to myself and I dont see any HTML
thing on my screen on the returned copy. So I dont know what else to do.

2. Yes. I tried to look at the return value of scanf() but got nothing
because the computer skipped it . As I just found out, it actually read
the "newline" (I dont know what it is ) so it registered nothing, creating
the problem in question.

3. What is debugger and IDE.? Hope you can bear with me because I am new
to all this and am doing self study. I have nobody else to ask.

Thank you and sorry again.
Khoon.

Nov 15 '05 #9

"Keith Thompson" <ks***@mib.or g> wrote in message news:ln******** ****@nuthaus.mi b.org...
"Red Dragon" <ts*****@stream yx.com> writes:
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);
It doesn't. Your call to scanf("%c", &q) is doing exactly what it's
supposed to do.

To find out what it's supposed to do, read the documentation for
scanf().

I have read the documentation in the Google as recommended by Walter. I understand now what has happened. On program running, my scanf() read the "newline" instruction in the buffer and jumped to the next statement. So I dont have a chance to make an input. To solve the problem, I can put in 2 scanf()s. But is this the correct way?
Rgds,
Khoon.
You're almost certainly better off using a different method to read
input. A common technique is to use fgets() to read a line at a time,
then use sscanf() to parse the string. (This can have its ow
drawbacks; fgets() either leaves the newline character in the input
string, or truncates the line if it's too long.)

Nov 15 '05 #10

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

Similar topics

6
4345
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
2146
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
5478
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
4347
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
1761
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
3717
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
2476
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
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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
9476
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...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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...
1
3279
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
2193
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.