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)

nicholes 2011-06-15 16:20

[solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
1 Attachment(s)
Here i have done a few things on my N900 from

http://ossguy.com/?p=475

to run .cpp files

and when i try to run this using g++ it shows error

here is my programme

# include <stdio.h>
# include <conio.h>
int main (void)
{
printf ("hlloe wolrd");
getch();

}

UPDATE: Thanks to all of you guys i managed to run and also modify .cpp files now (i am using filebox along with the leafpad to edit .cpp)

Captwheeto 2011-06-15 16:27

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
I've never touched C++ but I have done a little C.
Is there any reason why you're using them headers?
You should only need stdio, like so


#include <stdio.h>

int main(void)
{
printf("hello, world\n");
return 0;
}

unless you were planning to expand. Were you trying to get stdin from the user? I'm Perl/Haskell guy. Sorry I can't be of more help

attila77 2011-06-15 16:30

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

Originally Posted by nicholes (Post 1029733)
Here i have done a few things on my N900 from

http://ossguy.com/?p=475

to run .cpp files

and when i try to run this using g++ it shows error

here is my programme

# include <stdio.h>
# include <conio.h>
int main (void)
{
printf ("hlloe wolrd");
getch();

}

Any particular reason for not using the QtSDK but go for on-device compile ?

nicholes 2011-06-15 16:36

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

Originally Posted by Captwheeto (Post 1029737)
I've never touched C++ but I have done a little C.
Is there any reason why you're using them headers?
You should only need stdio, like so


#include <stdio.h>

int main(void)
{
printf("hello, world\n");
return 0;
}

unless you were planning to expand. Were you trying to get stdin from the user? I'm Perl/Haskell guy. Sorry I can't be of more help

i use your methode (using my pc dev c++) but it does nothig a cmd flashes and gone nothing happand

also i am using <conio.h> header file to use getch() for "heelo wolrd" to remain on the screen

nicholes 2011-06-15 16:39

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

Originally Posted by attila77 (Post 1029738)
Any particular reason for not using the QtSDK but go for on-device compile ?

i am learning in my institute and there is no qtsdk but a very older (turboc++ installed in a pc)

i wont to show them all THE POWER OF N900!

SubCore 2011-06-15 16:50

Re: (.cpp)"Hello world" in n900 (getting error) plz guide me.
 
you need to tell g++ where to look for conio.h, usually via the -L switch.

but google tells me that conio.h is a very old msdos library, i doubt it'll work in this environment. try something like "cin.get()" (and #include <iostream>)

StefanL 2011-06-15 16:55

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

Originally Posted by nicholes (Post 1029733)
Here i have done a few things on my N900 from

http://ossguy.com/?p=475

to run .cpp files

and when i try to run this using g++ it shows error

here is my programme

# include <stdio.h>
# include <conio.h>
int main (void)
{
printf ("hlloe wolrd");
getch();

}

Your problem is using a non-standard header file and function (google getch():rolleyes:), this was invented for DOS and only supported by certain compilers. Try using one of the standard input functions and get rid of conio.h if you want to compile it on the N900. ;)

nicholes 2011-06-15 16:56

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.

but google tells me that conio.h is a very old msdos library, i doubt it'll work in this environment. try something like "cin.get()" (and #include <iostream>)

i know but my my programme don't work without conio.h (a cmd flashes only)any other method to do this (i am new in coding)

nicholes 2011-06-15 16:58

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

Originally Posted by StefanL (Post 1029754)
Your problem is using a non-standard header file and function (google getch():rolleyes:), this was invented for DOS and only supported by certain compilers. Try using one of the standard input functions and get rid of conio.h if you want to compile it on the N900. ;)

can you give an example here for me how to get rid of conio.h and getch():)?

StefanL 2011-06-15 17:01

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():)?

Dude, I am not a C programmer, but using googles for getch pops a number of websites with the answer. Sorry can't help you any more (is like the horse and the water thingy;)).

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.

demolition 2011-06-16 13:14

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

Originally Posted by SubCore (Post 1030193)
system() is evil. case in point: "pause" is a windows/dos command, and won't work here.

It is useful in simple cases so one can actually see the console output before it vanishes. Though, didn't know "pause" wasn't possible outside windows/dos.

These will work.

Simple way (but might not be visible)
Code:

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

