Like Tree2Likes

first step towards C++

This is a discussion on first step towards C++ within the C++ Programming forums, part of the General Programming Boards category; Hi I'm a complete new to this programming world and know nothing about. My aim is to learn to C++ ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    first step towards C++

    Hi

    I'm a complete new to this programming world and know nothing about. My aim is to learn to C++ basic console based programming. I hope you all would kindly help me to do this.

    I copied this program from somewhere:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    void main ()
    {
    clrscr;
    int x;
    cout<<"Enter the number to be Cubed";
    cin>>x;
    cout<<x*x*x<<endl;
    getch();
    }
    I hope there were no errors in the above source code (I think that's what you call it). Please let me tell you again that I am blank illiterate in computer programming and am self learner.

    What do these statements mean at the start: #include <iostream.h>, #include <stdio.h>, and #include <conio.h>?

    I believe in C++ every statement is separated by ";"?

    What's the purpose of writing "getch()" at the end?

    I understand it takes a lot of energy and time to help someone like me. But I would be very much indebted to you for your help and teaching me to write some basic programs. With the hope that it's going to be pleasant and informative learning experience, I await your help.

    Best wishes
    Jack

    PS: I've wrapped the code as suggested by quzah
    Last edited by jackson6612; 03-18-2011 at 12:01 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Instead of using color tags, use code tags.
    [code] ...your code here [/code]

    Just hit edit, then wrap your code up in those. You should get a book on C++ or at least get some tutorials.

    It looks like you are going from some older point of reference, because "iostream.h" is now just "iostream". Anyway, the #include says to include the contents of a file specified in the following angled brackets, or quotes.

    Also conio.h is a pretty old, compiler specific file that lets you do things like color text, or move the cursor to specific spots. That sort of thing.

    The getch at the end is to keep it from closing the window if you run it without opening a command prompt. There is a FAQ on this near the top of the page.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Well, the first step in learning a language would be a good book. I'd recommend Accelerated C++.
    Also, whatever source you got that from, you should avoid. It's non-standard code and also quite old.
    For example, void main should be int main, iostream.h should be iostream, etc.
    phantomotap and iMalc like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, quzah.

    Let's try to write a program to calculate mean of four real numbers (I have read somewhere that you have to be very specific about every detail; so I specifically mentioned "real numbers", not "integers"). The general formula for this mean is: Mean = a+b+c+d/4

    At the top I would write the following (although quzah mentioned that ".h" is not included, however I would use it some time)


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

    void main()
    {


    Then, I have read that you have make declarations about the variable - i.e. what they actually are, are they integers, fractions, etc.). "real numbers" are declared by "float":

    float a, b, c, d, Mean

    Then, from whatever small bit of information I have about C++ I have I think you write the main program after "cin>>".

    The partial code in my opinion would be:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void main ()
    {float a, b, c, d, Mean;
    cin>>
    I humbly request you to help me how to complete this small program. I would like to these lines appear in the input area: "Enter the four numbers", and in the output area I would like this: Mean for a=(whatever value is input), b=(whatever value is input), c=(whatever value is input), d=(whatever value is input) is (whatever output is). Kindly help me with this. It would be nice of you.

    Best wishes
    Jack

    PS: I see Elysia has also replied. Thanks, Elysia. Okay, next time I wouldn't use those things.
    Last edited by jackson6612; 03-18-2011 at 02:43 AM.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,282
    Accelerated C++ is really the only way to go these days.

    [Off Topic]

    O_o

    You know, the "like" is starting to grow on me. I get to say that an answer is good without taking the time to quote or see who posted.




    *tsk*

    +1 Like to the "like" feature.

    [/Off Topic]

    [Edit]
    Is anyone else super impressed with how well this guy has framed his request for help with his homework?

    It is sublime.
    [/Edit]

    [Edit]
    I couldn't help it. This is simply amazing.
    [/Edit]

    Soma

    Code:
    #include <iostream>
    
    int main
    (
        int argc
      , char ** argv
    )
    {
        float a(0.0f);
        float b(0.0f);
        float c(0.0f);
        float d(0.0f);
        std::cout << "enter four numbers: _\b";
        std::cin >> a >> b >> c >> d;
        std::cout << ((a + b + c + d) / 4.0f) << '\n';
        return(0);
    }
    Last edited by phantomotap; 03-18-2011 at 02:49 AM. Reason: none of your business

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    jackson6612, you need a good book to learn the basics of the syntax before you proceed. Follow up on my (and phantomotap's) suggestion on getting Accelerated C++ first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jackson6612 View Post
    Thanks a lot, quzah.

    Let's try to write a program to calculate mean of four real numbers (I have read somewhere that you have to be very specific about every detail; so I specifically mentioned "real numbers", not "integers"). The general formula for this mean is: Mean = a+b+c+d/4

    At the top I would write the following (although quzah mentioned that ".h" is not included, however I would use it some time)


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

    void main()
    {


    Then, I have read that you have make declarations about the variable - i.e. what they actually are, are they integers, fractions, etc.). "real numbers" are declared by "float":

    float a, b, c, d, Mean

    Then, from whatever small bit of information I have about C++ I have I think you write the main program after "cin>>".

    The partial code in my opinion would be:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void main ()
    {float a, b, c, d, Mean;
    cin>>
    I humbly request you to help me how to complete this small program. I would like to these lines appear in the input area: "Enter the four numbers", and in the output area I would like this: Mean for a=(whatever value is input), b=(whatever value is input), c=(whatever value is input), d=(whatever value is input) is (whatever output is). Kindly help me with this. It would be nice of you.

    Best wishes
    Jack

    PS: I see Elysia has also replied. Thanks, Elysia. Okay, next time I wouldn't use those things.
    Okay, I will get that book in next couple of days. But for the present time please help me with some of the problems as the one in the quoted post. Thank you.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,282
    O_o

    LMAO! If you need more help then you've received; I've done no harm.

    Soma

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Code:
    #include <iostream>
    
    int main()
    {
    	float a, b, c, d, Mean;
    	std::cout << "Enter first number: ";
    	std::cin >> a;
    	std::cout << "Enter second number: ";
    	std::cin >> b;
    	std::cout << "Enter third number: ";
    	std::cin >> c;
    	std::cout << "Enter fourth number: ";
    	std::cin >> d;
    	std::cout << "The mean is: " << (a + b + c + d) / 4;
    }
    Here is an example.
    Note that float is a type that takes decimal numbers. If you don't need decimals, you should probably use int.
    std::cout outputs text to screen.
    std::cin inputs text from screen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Code:
    #include <iostream>
    
    int main()
    {
    	float a, b, c, d, Mean;
    	std::cout << "Enter first number: ";
    	std::cin >> a;
    	std::cout << "Enter second number: ";
    	std::cin >> b;
    	std::cout << "Enter third number: ";
    	std::cin >> c;
    	std::cout << "Enter fourth number: ";
    	std::cin >> d;
    	std::cout << "The mean is: " << (a + b + c + d) / 4;
    }
    Thanks a lot, Elysia.

    I see in the case of "std::cin >>" you didn't use quotation marks. Is it always the same? I mean to say quotation marks aren't to be used with "std::cin >>"?

    I compiled and ran your code on Dev-C++ compiler. One code can be run on any compiler, or do there have to be some specific statements in the source code which need to used in order to compile the code on a certain compiler software? I think the compiler my friend uses is an old one because I have seen he used "#include <iostream.h>" and "void main()" instead.

    Please guide me. Thanks.
    Last edited by jackson6612; 03-18-2011 at 03:21 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quotation marks ("") indicates text, a C-style string. If you did
    std::cin >> "a" you would get a compile error because you need to store the input into a variable, and "a" is not a variable.

    All C++ code works across all compilers without any need of additional statements. Of course, there is also language extensions and non-standard code. They do not always work across all compilers.
    For example, void main is non-standard and do not work across all compilers. Iostream.h is also a non-standard header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    Quotation marks ("") indicates text, a C-style string. If you did
    std::cin >> "a" you would get a compile error because you need to store the input into a variable, and "a" is not a variable.

    All C++ code works across all compilers without any need of additional statements. Of course, there is also language extensions and non-standard code. They do not always work across all compilers.
    For example, void main is non-standard and do not work across all compilers. Iostream.h is also a non-standard header.
    Thanks a lot, Elysia. It was very nice of you. By the way, how do I get the list of my threads. I have one thread started by me here but under user CP it says I have "0" threads. Could you please let me know about this?

    Best wishes
    Jackson

    PS: I believe I have to subscribe to the thread started by me to get it in the CP. Most of the forums I have been to have the thread started by you in the CP automatically. Thanks.
    Last edited by jackson6612; 03-20-2011 at 02:47 PM.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    In the CP, there is only subscribed threads. Threads with new notifications.
    You can search for threads started by your own username to see all your threads.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    In the CP, there is only subscribed threads. Threads with new notifications.
    You can search for threads started by your own username to see all your threads.
    Okay. Thanks for the information. I will continue this thread If I had any question(s) - in fact I'm going to have a lot of questions! So, please be there to help me.

    Best wishes
    Jackson

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    Code:
    #include <iostream>
    
    int main()
    {
    	float a, b, c, d, Mean;
    	std::cout << "Enter first number: ";
    	std::cin >> a;
    	std::cout << "Enter second number: ";
    	std::cin >> b;
    	std::cout << "Enter third number: ";
    	std::cin >> c;
    	std::cout << "Enter fourth number: ";
    	std::cin >> d;
    	std::cout << "The mean is: " << (a + b + c + d) / 4;
    }
    1: As you see above every "std::cout" is followed "std::cin", is this possible in some situation where "std::cout" or "std:cin" is followed another "std::cout" or "std::cin" respectively?

    2: I want the line "The mean is:" to appear red and bold? How do I do this?

    3: I have seen my friend use commands "\endl" and "\n" (hope I have them right). What functions do they serve?

    It is very kind of you to help me. I'm much obliged.

    Best wishes
    Jackson

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debugging - step into issue
    By barneygumble742 in forum C++ Programming
    Replies: 7
    Last Post: 03-15-2009, 12:30 PM
  2. recursion
    By paulmedic555 in forum C Programming
    Replies: 26
    Last Post: 01-27-2005, 11:43 PM
  3. robot step sizes
    By n00by in forum C Programming
    Replies: 2
    Last Post: 04-29-2004, 03:29 PM
  4. step by step debug != run debug
    By bonkey in forum Windows Programming
    Replies: 8
    Last Post: 09-09-2002, 12:55 PM
  5. this sites Compiler Resources Specs.
    By Powerfull Army in forum C++ Programming
    Replies: 9
    Last Post: 07-08-2002, 06:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21