Jump to content
xisto Community
Sign in to follow this  
electriic ink

My First C++ Program very basic

Recommended Posts

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

 

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:

 

#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://forums.xisto.com/no_longer_exists/

 

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

Share this post


Link to post
Share on other sites

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

 

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:

 

#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://forums.xisto.com/no_longer_exists/

 

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..

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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:

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


this:

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


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

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

is equal to:

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

:lol:

kl223

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. :)

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. :) )

kl223

Share this post


Link to post
Share on other sites

Good work for a beginner :P
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 :

cin.get();

by
system("PAUSE");
which shows you a beautiful line in the console

("Press any key to continue")

*Always return a 0 status from you program , in fact i always use :
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 ? )

#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.

Edited by rvalkass (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.