473,761 Members | 4,739 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
Red Dragon wrote:
"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:5t******** *************** *********@4ax.c om...
On Tue, 18 Oct 2005 17:57:50 +0800, in comp.lang.c , "Red Dragon"
<ts*****@stre amyx.com> wrote:
I dont quite understand the phrase "newline to terminate the line".How many keys do you press, when you supply user input value of 1? One
key or two?
--
Mark McIntyre


2 keys. One for 1 and One for Enter.
Can you kindly tell me is using 2 scanf() the best solution to the problem?


No, What would happen if the user entered more than one character on the
line before entering Enter?
What is a better method?


Have a look at the getchar() function. Use that to write a bit of code
that will keep getting characters until it receives a new line.

Kevin Bagust.
Nov 15 '05 #21
On Wed, 19 Oct 2005 19:02:32 +0800, in comp.lang.c , "Red Dragon"
<ts*****@stream yx.com> wrote:

"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:5t******* *************** **********@4ax. com...
On Tue, 18 Oct 2005 17:57:50 +0800, in comp.lang.c , "Red Dragon"
<ts*****@stream yx.com> wrote:
I dont quite understand the phrase "newline to terminate the line".
How many keys do you press, when you supply user input value of 1? One
key or two?
--
Mark McIntyre


2 keys. One for 1 and One for Enter.


.... so you see now where the newline comes from.
Can you kindly tell me is using 2 scanf() the best solution to the problem?
What is a better method?


Personally I prefer to fgets() into a char array of known size, then
parse the array via eg sscanf. I can then decide if there's still
input waiting to be read, and if so I can empty it via repeated calls
to say getchar.

This is probably a FAQ (12.18, 12.20, 12.26 all seem relevant to this
discussion, plus a few others in that area).
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #22
I tried getchar() and found it has same effect as scanf()
Anyway thanks at lot.
Rgds,
Khoon.

Nov 15 '05 #23
"Red Dragon" <ts*****@stream yx.com> writes:
I tried getchar() and found it has same effect as scanf()
Anyway thanks at lot.
Rgds,
Khoon.


Surely you've been here long enough to know how to post a proper
followup using Google Groups. I've even seen you do it in this
thread.

Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context. They're obviously two different
functions, doing different things. If you'll show us some code, we
can suggest improvements.

--
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 #24

"Keith Thompson" <ks***@mib.or g> wrote in message news:ln******** ****@nuthaus.mi b.org...
"Red Dragon" <ts*****@stream yx.com> writes:
I tried getchar() and found it has same effect as scanf()
Anyway thanks a lot.
Rgds,
Khoon.
Surely you've been here long enough to know how to post a proper
followup using Google Groups. I've even seen you do it in this
thread.


Sorry. What is Google Groups? I am using Outlook Express and Visual C++ platform.
Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context. They're obviously two different
functions, doing different things. If you'll show us some code, we
can suggest improvements.


Thank you. Here is a program to illustrate my point.

/*Program to Demonstrate getchar() and scanf()*/
#include <stdio.h>

int main (void)
{
char a,b;

/*Section 1*/
printf("Enter a character > ");
scanf("%c",&a);
printf("a=%c\n" ,a);
printf("Enter another character > ");
getchar("%c",&b );
printf("b=%c\n" ,b);

/*section 2*/
printf("\nEnter a character > ");
scanf("%c",&a);
printf("a=%c\n" ,a);
printf("Enter another character > ");
scanf("%c",&b);
printf("b=%c\n" ,b);

/*section 3*/
printf("\nEnter a character > ");
scanf("%c",&a);
printf("a=%c\n" ,a);
printf("Enter another character > ");
scanf("%c",&b); /* Dummy */
scanf("%c",&b);
printf("b=%c\n" ,b);

return 0;
}
/*PRINT RESULT
Enter a character > a
a=a
Enter another character > b=¦

Enter a character > a
a=a
Enter another character > b=

Enter a character > a
a=a
Enter another character > b
b=b
Press any key to continue
*/

You can see that I have divided the program into 3 sections.
1st section with getchar()
2nd section with single scanf()
3rd section with double scanf()
In the Printout Result, only 3rd Section with a Dummy performed perfectly.
BTW, Do you have HTML problem with my program above?
Regards and thank you very much.
Khoon.
Nov 15 '05 #25
"Red Dragon" <ts*****@stream yx.com> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Red Dragon" <ts*****@stream yx.com> writes:
I tried getchar() and found it has same effect as scanf()
Anyway thanks a lot.
Surely you've been here long enough to know how to post a proper
followup using Google Groups. I've even seen you do it in this
thread.

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


