Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> My First C++ Program, very basic
electriic ink
post Mar 15 2006, 08:35 PM
Post #1


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,200
Joined: 11-February 05
From: Heaven
Member No.: 3,709



Okay, well today I've started learning C++ and after about 30 minutes I've managed to compile my first program smile.gif

All it does is prompt you to type a number in between 1 and 10. If it's not in that range, it adds/subtracts until it is. The best part is you get to see it add/subtract the numbers up one-by-one!

Code:

CODE
#include <iostream>

using namespace std;


int main() {

// Define variables

int anumber;

cout<<"Please enter a number between 1 and 10";
cin>> anumber;
cin.ignore();

if (anumber > 10) {

cout<<"\nNumber above 10!\n\n";

} else if (anumber < 1) {

cout<<"\nNumber below 1!\n\n";

} else {

cout<<"\nYou entered "<< anumber <<" which is between 1 and 10\n\n";

}

if (anumber > 10) {

cout<<"Decreasing number entered to 10....\n\n";

for (int a = anumber; a > 10; a--) {

cout<< a <<"\n";

}

cout<<"10\n\n";

} else if (anumber < 1) {

cout<<"Increasing number entered to 1....\n\n";

for (int b = anumber; b < 1; b++) {

cout<< b <<"\n";

}

cout<<"1\n\n";

}



cin.get();

}


Download (454kb):

http://www.iambored.info/project.exe

PS: Learning C++ seems so far so much easier if you know php
Go to the top of the page
 
+Quote Post
awild
post Mar 21 2006, 05:23 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 10
Joined: 21-March 06
Member No.: 20,449



QUOTE(electriic ink @ Mar 16 2006, 03:35 AM) *

Okay, well today I've started learning C++ and after about 30 minutes I've managed to compile my first program smile.gif

All it does is prompt you to type a number in between 1 and 10. If it's not in that range, it adds/subtracts until it is. The best part is you get to see it add/subtract the numbers up one-by-one!

Code:

CODE
#include <iostream>

using namespace std;


int main() {

// Define variables

int anumber;

cout<<"Please enter a number between 1 and 10";
cin>> anumber;
cin.ignore();

if (anumber > 10) {

cout<<"\nNumber above 10!\n\n";

} else if (anumber < 1) {

cout<<"\nNumber below 1!\n\n";

} else {

cout<<"\nYou entered "<< anumber <<" which is between 1 and 10\n\n";

}

if (anumber > 10) {

cout<<"Decreasing number entered to 10....\n\n";

for (int a = anumber; a > 10; a--) {

cout<< a <<"\n";

}

cout<<"10\n\n";

} else if (anumber < 1) {

cout<<"Increasing number entered to 1....\n\n";

for (int b = anumber; b < 1; b++) {

cout<< b <<"\n";

}

cout<<"1\n\n";

}



cin.get();

}


Download (454kb):

http://www.iambored.info/project.exe

PS: Learning C++ seems so far so much easier if you know php


it's simple right?

i think C++ is basic programing language..if u know it, other language will be easy..
Go to the top of the page
 
+Quote Post
bidarshi
post Mar 23 2006, 11:11 AM
Post #3


Newbie
*

Group: Members
Posts: 8
Joined: 23-March 06
Member No.: 20,552



Let me join discussing this topic and add continuation to this thread. C++ is an easy language to learn. And it is true that programming is often considered to be an art. The idea of efficient programming is to master the logic flow and designing according to th requirement of the problem or client.C++ is a structured language which is a super set of the C language. Many new features are added to the classical C to evolve itself into C++. To learn C++ or any other programming language one must master logic flow concept and the basics of the programming language syntax.Be it C++ or Java or PHP programming is never difficult. to learn concept of structured language C++ is a good start while C is the classical procedural language.
Go to the top of the page
 
+Quote Post
kl223
post May 9 2006, 02:11 PM
Post #4


Newbie
*

Group: Members
Posts: 5
Joined: 8-May 06
Member No.: 23,256



electriic ink: You posted this topic a while ago, so you're a bit more in C++ by now, I think.
But there is a little thing that's itching me to correct:

Just in order to be more elegant, you should use instead of this:
QUOTE
for (int a = anumber; a > 10; a--) {
cout<< a <<"\n";
}
cout<<"10\n\n";


this:

QUOTE
for (int a = anumber; a >= 10; a--) {
cout<< a <<"\n";
}


That way, you don't have to put that cout-line. smile.gif It's the easiest way to interpret for if the standard definition looks like:

CODE
for ( stat1; stat2; stat3 ) { ... }


is equal to:

CODE
stat1;
while ( stat2 ) {
   ...
   stat3;
}


smile.gif

kl223
Go to the top of the page
 
+Quote Post
fffanatics
post May 9 2006, 02:41 PM
Post #5


Privileged Member
*********

Group: [HOSTED]
Posts: 937
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



I agree with kl223. It is just a better practice. Also, about ur comment about how c++ is easier to learn than php, they are very similar. PHP just added a few features that make it easier to interact with a database and such that make it seem harder. However, if you know one, you will be fine programming in the other. Also, once you know one language that is a higher level language like C++ or Java, it is very easy to use the other one since all the concepts are the same just different words and a few other syntax differences.
Go to the top of the page
 
+Quote Post
kl223
post May 10 2006, 06:45 PM
Post #6