int main()                        // declare f'n name & return type
{
        using namespace std;        // do this inside scope

        cout << "hello, world";        // self-terminating string
        cout << endl;                // output a line end

        return 0;                // return an int - quit programme
}

If the console disappears too quickly, this version should help:
Code:

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

int main()                        // declare f'n name & return type
{
        using namespace std;        // do this inside scope

        cout << "hello, world";        // self-terminating string
        cout << endl;                // output a line end

        // get console to wait for user input...
        cout << "Press any letter then [Enter]";
        cout << std::endl;        // end line after user instruction
        char ch;                // temporary variable
        cin >> ch;                // store letter
        cout << ch;                // do something with it

        return 0;                // return an int - quit programme
}

Never spent so much time on the first programme! Guess making things multi-system has to start at the beginning....

nicholes 2011-06-16 13:16

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

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

well there are too many programmes i have got in the thread(thanks to all of you genus guys)

since i am very very new in coding so i want to know what to type in xterminal after this (see image)

i have got a.out in home/ user/
(see my first post to know what i have installed on n900 to run .cpp)
(i myself not clear what i have done! lol)

SubCore 2011-06-16 13:27

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

Originally Posted by nicholes (Post 1030238)
since i am very very new in coding so i want to know what to type in xterminal after this (see image)

since you have a.out in /home/user, which is a proper linux filesystem (ext2), type
Code:

chmod +x a.out
to make it executable. then you can run it by
Code:

./a.out
note the ./ at the beginning, this tells your shell to look in the current directory (which, contrary to windows systems, is NOT part of the $PATH variable).

nicholes 2011-06-16 13:32

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

Originally Posted by SubCore (Post 1030242)
since you have a.out in /home/user, which is a proper linux filesystem (ext2), type
Code:

chmod +x a.out
to make it executable. then you can run it by
Code:

./a.out
note the ./ at the beginning, this tells your shell to look in the current directory (which, contrary to windows systems, is NOT part of the $PATH variable).

thanks it did work for me

hello world apeared thanks to all

CHEERS!!!!!!!!:)

StefanL 2011-06-16 13:54

Re: [solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Not being a real C or C++ programmer, should the logic of the program not be something like:

While no user input -> Hello World.

:confused:

demolition 2011-06-16 14:44

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

Originally Posted by StefanL (Post 1030261)
Not being a real C or C++ programmer, should the logic of the program not be something like:

While no user input -> Hello World.

:confused:

No. The sole purpose of this programme is to produce an output i.e. "hello, world". Nothing else. The only user input should be to run the programme, that's it. The slight glitch can be that the console disappears so fast the output cannot be read hence my alternate version, which does require user input.

The entry point of a C++ programme is main(), or pseudo-name thereof. The first command in main(), for this programme, is to stream a string to the standard output (console). The next is to return i.e. to end.

In more complex programmes one might want {while(no user input){action}}, but here it's really simple. The other reason for saying No is in C++ while is a loop, so your logic would produce many, many "hello, world" s. However, only one is required.

StefanL 2011-06-16 18:12

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

Originally Posted by demolition (Post 1030290)
No. The sole purpose of this programme is to produce an output i.e. "hello, world". Nothing else. The only user input should be to run the programme, that's it. The slight glitch can be that the console disappears so fast the output cannot be read hence my alternate version, which does require user input.

The entry point of a C++ programme is main(), or pseudo-name thereof. The first command in main(), for this programme, is to stream a string to the standard output (console). The next is to return i.e. to end.

In more complex programmes one might want {while(no user input){action}}, but here it's really simple. The other reason for saying No is in C++ while is a loop, so your logic would produce many, many "hello, world" s. However, only one is required.

Thanks, I was thinking along of the lines of introducing the OP to event driven programming rather than linear programming, which I thought is more relevant today. Also the next logical step in any extension to this programming excercise would be to echo user input, which then just needs a user input processing routine in the loop. But there is really no need to get ahead of the game at this point. :)

I do agree that the linear programming does the job probably with the smallest effort and tightest code.

nicholes 2011-06-17 09:24

