473,770 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gcc doesn't seem to get my scanf right, but Visual Studio does, what's wrong?

Bangonkali
7 New Member
First I actually thought there was something wrong with my programming skills. But after I compiled the same code using Visual Studio, it worked. So here's the code that doesn't seem to be working right with gcc compilers, but they do compile. Note I'm using the Cygwin 3.4.4 version of gcc and 4.5.1 version of gcc from MinGW.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     long double r = 0;
  6.     long double p = 0;
  7.  
  8.     printf ("Enter a number: ");
  9.     scanf ("%lf", &r);
  10.     printf ("Enter its power: ");
  11.     scanf ("%lf", &p);
  12.     printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
  13.  
  14.     printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
  15.     printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
  16.     printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
  17.     return 0;
  18. }
This is the first one. It compiles with GCC but the output doesn't seem to be what I'm expecting. (MinGW gcc (GCC) 4.5.1)
Expand|Select|Wrap|Line Numbers
  1. Enter a number: 5
  2. Enter its power: 3
  3. 5.000000 to the power of 0.000000 = 0.000000
  4. 7 ^ 3 = 343.000000
  5. 4.73 ^ 12 = 125410439.217423
  6. 32.01 ^ 1.54 = 208.036691
The part "5.000000 to the power of 0.000000 = 0.000000" doesn't seem to be right. But on the other hand, when I compiled it using Visual Studio, it worked very well.
Expand|Select|Wrap|Line Numbers
  1. Enter a number: 5
  2. Enter its power: 3
  3. 5.000000 to the power of 3.000000 = 125.000000
  4. 7 ^ 3 = 343.000000
  5. 4.73 ^ 12 = 125410439.217423
  6. 32.01 ^ 1.54 = 208.036691
I looked for a solution in the internet and I found sscanf. I tried to modified the code, it still compiles with gcc but yielding the same results.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     char line[100];
  6.     long double r = 0;
  7.     long double p = 0;
  8.  
  9.     printf ("Enter a number: ");
  10.     fgets(line,sizeof(line),stdin);
  11.     sscanf(line,"%lf",&r);
  12.  
  13.     printf ("Enter its power: ");
  14.     fgets(line,sizeof(line),stdin);
  15.     sscanf(line,"%lf",&p);
  16.  
  17.     printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
  18.  
  19.     printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
  20.     printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
  21.     printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
  22.     return 0;
  23. }
Using the gcc compiler of MinGW this is the result: (MinGW gcc (GCC) 4.5.1)
Expand|Select|Wrap|Line Numbers
  1. Enter a number: 5
  2. Enter its power: 3
  3. 5.000000 to the power of 0.000000 = 0.000000
  4. 7 ^ 3 = 343.000000
  5. 4.73 ^ 12 = 125410439.217423
  6. 32.01 ^ 1.54 = 208.036691
The previous yields the same result as before where the part "5.000000 to the power of 0.000000 = 0.000000" is not working as intended. If I use the compiler from Visual Studio 2008 C++, this is the result:
Expand|Select|Wrap|Line Numbers
  1. Enter a number: 5
  2. Enter its power: 3
  3. 5.000000 to the power of 3.000000 = 125.000000
  4. 7 ^ 3 = 343.000000
  5. 4.73 ^ 12 = 125410439.217423
  6. 32.01 ^ 1.54 = 208.036691
Dec 11 '10 #1
12 7560
donbock
2,426 Recognized Expert Top Contributor
Perhaps the difference in line termination (LF vs CR-LF) between unix [Cygwin] and DOS [Visual Studio] is catching you. You should be testing the return code from scanf.
Dec 12 '10 #2
AdrianH
1,251 Recognized Expert Top Contributor
No, looks like a bug. Submitting to gcc bugzilla. Bug 46908


A
Dec 12 '10 #3
Bangonkali
7 New Member
Thanks guys! But I think I have found the answer to my problem now. According to someone who replied to my email it was because gcc was keeping a space in the keyboard buffer. And you can tell if the same thing's happening again if the second input is always 0. He said I should try to use the following code separated by a space instead:

Expand|Select|Wrap|Line Numbers
  1. scanf( "%lf %lf", &r, &p );
