C++ Question very basic question

Max Pain

Member
Joined
Sep 2, 2008
Messages
99
Reaction score
0
Points
6
I'm about to learn the very basic of c++ with simple command line programs. I have written this programm from a tutorial:

Code:
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    int ivar;
    double dvar;

    ivar = 100;

    dvar = 100.0;

    cout << "Original value of ivar: " << ivar << "\n";
    cout << "Original value of dvar: " << dvar << "\n";

    cout << "\n";

    ivar = ivar / 3;
    dvar = dvar / 3.0;

    cout << "ivar after division: " << ivar << "\n";
    cout << "dvar after division: " << ivar << "\n";

    return 0;
}
The purpose of this programm is to see the difference between int and double.

Its output should be:

Code:
Original value of ivar: 100  
Original value of dvar: 100   

ivar after division: 33  
dvar after division: 33.3333
But I get:

Code:
ivar after division: 33  
dvar after division: 33
BTW I use Visual Studio Express 2008 and Vista Home Premium. I also use . as decimal comma.

I'm sorry if this question is inappropiate for this forum since it has nothing to do with orbiter (despite of the fact that I learn c++ for orbiter). If you know a good c++ forum where this question is more appropiate, then please link me to it.

Thanks
 
In your own code:
Code:
 cout << "ivar after division: " << ivar << "n";
 cout << "dvar after division: " << ivar << "n";
Are you absolutely sure this typo is not in the code you run?
 
This is how it stands in the tutorial. And if I give the variable other names, like ivar1 and dvar1 for the results, it doesn't change anything.
 
It's a typo in the tutorial then. There is no possibility it would output the same value differently. There should be dvar in the second line.
 
This is how it stands in the tutorial. And if I give the variable other names, like ivar1 and dvar1 for the results, it doesn't change anything.

No. What Artlav means is, that you have two identical lines for the final output. Both times, you print the int value.
 
Sorry, I get the mistake.:blush: Ok I should have seen this for myself.

Anyway I don't want to bother an Orbiter forum with totally unrelated c++ questions. Since there will surely arise new questions during the learning process, I would be glad if somebody links me to a good c++ forum, which answers as quick as the orbiter forum and is as reliable.
 
Don't worry about the errors you make. I'm in a C# course (similar to C++) and I make stupid errors all the time (most of them are the compiler's fault!) and just mentally smack myself and move on. It gets irritating, but it's a part of coding.
 
Back
Top