Jun 13, 2012 at 12:33pm UTC
I Finally learned how to use switch statements and for some reason I felt the need to post a topic about it. Feel free to spam me about how this is a stupid topic, but I really felt the need to share xD
Jun 13, 2012 at 12:41pm UTC
Well if you know switch then could you say what will be printed in the following program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <stdio.h>
int main( void )
{
puts( "--- 1 ---" );
switch ( 0 )
if ( 1 ) case 0: puts( "0" );
else default : puts( "default" );
puts( "--- 2 ---" );
switch ( 0 )
while ( 0 )
if ( 1 ) case 0: puts( "0" );
else default : puts( "default" );
return ( 0 );
}
Last edited on Jun 13, 2012 at 12:42pm UTC
Jun 13, 2012 at 12:45pm UTC
Whats the "puts" thing? was this done is that visual C++ thing? 'cause I hear that has some different functions than most compilers like Code::Blocks.
Jun 13, 2012 at 12:48pm UTC
It is a C code. puts is a standard functiion declared in <stdio.h>. You may use this code as C++ by including <cstdio> and using standard name space.
Last edited on Jun 13, 2012 at 12:48pm UTC
Jun 13, 2012 at 12:50pm UTC
hm, so is it like the cout << function?
Jun 13, 2012 at 12:53pm UTC
Yes you can use standard C++ stream functions as the operator << and you can use C-style output functions.
For example
1 2 3 4 5 6
#include <cstdio>
int main()
{
std::puts( "Hello World!" );
}
Last edited on Jun 13, 2012 at 12:53pm UTC
Jun 13, 2012 at 12:57pm UTC
ok so if that "puts" thing is pretty much exactly like cout << then that would print
--- 1 ---
0
--- 2 ---
would it not?
Jun 13, 2012 at 12:59pm UTC
No, it is incorrect answer. The result will be
--- 1 ---
0
--- 2 ---
0
Jun 13, 2012 at 12:59pm UTC
oh, oops
oh well ^.^ i was close
Last edited on Jun 13, 2012 at 1:00pm UTC
Jun 13, 2012 at 1:15pm UTC
The enter character is still in the input buffer after you entered a value with the operator >>. So you shall clear the buffer before the next
cin.get(hold);