Re: [solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
1 Attachment(s)
One more qustion
#include <iostream>


int main()
{
int s; //salry

printf("enter your salary");
scanf ("%d",&s);
if (s>=1000)
{
if (s>=3000)
{
if (s>=5000)
printf ("your bonus is 500");

else
printf("your bonus is300");
}
else
printf("your bonus is100");
}
else
printf("no bonus");
system("PAUSE");
}



it execute and works perfact but
sh:pause not found comes!! Any way to get rid of it

demolition 2011-06-17 10:00

Re: [solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Now, you really need to stop using C standard functions, like printf and scanf. The C++ equivalents make the code easier to read and are *just as well implemented* in the STL (standard template library). C code will normally compile because C++ is a superset of C and some software, e.g. win32, hasn't been fully converted to C++ yet.

Next, as mentioned above, system("pause") doesn't work outside dos/windows. If your programme just runs and exits without you being able to see the output try my v2. It seems you are very new to programming overall - no worries and well done for giving it a go.

Amongst other paradigms, Encapsulation and Abstraction are part of the bedrock of C++, as an OO language. In programming, encapsulation means wrapping functionality together. And, abstraction means removing or separating one bit of functionality from another.

I will edit this post in a minute with the C++ version of your code.

ps - any chance you can either start a new thread or remove the Solved if you've got more questions. Bit misleading otherwise.

Captwheeto 2011-06-17 10:48

Re: [solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Yeah I said this earlier. You're writing just plain C in a very procedural manner, I think you need to look for a better C++ resource, one that is aimed at linux or uses standard headers, operators and such.

You could learn C if you like, I would recommend it over C++, but if you're looking for a career in programming then stick with OO languages like Python, C++ or Java

onethreealpha 2011-06-17 11:08

Re: [solved](.cpp)"Hello world" in n900 (getting error) plz guide me.
 
Good beginners website HERE if you're interested in C++

i found it great when I started learning C++

nicholes 2011-06-17 11:21

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

Originally Posted by demolition (Post 1030734)
Now, you really need to stop using C standard functions, like printf and scanf. The C++ equivalents make the code easier to read and are *just as well implemented* in the STL (standard template library). C code will normally compile because C++ is a superset of C and some software, e.g. win32, hasn't been fully converted to C++ yet.

Next, as mentioned above, system("pause") doesn't work outside dos/windows. If your programme just runs and exits without you being able to see the output try my v2. It seems you are very new to programming overall - no worries and well done for giving it a go.

Amongst other paradigms, Encapsulation and Abstraction are part of the bedrock of C++, as an OO language. In programming, encapsulation means wrapping functionality together. And, abstraction means removing or separating one bit of functionality from another.

I will edit this post in a minute with the C++ version of your code.

ps - any chance you can either start a new thread or remove the Solved if you've got more questions. Bit misleading otherwise.

well since my teacher is teaching me only C not C++ for now and,i have been told to learn C before starting c++ to learn basics of codes etc. so i would programme under C only for now but later i would expand....

also my problem is solved by execute the .cpp so i changed the thread title by "[solved]" and for small questions i don't want to start a new thread and become a part of spam!!(no offence plz)

again Thanks to all of you!

lfcobra 2011-06-17 11:22

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

Originally Posted by nicholes (Post 1030721)
One more qustion
#include <iostream>


int main()
{
int s; //salry

printf("enter your salary");
scanf ("%d",&s);
if (s>=1000)
{
if (s>=3000)
{
if (s>=5000)
printf ("your bonus is 500");

else
printf("your bonus is300");
}
else
printf("your bonus is100");
}
else
printf("no bonus");
system("PAUSE");
}

just a quick couple of tips. As previously stated try to use the actual c++ functions instead of the c funtions. They will make the code much easier to read, and also avoid unnecessary code complexity with unneeded nesting

Code:

#include <iostream.h>       
#include <stdio.h>

int main(){
using namespace std;
int s; //salary

cout << "Enter your salary: " ;
cin >> s;

if (s>=5000)
  cout << "Your bonus is 500";
else if (s>=3000)
  cout << "Your bonus is 300";
else if (s>=1000)
  cout << "Your bonus is 100";
else
  cout << "No bonus";
getchar();

return 0;
}

note: this code should work but i didnt get a chance to compile it.

edit: just saw your post about being taught C so you can disregard my comment about using c++ functions :)

SubCore 2011-07-08 17:34

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

Originally Posted by nicholes (Post 1030774)
i have been told to learn C before starting c++ to learn basics of codes etc

that's not a good idea. it's a waste of time actually: Should I learn C before I learn OO/C++?


All times are GMT. The time now is 01:08.

vBulletin® Version 3.8.8