d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > C++ Homework > Easy One.
Add Reply New Topic New Poll
Member
Posts: 9,972
Joined: Feb 14 2010
Gold: 228,611.95
May 20 2014 01:11pm
Comment and tell us the result when you use this script c++.

Code
#include <iostream.h>
#include <conio.h>
void main()
{
int i,j,n;
i=0;
n=i++;
cout << "A : i= " << i << " n = " << n << endl;
i=10;
n=++i;
cout << "B : i = " << i << " n = " << n << endl;
}
i=20;
j=5;
n=i++ * ++j;
cout << "C : i = " << i << " j = " << j << " n = " << n << endl;
getch();


Thanks,
Member
Posts: 9,085
Joined: Dec 30 2008
Gold: 6,675.00
May 20 2014 03:39pm
when i tried to run it, it failed. i used a shitty compiler though so that might have been the issue.

This post was edited by Barcelona1011 on May 20 2014 03:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 20 2014 05:45pm
i'm not big into C, but iirc you can't execute code outside of your function like that. not to mention that the variables would no longer be in scope afaik.

run the code yourself and see what the output is. you should be able to comment each line by yourself as well.

This post was edited by carteblanche on May 20 2014 05:45pm
Member
Posts: 9,972
Joined: Feb 14 2010
Gold: 228,611.95
May 20 2014 05:59pm
the script code is ok , there is no problem with it , maybe you have to rename the #include <iostream.h> to <iostream> depends on wich compiler you use , well i will do it myself when i am on my mother comp ;)
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
May 21 2014 04:50am
as said earlier that code is scrambled probably because you didn't copy it properly. Anyway here's a working version

Code
#include <iostream>
int main()
{
int i,j,n;
i=0;
n=i++;
std::cout << "A : i = " << i << " n = " << n << std::endl;
i=10;
n=++i;
std::cout << "B : i = " << i << " n = " << n << std::endl;
i=20;
j=5;
n=i++ * ++j;
std::cout << "C : i = " << i << " j = " << j << " n = " << n << std::endl;
return 0;
}


and the output is
Code
A : i = 1 n = 0
B : i = 11 n = 11
C : i = 21 j = 6 n = 120


This post was edited by m0hawk on May 21 2014 04:52am
Member
Posts: 9,972
Joined: Feb 14 2010
Gold: 228,611.95
May 21 2014 07:24am
Quote (m0hawk @ 21 May 2014 12:50)
as said earlier that code is scrambled probably because you didn't copy it properly. Anyway here's a working version

Code
#include <iostream>
int main()
{
int i,j,n;
i=0;
n=i++;
std::cout << "A : i = " << i << " n = " << n << std::endl;
i=10;
n=++i;
std::cout << "B : i = " << i << " n = " << n << std::endl;
i=20;
j=5;
n=i++ * ++j;
std::cout << "C : i = " << i << " j = " << j << " n = " << n << std::endl;
return 0;
}


and the output is
Code
A : i = 1 n = 0
B : i = 11 n = 11
C : i = 21 j = 6 n = 120


Yes thanks you , i had already find the result ;) i have made some change :

Code
#include <iostream>
#include <conio.h>
using namespace std;

main ()
{
int i,j,n;
i=0;
n=i++;
cout << " A : i= " << i << " n = " << n << endl;

i=10;
n=++i;
cout << " B : i = " << i << " n = " << n << endl;

i=20;
j=5;
n=i++ * ++j;
cout << " C : i = " << i << " j = " << j << " n = " << n << endl;
getch();

}
Member
Posts: 3,461
Joined: Feb 9 2014
Gold: 616.00
May 25 2014 12:51pm
Ugh bummer u gotta do this
Go Back To Homework Help Topic List
Add Reply New Topic New Poll