Sorry, my mistake.

groups.google.c om has a broken interface that encourages users to post
followups with no attributions or quoted text. I don't know why I
didn't check your article's headers before assuming you were using
Google Groups.

In general, posting a followup that doesn't quote any of the parent
article is considered rude.
Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context. They're obviously two different
functions, doing different things. If you'll show us some code, we
can suggest improvements.

Thank you. Here is a program to illustrate my point.

/*Program to Demonstrate getchar() and scanf()*/
#include <stdio.h>

int main (void)
{
char a,b;

/*Section 1*/
printf("Enter a character > ");
scanf("%c",&a);
printf("a=%c\n" ,a);
printf("Enter another character > ");
getchar("%c",&b );
printf("b=%c\n" ,b);


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.

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

If you're going to post code, you need to copy-and-paste the *exact*
code that you fed to the compiler. If you try to re-type it, you'll
make mistakes (I know I would), and there's no way we can guess which
errors are in the original code and which you introduced by re-typing
it.

If that really was the code you compiled, including the getchar() call
with too many arguments, you need to compile without turning off
diagnostic messages, and you need to fix any errors flagged by the
compiler.

[...]
BTW, Do you have HTML problem with my program above?


Not that I can see, but I think my newsreader sometimes quietly
renders HTML as plain text.

--
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 #26
In general, posting a followup that doesn't quote any of the parent
article is considered rude.
Yes. I agree with that.
Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context.

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.
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.
If you're going to post code, you need to copy-and-paste the *exact*
code that you fed to the compiler. If you try to re-type it, you'll
make mistakes (I know I would), and there's no way we can guess which
errors are in the original code and which you introduced by re-typing
it.

When Tim Rentsch posted back to me what he had received, I was horrified by the load of gibberish HTML codes I had inadvertently created.
What I had earlier done was to copy from my Visual C++ platform and paste into Outlook Express. What I now do is to cut and paste into Notepad, and then cut from Notepad and paste into Outlook. In this way, I reckon I have killed all the HTML code. I did not do any retyping.
BTW, Do you have HTML problem with my program above?


Not that I can see, but I think my newsreader sometimes quietly
renders HTML as plain text.


Thank you very much.
Regards,
Khoon.
Nov 15 '05 #27
In article <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
"Red Dragon" <ts*****@stream yx.com> writes:

....
/*Program to Demonstrate getchar() and scanf()*/
#include <stdio.h>

int main (void)
{
char a,b;


This is the first error. a and b should be ints.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #28
On Fri, 21 Oct 2005 17:19:09 +0800, Red Dragon wrote:
[in reply to Keith Thompson]
Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context.

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.


Actually the point is that it was _not_ reading newline.
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.
The dummy scanf is reading the newline (generated when you press
enter/return) that was not read by the first scanf.

[...] I see getchar() is a topic in Chapter 7.
getchar() doesn't take arguments as scanf does, it returns a value.

Your code getchar("%c",&b ); should be rewritten b = getchar();

[...] When I compiled my code as shown in my previous program, I got 0 errors
and 0 warnings.
Find out how to invoke your Visual C++ compiler in ANSI or ISO C mode
(these may be abbreviated as C89/C90/C99). In such a mode it is required
to issue a diagnostic for your code.

[...] When Tim Rentsch posted back to me what he had received, I was
horrified by the load of gibberish HTML codes I had inadvertently
created. What I had earlier done was to copy from my Visual C++ platform
and paste into Outlook Express. What I now do is to cut and paste into
Notepad, and then cut from Notepad and paste into Outlook. In this
way, I reckon I have killed all the HTML code.


Some of your posts - even those without source code - still contain HTML,
including the one to which I'm replying and its predecessor. It is
likely that Outlook is generating it - check that Plain text is still
selected for News sending on the Send tab.

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #29
Red Dragon wrote:

BTW, Do you have HTML problem with my program above?
Regards and thank you very much. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">


Keith may not see it, but I do. Weren't you given instructions on how
to set OE so it doesn't do that?

Brian
Nov 15 '05 #30

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
9989
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
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
9811
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...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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?
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.