maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Nokia N900 (https://talk.maemo.org/forumdisplay.php?f=44)
-   -   [solved](.cpp)"Hello world" in n900 (getting error) plz guide me. (https://talk.maemo.org/showthread.php?t=74021)

slvr32 2011-06-15 17:08

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Quote:

Originally Posted by SubCore (Post 1029753)
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.

nicholes 2011-06-15 17:09

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
1 Attachment(s)
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!

slvr32 2011-06-15 17:11

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Quote:

Originally Posted by nicholes (Post 1029756)
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

nicholes 2011-06-15 17:27

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
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

SubCore 2011-06-16 08:43

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Quote:

Originally Posted by nicholes (Post 1029772)
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.

Captwheeto 2011-06-16 09:24

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
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;       
}


onethreealpha 2011-06-16 10:48

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
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++"

demolition 2011-06-16 11:45

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
@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.

SubCore 2011-06-16 11:54

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Quote:

Originally Posted by demolition (Post 1030186)
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.

momcilo 2011-06-16 12:04

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
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.


All times are GMT. The time now is 06:29.

vBulletin® Version 3.8.8