C/C++ Beginner's Guide and Post Basic Questions here

harshilsharma63

DIY FTW!
Joined
Jul 2, 2012
Messages
6,430
Explain me the output of this:

Code:
void main()
{
float a=1.22; 
if (a==1.22) 
printf("C");
else 
printf("cD");
getch();
}

Thats a classic! It's just one of many codes to represent the complex world of floating point numbers. So, this is what happens:

> 1.22 base 10 is (0.0011100001010001111010111000010100011110101110000101...) base 2.

> The bold part is the binary pattern that repeats till infinity.

> As we do not have infinite memory, so to store it in a memory, it is truncated and rounded off to (1.22000003) base 10.

> Since 1.22000003 is NOT equal to 1.22 (which is stored as 1.22000000 in a float variable), so the program executes the false parte instead of the true part of if-else code (debug the program to see the actual values).

> Have a look at this page: 2.5 — Floating point numbers « Learn C++

> Float numbers are beyond any webpage's scope!
 

velociraptor

Broken In
Joined
Dec 31, 2011
Messages
72
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed
 

nightcrawler

Broken In
Joined
Sep 25, 2006
Messages
84
Check out the MinGW wiki for First Time Users and the HOWTO guide on their site. It should solve all your problems and queries easily. Unless you are having problems in compiling itself.
 

Vyom

The Power of x480
Staff member
Admin
Joined
May 16, 2009
Messages
7,046
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed

You can either install Code::blocks, which after integration with MinGW, will give you a full fledge IDE. Or compile and run program from command line.

  1. Press Windows + R.
  2. Open Command Prompt.
  3. With CD command navigate to the "bin" folder where MinGW is installed.
  4. Compile the program:
    g++ cppfile.cpp -o output.exe
  5. And then run the output file by just entering the name of output file and pressing enter.
    output.exe


You could have also used c++ command as an alternative to g++.

PS: Of course you have to keep program in bin folder, or give full path of the cpp filename.
 

gameranand

Living to Play
Joined
Apr 26, 2008
Messages
14,252
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed

If you don't want any trouble and don't want to go the conventional way then you can use Code::Blocks. :)
 

somebodysme

Journeyman
Joined
Dec 9, 2011
Messages
139
For improving problem solving
*projecteuler.net
*www.codechef.com

Register and get started
 

nightcrawler

Broken In
Joined
Sep 25, 2006
Messages
84
In projecteuler.net, do they give solutions too?
No not directly. They give you a problem and ask you for the output. If it is correct you have solved the puzzle. I believe once that is done you could access the forum to get the answer or something. Although not sure. Not been on projecteuler for last 2 years or so.
 

harshilsharma63

DIY FTW!
Joined
Jul 2, 2012
Messages
6,430
In projecteuler.net, do they give solutions too?
They have a forum for the purpose. However, I recommend to build the code yourself, get the correct answer, the optimize the code to the lowest complexity possible and then compare it with a code in the forum or elseware :)
 

kunalht

In the zone
Joined
Sep 7, 2012
Messages
392
Hii
I have just completed 12th sci.
Now I have vacation.
I want to learn programming in this vacation.
Which shold I learn HTML or C or C++ or JAVA ????
I want to learn this at home(without any classes)
can i learn it without classes??
& also plz suggest me some website or books or ebooks for learning that....
 

Nerevarine

Incarnate
Joined
Feb 6, 2011
Messages
8,566
python, learn it as a hobby..
Im learning it myself..
although.. u need basic OOP concepts to do any good
 

rohitshubham

Ambassador of Buzz
Joined
Jul 20, 2008
Messages
550
Help Me with this code
Code:
[COLOR=#339933]#include<stdio.h>[/COLOR]
 [COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#993333]void[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]   
 [COLOR=#993333]long[/COLOR] x[COLOR=#339933],[/COLOR]y [COLOR=#339933];
[/COLOR] [URL="*www.opengroup.org/onlinepubs/009695399/functions/scanf.html"][COLOR=#000066]scanf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l %l"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]x[COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]   [COLOR=#B1B100]if[/COLOR][COLOR=#009900]([/COLOR]x[COLOR=#339933]>[/COLOR]y[COLOR=#009900])
[/COLOR]    [COLOR=#009900]{[/COLOR]    [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]     [COLOR=#009900]}[/COLOR]  
  [COLOR=#B1B100]else[/COLOR]     [COLOR=#009900]{[/COLOR]     
[COLOR=#B1B100]       if[/COLOR][COLOR=#009900]([/COLOR]x [COLOR=#339933]%[/COLOR] [COLOR=#0000DD]5[/COLOR] [COLOR=#339933]==[/COLOR][COLOR=#0000DD]0[/COLOR][COLOR=#009900])[/COLOR]  
      [COLOR=#009900]{[/COLOR]         y[COLOR=#339933]=[/COLOR] y[COLOR=#339933]-[/COLOR]x [COLOR=#339933];[/COLOR]    
               y[COLOR=#339933]=[/COLOR]y[COLOR=#339933]-[/COLOR][COLOR=#800080]0.5[/COLOR][COLOR=#339933];
[/COLOR]               [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]     [COLOR=#009900]}[/COLOR]     
      [COLOR=#B1B100]else[/COLOR] 
       [COLOR=#009900]{[/COLOR]            [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]                [COLOR=#009900]}[/COLOR]     
[COLOR=#009900]}
[/COLOR]     [COLOR=#009900]}[/COLOR]
It's Giving me runtime error

Help Me with this code
Code:
[COLOR=#339933]#include<stdio.h>[/COLOR]
 [COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#993333]void[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]   
 [COLOR=#993333]long[/COLOR] x[COLOR=#339933],[/COLOR]y [COLOR=#339933];
[/COLOR] [URL="*www.opengroup.org/onlinepubs/009695399/functions/scanf.html"][COLOR=#000066]scanf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l %l"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]x[COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]   [COLOR=#B1B100]if[/COLOR][COLOR=#009900]([/COLOR]x[COLOR=#339933]>[/COLOR]y[COLOR=#009900])
[/COLOR]    [COLOR=#009900]{[/COLOR]    [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]     [COLOR=#009900]}[/COLOR]  
  [COLOR=#B1B100]else[/COLOR]     [COLOR=#009900]{[/COLOR]     
[COLOR=#B1B100]       if[/COLOR][COLOR=#009900]([/COLOR]x [COLOR=#339933]%[/COLOR] [COLOR=#0000DD]5[/COLOR] [COLOR=#339933]==[/COLOR][COLOR=#0000DD]0[/COLOR][COLOR=#009900])[/COLOR]  
      [COLOR=#009900]{[/COLOR]         y[COLOR=#339933]=[/COLOR] y[COLOR=#339933]-[/COLOR]x [COLOR=#339933];[/COLOR]    
               y[COLOR=#339933]=[/COLOR]y[COLOR=#339933]-[/COLOR][COLOR=#800080]0.5[/COLOR][COLOR=#339933];
[/COLOR]               [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]     [COLOR=#009900]}[/COLOR]     
      [COLOR=#B1B100]else[/COLOR] 
       [COLOR=#009900]{[/COLOR]            [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]                [COLOR=#009900]}[/COLOR]     
[COLOR=#009900]}
[/COLOR]     [COLOR=#009900]}[/COLOR]
It's Giving me runtime error
 

harshilsharma63

DIY FTW!
Joined
Jul 2, 2012
Messages
6,430
^ At least mention the error and the input for which you are getting this error.

oops, krishnandu.sarkar's post cane beteen.
 
Top