I edited my original code to work like this:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     // my personal troubles with long double
  6.     // while using gcc under cygwin and mingw
  7.     long double r = 0;
  8.     long double p = 0;
  9.     long double a = 0;
  10.  
  11.     printf ("Enter a number <space> power: ");
  12.     scanf( "%Lf %Lf", &r, &p );
  13.  
  14.     // r to the power of p
  15.     a = pow(r,p);
  16.  
  17.     printf ("%.2Lf to the power of %.2Lf = %.2Lf\n", r, p, a);
  18.     printf ("7 ^ 3 = %.2Lf\n", (long double)pow(7.0,3.0));
  19.     printf ("4.73 ^ 12 = %.2Lf\n", (long double)pow(4.73,12.0));
  20.     printf ("32.01 ^ 1.54 = %.2Lf\n", (long double)pow(32.01,1.54));
  21.     return 0;
  22. }
Now it works perfectly fine with the following output when compiled with gcc both from MinGW and Cygwin.

Expand|Select|Wrap|Line Numbers
  1. 5.00 to the power of 3.00 = 125.00
  2. 7 ^ 3 = 343.00
  3. 4.73 ^ 12 = 125410439.22
  4. 32.01 ^ 1.54 = 208.04
I still don't know how accept value from a second scanf though. If anyone of you who knows how to circumvent this issue please help. I need to accept the second input only after the return has been pressed from the first input and not separated by space.
Dec 12 '10 #4
Bangonkali
7 New Member
THIS PROBLEM IS SOLVED! Thank you for everyone who replied. Not only from this forum but also from all the other forums which I sent this problem to.

First let me summarize the problem. I'm using scanf to accept a long double value from the user. I used %Lf to accept this and used %.2Lf to printf it. The problem is that I can't do more than 2 scanf's because I can only read the first one if I used Cygwin's gcc compiler and MinGW's gcc compiler. The same code works with VSC++ though.

However, if I use only double, and use %lf to scanf and printf data, the programs works quite well even if I have many scanf's.

A person from one forum gave me an idea about keyboard caching. Which basically means, after I enter a value from the first %Lf (long double) another character is also received which totally messes up the next scanf. And thus when I run my initial code, I receive a 0 value for the next scanf.

I found out that by putting getchar() from between the two scanf's I can circumvent the issue. Let me repeat, the problem only works on long double scanf's using %Lf, it doesn't occur on only doubles using %lf. The following is my final code that really works on gcc.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     // my personal troubles with long double
  6.     // while using gcc under cygwin and mingw
  7.     long double r = 0;
  8.     long double p = 0;
  9.     long double a = 0;
  10.  
  11.     //printf ("Enter a number <space> power: ");
  12.     //scanf( "%lf %lf", &r, &p );
  13.  
  14.     printf ("Enter a number: ");
  15.     scanf( "%Lf", &r);
  16.  
  17.     printf ("Enter a number: ");
  18. getchar();    
  19.     scanf( "%Lf", &p );
  20.  
  21.     // r to the powler of p
  22.     a = pow(r,p);
  23.  
  24.     // put (long double) typecast before pow if you're
  25.     // printing pow directly on printf and you're using
  26.     // Cygwin's gcc (GCC) 3.4.4. You may not edit this 
  27.     // code if you're compiling on VSC++ or MinGW's gcc 
  28.     // (GCC) 4.5.1
  29.     printf ("%.2Lf to the power of %.2Lf = %.2Lf\n", r, p, a);
  30.     printf ("7 ^ 3 = %.2Lf\n", pow(7.0,3.0)); 
  31.     printf ("4.73 ^ 12 = %.2Lf\n", pow(4.73,12.0));
  32.     printf ("32.01 ^ 1.54 = %.2Lf\n", pow(32.01,1.54));
  33.     return 0;
  34. }
And also, because of the problem I learned something odd. If you are going to compile with Cygwin's gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125) make sure to add (long double) type casting before pow(4.73,12.0) if you are going to directly printf it using %Lf. But if you're going to use MinGW's gcc (GCC) 4.5.1, you must not put (long double) type casting before pow(4.73,12.0) if you are going to directly printf it using %Lf. But both ways work with VSC++. :D

I'm just a newbie here and without the help of the many people who replied I could not have found the idea about the getchar() to solve this problem. I didn't know there really are a lot of good people around the internet. And seriously, I'm really sorry for those who I have annoyed because i sent the same thread to many forums. Thank you guys. THIS THREAD IS SOLVED. :D
Dec 12 '10 #5
AdrianH
1,251 Recognized Expert Top Contributor
Please look at the bug report with the test code I submitted. It is a bug. It should not be responding that way.


