Active Topics

 


Reply
Thread Tools
slvr32's Avatar
Posts: 168 | Thanked: 104 times | Joined on Feb 2008 @ California, USA
#11
Originally Posted by SubCore View Post
you need to tell g++ where to look for conio.h, usually via the -L switch.
I think you mean '-I' for including header files in non-standard paths, whereas -L is a linker flag for libraries.

But I agree with conio.h and getc() being old ms-dos legacy.

What about getchar()? Also only needs stdio.h, but I'm not sure about the buffered/unbuffered input issue.

Last edited by slvr32; 2011-06-15 at 17:17.
 

The Following User Says Thank You to slvr32 For This Useful Post:
nicholes's Avatar
Posts: 1,103 | Thanked: 368 times | Joined on Oct 2010 @ india, indore
#12
i got this
#include <stdio.h>

main()
{
for( ;; )
{
printf ("Hello World!\n");
}
}



but this is for loop and works on pc

and when i try to run this on n900 like in my first post..

this time nothing happened and put me back to Mydocs

it shows nothing!
Attached Images
 
__________________
N900 gave me a reason to live in this cruel world

get your smooth live wallpaper today
My YouTube videos

Last edited by nicholes; 2011-06-15 at 17:13.
 
slvr32's Avatar
Posts: 168 | Thanked: 104 times | Joined on Feb 2008 @ California, USA
#13
Originally Posted by nicholes View Post
can you give an example here for me how to get rid of conio.h and getch()?

Code:
# include <stdio.h>

int main (void)
{
printf ("hellow wolrd");
getchar();

}
compiles with...

g++ hello.cpp

and runs with...

./a.out

where a.out is the default name for executables created with gcc/g++, if you don't specifiy a different output file with -o, e.g. g++ -o myprogram hello.cpp

Last edited by slvr32; 2011-06-15 at 17:18.
 

The Following 2 Users Say Thank You to slvr32 For This Useful Post:
nicholes's Avatar
Posts: 1,103 | Thanked: 368 times | Joined on Oct 2010 @ india, indore
#14
there is no error now but it does not execute also(i think problem is related to device now not in programme)

BTW thanks to all you guys for so quick reply
__________________
N900 gave me a reason to live in this cruel world

get your smooth live wallpaper today
My YouTube videos
 
SubCore's Avatar
Posts: 850 | Thanked: 626 times | Joined on Sep 2009 @ Vienna, Austria
#15
Originally Posted by nicholes View Post
there is no error now but it does not execute also(i think problem is related to device now not in programme)

BTW thanks to all you guys for so quick reply
after it compiles without error, you have to make the resulting file (a.out is default) executable by "chmod +x a.out". this has to be done in a folder which supports this flag, MyDocs is a FAT filesystem which does NOT support it.
copy it to /home/user f.ex., then use chmod +x.
__________________
"What we perceive is not nature itself, but nature exposed to our method of questioning."
-- Werner Karl Heisenberg
 

The Following 3 Users Say Thank You to SubCore For This Useful Post:
Captwheeto's Avatar
Posts: 302 | Thanked: 193 times | Joined on Oct 2008 @ England
#16
It looks like you're writing just plain C anyway.

I've just written my first ever bit of C++ code so don't laugh

Code:
#include <iostream>
using namespace std;

main() {
	cout << "hello, world\n";
	if (cin.get() == '\n')
	return 0;	
}

Last edited by Captwheeto; 2011-06-16 at 09:30.
 

The Following 3 Users Say Thank You to Captwheeto For This Useful Post:
onethreealpha's Avatar
Posts: 434 | Thanked: 990 times | Joined on May 2010 @ Australia
#17
for c++ try this

#include <iostream>

using namespace std;

int main()
{
cout << "Hello world!" << endl;
return 0;
}

this will give you the "hello world" with confirmation of correct operation and ask for keyboard input to finish (return 0 to main)

iostream is required for input/output functions
printf is the "C" equivalent cout when using "C++"
__________________
Always remember you're unique, just like everyone else.
 

The Following 3 Users Say Thank You to onethreealpha For This Useful Post:
Posts: 560 | Thanked: 422 times | Joined on Mar 2011
#18
@nicholes
Just to reiterate: your initial code is mostly C, not C++. To write C++, use the correct libraries (iostream in this case) and C++ functions e.g. std::cout in place of printf.

I might do things slightly differntly to the above two suggestions...

re: Captwheeto's code
  • main() ends with return 0 but doesn't define a return type, thus won't compile.
  • It's better style (safer) to put the using inside the scope it's being used; in this case move it inside main() or drop it altogether and use std:: because the programme's so short.
  • There's no need to add \n at the end of each string. Using " at the beginning and end of a string turns it into a std::string.

re: onethreealpha' code
  • The programme will run but might do so without the user being able to see the output. It is advisable to put something before main returns such as char anyLetter; std::cin>>anyLetter; Or system("PAUSE") if the compiler supports it.

Code:
#include <iostream>		// use correct i.e. C++ library

int main()			// declare return type
{
	using namespace std;	// do this inside scope

	cout << "hello, world";	// self-terminating string

	system("PAUSE");	// requires user input (keypress) to quit

	return 0;		// return an int
}
Look at this book, it really helped me:
http://www.acceleratedcpp.com/
You might think it's a bit old but it's really good for the basics, especially using the standard template library.

Last edited by demolition; 2011-06-16 at 13:17. Reason: typo!
 

The Following 2 Users Say Thank You to demolition For This Useful Post:
SubCore's Avatar
Posts: 850 | Thanked: 626 times | Joined on Sep 2009 @ Vienna, Austria
#19
Originally Posted by demolition View Post
Or system("PAUSE") if the compiler supports it.
system() is evil. case in point: "pause" is a windows/dos command, and won't work here.
__________________
"What we perceive is not nature itself, but nature exposed to our method of questioning."
-- Werner Karl Heisenberg
 

The Following 2 Users Say Thank You to SubCore For This Useful Post:
Posts: 673 | Thanked: 856 times | Joined on Mar 2006
#20
I assume you are trying DOS/WIndows based c++ example.

Please note that getch does not exist in gnu linux.

As someone pointed out, you need to use getchar, but prior to that you have to modify the terminal settings (from within code).

The functioning of getchar depends on terminal settings in order to emulate getch. By default it returns no matter what/if the key was pressed.

See the manual termios.h and tcgetattr.
 

The Following User Says Thank You to momcilo For This Useful Post:
Reply

Tags
i am sleeping


 
Forum Jump


All times are GMT. The time now is 19:45.