Starting C++. A tutorial. - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Starting C++. A tutorial.

  1. #11
    Member
    Join Date
    Feb 2003
    Posts
    98

    Updating

    I know this is bad, I am rewriting it and expanding it, be patient please. But thanks for the idea's. (I realize I didn't include couting to files. I also am missing a lot of other stuff. I wrote this at 1:00 in the morning so that is a lot of the reason for incompleteness. BIGGER TUTORIAL COMING SOON.) I have debated how I was posting it and have decided to include the New tutorial as a downloadable zipped .txt with .cpp example files.
    \"The wise programmer is told about Tao and follows it. The average programmer is told about Tao and searches for it. The foolish programmer is told about Tao and laughs at it.
    If it were not for laughter, there would be no Tao.\"

  2. #12
    Senior Member
    Join Date
    May 2002
    Posts
    344
    I realize I didn't include couting to files.
    I seriously suggest that you search for tutorials before you decide to write another one. I have already written one about "fouting" to a file. if you care to read it go here: http://www.antionline.com/showthread...hreadid=247364
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  3. #13
    Junior Member
    Join Date
    Jul 2001
    Posts
    8
    Okay here is my question i can do very little programming. I can semi write a program in C++ that inputs 3 test scores and then prints teh avgerage of the two highests scores. But what i can't do is Input 3 values and output one of the following three messaes that best describes the three Values 1. THREE VALUES EQUAL 2. TWO VALUES EQUAL 3. NO VALUES EQUAL. so if someone could kinda of explain how to go about writting the code for a program like this please get back to me thanx a lot

  4. #14
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    hickman: The logicial test would be like so:

    Code:
    if ( (value1 == value2) && (value2 == value3) )
    {
    std::cout << "Three values equal" << std::endl;
    }
    else if ( (value1 == value2) || (value1 == value3) || (value2 == value3) )
    {
    std::cout << "Two values equal" << std::endl;
    }
    else
    {
    std::cout << "No values equal" << std::endl;
    }
    Assuming that value1, value2 and value3 are the three variables that are input by the user of the program (if not then just change those to reflect the variable names you used).

    If you want to check more cases, you'd probably be better using a lnked list and moving through it one element at a time, keeping a running check of the number of matches (perhaps using another linked list) - e.g. if you had 5 values (because otherwise it would get much more complicated using if...else statements).
    Paul Waring - Web site design and development.

  5. #15
    Junior Member
    Join Date
    Oct 2003
    Posts
    14
    Originally posted here by pwaring

    BTW, cout doesn't necessarily mean that some text will be displayed. Yes, in 90% of cases the standard output will be to the console, but it can be redirected to files or other devices. Obviously this doesn't happen in the Hello world type of programs, but it is a point that needs to be emphasised.
    No, cout will always display to the console window, you may be confused with ostream. The difference between cout and ostream is cout is an object while ostream is a type. The ostream type can be used on any particular device while the cout object is initialized to be used for the console.

  6. #16
    Senior Member
    Join Date
    May 2002
    Posts
    344
    No, cout will always display to the console window, you may be confused with ostream. The difference between cout and ostream is cout is an object while ostream is a type. The ostream type can be used on any particular device while the cout object is initialized to be used for the console.
    you tell me...


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
    	ofstream cout; 
    	cout.open("filename.txt");
    	
    	cout << "You can do anything you want with cout :)";
    
            cout.close(); 
    
    	return 0;
    }
    cout will always display to the console window
    Wrong.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  7. #17
    Junior Member
    Join Date
    Oct 2003
    Posts
    14
    Please - he wasn't refering to shadowing the std::cout with a local scoped variable, take your semantics else where.

    Though I'm going to partially-retract my statement because it is possible that cout will not display to a console window. cout is guarenteed to display what ever stdout points to(which can not be changed internally) but can be changed via how you instantiate your program. Such as if you run your program from command line stdout will point to the command line and if you run your program on an embeded device stdout will point to whatever form of output the particular device uses. As for changing what device cout will point to that is not possible from a programmers stand point it is possible only through the platform that the program is run on, making the ostream portable to many platforms(including emebeded devices)

  8. #18
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    Originally posted here by progme


    No, cout will always display to the console window, you may be confused with ostream. The difference between cout and ostream is cout is an object while ostream is a type. The ostream type can be used on any particular device while the cout object is initialized to be used for the console.
    Of course ostream is a type, otherwise you wouldn't be able to create an instance of it. Don't think that I'm confused between the two.

    std::cout will print to standard output, just like std::cin will read from standard input. What those happen to refer to are not necessarily the console, although as I stated before 90% of the time they are (and they will most likely be so for the majority of simple programs). As a programmer, it's not just important to realise what *you* have control over, but also what the user may choose to do with your program. For example, if the hello world program was run as:

    Code:
    ./hello > hello.txt
    under *nix, then the output would not go to the console because it is redirected. That doesn't mean that the programmer can control where std::cout goes (because it is sent to the standard output - wherever that may be set to), but s/he should be aware of the fact that it may be redirected by the user.

    So, in conclusion, cout will not always display to the console window, even if that happens to be the default.
    Paul Waring - Web site design and development.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •