First program

Re: First program

Post by BhaaL on Mon Jul 27, 2009 11:52 am
([msg=27533]see Re: First program[/msg])

STUDIOny wrote:I've been using:

Code: Select all
<< endl;


Ive started to use \n

You might want to use endl if you plan to write portable code, since it becomes \n on *nix, \r on mac os and \r\n on windows if i'm not mistaken.

STUDIOny wrote:but, where would I include the \n in the following code?

Code: Select all
cout << "First we will add: " << tempInt1 << " + " << tempInt2 << " = " << total1;

Wherever you want it (and would have put the \n):
Code: Select all
cout << "First we will add: " << tempInt1 << " + " << tempInt2 << " = " << total1 << endl;

to make it "First we will add: 1 + 2 = 3<newline here>
".
Or probably
Code: Select all
cout << "First we will add: " << endl << tempInt1 << " + " << tempInt2 << " = " << total1 << endl;

to make it "First we will add: <newline here>
1 + 2 = 3<newline here>
".
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Mon Jul 27, 2009 12:22 pm
([msg=27536]see Re: First program[/msg])

Ill use endl then.
From the dude on the first page, he seemed to think I should be using \n

Anyway, one more question, what is the advange of using printf?
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by BhaaL on Mon Jul 27, 2009 12:44 pm
([msg=27538]see Re: First program[/msg])

Real advantage, formatting. Individual advantage, preference.
You can do easy things like this with printf:
Code: Select all
printf("Number in hex, at eight digits: 0x%08x.\n", 4919);

while C++ and ostream-modifiers look like that:
Code: Select all
std::cout << "Number in hex, at eight digits: 0x" << std::hex << std::setw(8) << std::setfill('0') << 4919 << "." << std::endl;

Just a matter of preference, yet the latter can become ugly and annoying the more you want to print.

You could have done C instead if you wanted ;)
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Mon Jul 27, 2009 12:58 pm
([msg=27539]see Re: First program[/msg])

Thanks man, Ill have a go with printf ;]

I think I would just feel a bit weird going and learning C, when I've just started to learn C++
I figure I should just stick to what I have started, but I don't know...

EDIT: Changed my code to the below, looking better?

Code: Select all
#include <iostream>
#include <stdio.h>

using namespace std;

int main( void )

{

int tempInt1 = 1;
int tempInt2 = 2;
int tempInt3 = 3;
int total1 = tempInt1 + tempInt2;
int total2 = total1 * tempInt3;

    printf("Hello World!\n");
    printf("We are going to be adding a few numbers:\n");
    printf("First we will add: %i + %i = %i\n", tempInt1, tempInt2, total1);
    printf("Next, we will multiply %i * %i = %i\n", total1, tempInt3, total2);
    printf("Goodbye world!");

    return 0;

}
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by BhaaL on Mon Jul 27, 2009 2:37 pm
([msg=27545]see Re: First program[/msg])

Remove the #include <iostream> and the using namespace std; and you have C.
C++ just builds on top of C, and adds object orientedness. You might be better off using/trying to learn C first if you have no idea about OOP and the sorts, to learn procedural style first.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Mon Jul 27, 2009 3:36 pm
([msg=27547]see Re: First program[/msg])

Yes, I have no prior knowledge of programming, so, when it comes to OOP, I have just read bits and bobs about it in general, but programming in OOP, I don't have a clue.
If C is basically what I have just written, I don't see it being much of a hassle going to it, from what I already know from c++, being nearly the same.

Ill give it a look..
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by tgoe on Mon Jul 27, 2009 8:45 pm
([msg=27560]see Re: First program[/msg])

I think the gripe about endl is performance.
I just did some crude testing with these programs

Code: Select all
/* one.c */
#include <stdio.h>

int main(void) {
    int i;
    for(i = 0; i != 10000; i++) {
        printf("Imagine\nyou're\ndoing\nlots\nof\noutput.\n");
    }

    return 0;
}

Code: Select all
// two.cpp
#include <iostream>

using namespace std;

int main(void) {
    int i;
    for(i = 0; i != 10000; i++) {
        cout << "Imagine" << endl << "you're" << endl << "doing" <<
             endl << "lots" << endl << "of" << endl << "output." << endl;
    }

    return 0;
}


by timing their output redirected to separate files. The C version is instantaneous. The C++ version takes about 1/5 of a second. Turns out that endl does an implicit buffer flush each time (std::flush()?). So the above C++ is more like this C:

Code: Select all
#include <stdio.h>

int main(void) {
    int i;
    for(i = 0; i != 10000; i++) {
        printf("Imagine\n");
        fflush(stdout);
        printf("you're\n");
        fflush(stdout);
        printf("doing\n");
        fflush(stdout);
        printf("lots\n");
        fflush(stdout);
        printf("of\n");
        fflush(stdout);
        printf("output.\n");
        fflush(stdout);
    }

    return 0;
}

which is obviously slower and probably not what you intended.
User avatar
tgoe
Contributor
Contributor
 
Posts: 639
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: First program

Post by BhaaL on Tue Jul 28, 2009 12:21 pm
([msg=27592]see Re: First program[/msg])

Output is slow in general. If you ever have performance-critical stuff, I doubt it would/should be printing that much to slow it down even further.
But I doubt performance and optimization are the last things he wants to hear, he's just about to get started.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by tgoe on Wed Jul 29, 2009 3:15 pm
([msg=27638]see Re: First program[/msg])

Agreed. This thread had just gotten me curious :)
User avatar
tgoe
Contributor
Contributor
 
Posts: 639
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Wed Jul 29, 2009 6:24 pm
([msg=27642]see Re: First program[/msg])

To be fair i am looking to learn an much as possible, so what tgoe had posted was helpful.
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Previous

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests