Results 1 to 10 of 10
Code:
#include <stdio.h>
main()
{
int x,y,b;
x = 15;
y = 10;
b = x*y;
printf(b);
}
how to display ? would i use printf?
would i use return ...
- 09-03-2010 #1Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
how to print the total of 2 numbers?
how to display ? would i use printf?Code:#include <stdio.h> main() { int x,y,b; x = 15; y = 10; b = x*y; printf(b); }
would i use return b;?
and does any one recommend a good book for linux C++ programming what ive noticed is that C++ windows its a bit different like the #include <iostream> is non existent in linux or am i missing something.Last edited by donaldfarkas; 09-03-2010 at 01:08 AM.
- 09-03-2010 #2Just Joined!
- Join Date
- Sep 2010
- Posts
- 1
a.) Ask questions like this in the linux programming section. People here will generally get upset with questions that are about programming and not linux but....... since your asking about programming in linux.
use this:
#include <stdio.h>
main(int argc, char** argv) // Don't worry about the meaning of this yet
{
int x,y,b;
x = 15;
y = 10;
b = x*y;
printf("%d\n", b);
return 0; // Don't worry about the meaning of this yet either
}
this is a c program by the way but Im sure you already knew that.
b.) Get the book "C++ How to Program" by Deitel and Deitel. Its expensive but if you are serious its a minimal investment.
- 09-03-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,970
This is C code, not C++. Try this:
Note that with C++ and newer versions of C, main() must return an integer. Also, not declaring a return value for a function (including main()) implies an integer return value.Code:#include <iostream> int main(void) { int x, y, b; x = 15; y = 10; b = x*y; cout << b << endl; return 0; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-03-2010 #4Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
when i type in the above code it wotn work i tried this already.Code:[Donald@localhost ~]$ cd Desktop/C++ [Donald@localhost C++]$ make test cc test.c -o test test.c:1:20: error: iostream: No such file or directory test.c: In function ‘main’: test.c:9: error: ‘cout’ undeclared (first use in this function) test.c:9: error: (Each undeclared identifier is reported only once test.c:9: error: for each function it appears in.) test.c:9: error: ‘end1’ undeclared (first use in this function) make: *** [test] Error 1
- 09-03-2010 #5Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
- 09-03-2010 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,970
It seems you don't have the c++ compiler installed, or you need to rename your source file from test.c to test.cpp so make will know to use the c++ compiler. The C compiler doesn't know about C++ stuff like <iostream>, etc.
As for your question about the arguments passed to main(), "int argc" and "char** argv", these are passed to your program when you have arguments on the command line. If your program doesn't take any arguments, then you can use the void argument instead. Also, it is better to use "const char** argv" since you probably don't want to be able to modify the contents of the arguments directly. So, argc is the number of arguments passed to the program from the command line, including the command itself, and argv is an array of strings that contains the actual arguments themselves. So, if you executed the command "test a b c", argc would equal 4 and argv would be an array that looks like this:
const char* argv[4] = { "test", "a", "b", "c" };
As a result, you can process your arguments like this:
for (int i = 1; i < argc; i++)
{
cout << "arg[" << i << "] == " << argv[i] << endl;
}Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-03-2010 #7Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
changed the test file to test.cpp
[Donald@localhost ~]$ cd Desktop/C++
[Donald@localhost C++]$ make test
g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:9: error: ‘cout’ was not declared in this scope
test.cpp:9: error: ‘end1’ was not declared in this scope
make: *** [test] Error 1
this is ther error iget when i try to compile
- 09-03-2010 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,970
1. It is endl (with a letter ell) not end1 (number 1).
2. It may may be that you need to include the standard namespace, so try this:
Code:#include <iostream> using namespace std; int main( int argc, const char** argv) . . .
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-03-2010 #9Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
yea i had number 1 there and also i didnt have using namespsace std;
- 09-03-2010 #10Linux Newbie
- Join Date
- May 2009
- Location
- Kitchener, Ontario, Canada
- Posts
- 187
i managed to land a digital copy without denting a big whole in my wallet, anyway i got the book and im gonna start working on it the problem is the tutorials are in chm format so i gotta find a way linux can read it the the tutorial program files are cpp so no problem there
found a chm viewerLast edited by donaldfarkas; 09-03-2010 at 07:32 PM.


Reply With Quote