Newbie
*

Group: Members
Posts: 5
Joined: 8-May 06
Member No.: 23,256



QUOTE(fffanatics @ May 9 2006, 04:41 PM) *

I agree with kl223. It is just a better practice. Also, about ur comment about how c++ is easier to learn than php, they are very similar. PHP just added a few features that make it easier to interact with a database and such that make it seem harder. However, if you know one, you will be fine programming in the other. Also, once you know one language that is a higher level language like C++ or Java, it is very easy to use the other one since all the concepts are the same just different words and a few other syntax differences.



It IS true that you can learn a programming language after you know another one.
However, there are significant differences beetween C++ and PHP.
I would say, basically the only similarity is that both are able to access network. smile.gif

PHP is a script-language. Being it, it's interpreted instead of compiled. That means, you'll never see the binary from the php source you've written. It also has the usual improvements that a normal script-language does. For example: no need for explicit variable type declaration; runtime evaluation of a string into code; easily written self-modifier programs; etc.
It's meant to be a server-side scripting language that uses databases/etc and generates HTML output for the user.
Of course there are other ways of use PHP, too. There are for example bittorrent trackers written in PHP or there is a way to use php as a client-side script, just like bash, python, perl, etc.

In the opposite, C++ is a precompiled-language. That means, you cannot do anything more with the source than to compile 'em. In C++, every variable has its own well-defined type, and it cannot be changed during the execution of the program.
It is also capable of getting used as server-side CGI script, but it's not that popular. Not even that supported, either. C++ is alot faster in most cases and it is just like C or Java. It's not similar to a scripting language.

So, I believe C++ cannot help too much more in PHP programming than any other programming language.
(ps: even if it's not true the other way around. Since php is written in C/C++, you can look into the source of the php interpreter and copy-paste functions if you like. Some of them are really interesting. biggrin.gif )

kl223
Go to the top of the page
 
+Quote Post
Dark_Prisoner
post Feb 27 2008, 06:35 PM
Post #7


Newbie [Level 1]
*

Group: Members
Posts: 22
Joined: 20-February 08
Member No.: 58,158



Good work for a beginner wink.gif
Any way you was able to put each procedures running in 2 if() conditions having the same condition int the same if()
*And there is a word replacing "\n" in the C++ language which is : endl = End line . I think it is easier to be read .
*Why don't you replace :
CODE
cin.get();

by
CODE
system("PAUSE");

which shows you a beautiful line in the console
QUOTE
("Press any key to continue")

*Always return a 0 status from you program , in fact i always use :
CODE
return EXIT_SUCCESS
which means that the program encountered no problem .

->After modifications you code will be like this : (Your actual code is correct but it will be more ... Professional ? )

CODE
#include

using namespace std;


int main()
{

[indent]// Define variables
int anumber;

cout<<"Please enter a number between 1 and 10";
cin>> anumber;
cout<

if (anumber > 10)
{

[indent]cout<<"Number above 10!";
cout<<< endl;

cout<<"Decreasing number entered to 10....";
cout<<< endl;

for (int a = anumber; a >= 10; a--)
{

[indent]cout<< a <<endl;
[/indent]
}

[/indent]}
else if (anumber < 1)
{

[indent]cout<<"Number below 1!";
cout<<< endl;

cout<<"Increasing number entered to 1....";
cout<<< endl;

for (int b = anumber; b <= 1; b++)
{
[indent]cout<< b <<endl;
[/indent]}

[/indent]}
else
{

[indent]cout<<"You entered "<< anumber <<" which is between 1 and 10";
cout<<< endl;
[/indent]}
cout<<< endl;

system("PAUSE");
return EXIT_SUCCESS;
[/indent]
}

Now what do you think ?

Notice from rvalkass:

Your code needs to have Code tags around it. Added them.


This post has been edited by rvalkass: Feb 27 2008, 07:35 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. What Program Do You Use To Design Your Web?(226)
  2. Auto Run Java Program(11)
  3. Need Help With My Python Programs(11)
  4. [tutorial] Visual Basic 6(2)
  5. [tutorial] Visual Basic 6 Minimize To Tray(4)
  6. Make Anty-spyware Program In Delphi 7 ?(6)
  7. Crazy Looking C Program(19)
  8. The Best Banner Program(8)
  9. I Need A Gfx Program(6)
  10. Are You In Search Of Free Google Cash Program ?(10)
  11. Visual Basic 6.0 Help Needed(13)
  12. How To Make A Web Browser(48)
  13. Need Help With C Program To Test If A Number Is Prime(12)
  14. Executing An Exe From Within A Program!(7)
  15. Simple C File Handling In Action(3)
  1. My Program, Test It Please(5)
  2. Get Paid 4 Cool Surveys Games Affiliates & Offers 3 Sign Up Great Program(4)
  3. Help! Php Or Just Html?(13)
  4. What Program Do You Use To Design Your Web?(82)
  5. Some Basic But Important Info About Cancer(3)
  6. Ti-basic --- Slot Machine(9)
  7. Ti Basic: Pick A Number(1)
  8. A Twist On Basic Authentification(1)
  9. Program List(4)
  10. Buliding A Basic Community Site(2)
  11. What Is The Program To Join Videos Together?(13)
  12. Basic Of Website Creation(0)
  13. State Of New Mexico Dot(0)