A
Dec 12 '10 #6
Bangonkali
7 New Member
THIS THREAD IS SOLVED You were using %lf for long double. It should be %Lf. As noted, the status of your bug report is RESOLVED INVALID, which basically means, it was a false report. Once again, for long double, use %Lf and not %lf. I commited that mistake in my first post, which lead me to believe it was a bug. I've learned a lot since then though. And bte, thank you so much for clarifying your point, I missed it out in the first post. THIS THREAD IS SOLVED
Dec 12 '10 #7
AdrianH
1,251 Recognized Expert Top Contributor
Well, I've retested with %Lf with the same results and resubmitted the bug. Looks like gcc 4.5.0 doesn't recognise %Lf for scanf, so it is either a bug or it hasn't been implemented yet. Also, to get printf to recognise %Lf, you need to change the standard settings with compiler flag -std=c99.

Something else I forgot to do, but always did when I was using gcc is to use the -Wall switch. This will pick up many inadvertent errors in the code.


A

[edit]bug has been resubmitted.
Dec 12 '10 #8
AdrianH
1,251 Recognized Expert Top Contributor
Also, IIRC, white spaces are ignored unless expressly stated not to be. I.e. use %[ a-zA-Z_0-9] as a format string. So, your solution is a workaround, not a code correct solution.


A
Dec 12 '10 #9
Bangonkali
7 New Member
Yes you do have a point there.
Dec 12 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3412
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script language="JavaScript"> function doWord(file) { if (navigator.userAgent.indexOf("MSIE")!=-1)
33
3170
by: John Timbers | last post by:
I'd like to purchase Visual C# .Net for learning purposes only since it's a lot cheaper than Visual Studio (note that I'm a very experienced C++ developer). Can someone simply clarify the basic differences. Ok, Visual Studio has C++, VB and J++ thrown in plus some extra bells and whistles (I already have some minimal experience) but are both IDE's essentially the same (including the same IDE support for creating forms, ADO.NET DataSets,...
5
1616
by: Daniele | last post by:
Hi, how is it possible to create a project with a specific template, compile it and close it? All with a c# script. Thanks, Daniele
4
1886
by: fyleow | last post by:
I create a new Python file with the following using Wing IDE. import feedparser d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml") print d.feed.title I get this error when I debug. AssertionError:
5
2334
by: Nathan Sokalski | last post by:
I recently upgraded from Visual Studio .NET 2003 to Visual Studio .NET 2005. Visual Studio .NET 2005 does not create the Global.asax files that Visual Studio .NET 2003 did, which I used for variables such as the location of my database. What is the conventional way of creating global variables for purposes like this in Visual Studio .NET 2005? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
6
2308
by: Jerad Rose | last post by:
I recently installed Visual Studio 2003, after having already installed VS 2005. Unfortunately, it took over all of the file associations. There is an option in Visual Studio 2005 to restore these associations, under Tools > Options Environment General. However, this button doesn't seem to do anything. Specifically, my SLN files were associated to VS 2003, and when I clicked this, this association didn't change. I even tried deleting...
0
1155
by: ShimmyShine | last post by:
My laptop is a HP Compaq nc6000. My OS is Windows XP Professional Service Pack 2. And I am not sure what is wrong but I can't seem to install visual studio 5.0. I bought it, came home on my older computer and it installed fine, although im pretty sure that was Windows XP Service Pack 1. But then i reformatted my computer and got Windows XP Professional Service Pack 2 and it will not install at all. There is no error all that happens is right...
2
1466
by: lamanvic | last post by:
i am trying to create a list of array with random numbers and use the colour sort to sort them to let the negative number on LHS zero in middle and positive number on RHS and print out the sorted array but it doesn't work and i have no idea what is wrong with it can some one please help me thanks using System; using System.Collections.Generic; using System.Text;
0
1167
by: Sin Jeong-hun | last post by:
http://misako.co.kr/externaldata/Test.aspx I've created a very simple user control which just has a label on it. I followed the instruction but it doesn't show up. I don't know what's wrong because there's no error message. It just showed an iframe-like thing. Please let me know what went wrong. The user control itself can be downloaded at http://misako.co.kr/externaldata/Test.zip (I zipped it because the .dll file cannot be...
0
7335
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results. Visual Studio automatically creates a number of...
0
9453
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
10254
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
10099
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...
0
6710
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
5354
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
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.