![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 12
Rep Power: 0
![]() |
// program3.cpp
//
// Purpose: declare integer up_down and intitialize it to 3
// Programmer: Franklin Graves
#include <iostream.h>
#include <stdlib.h>
int main()
{
*int up_down; * * // declare integer up_down
*int A; * * * * * // increment variable
*int B; * * * * * // decrement variable
*up_down = 3; * * // initialize up_down
*A = up_down; // initialize up_down to A
*B = up_down; // initialize up_down to B
*A++; * // increment A
*B--; * // decrement B
*// output to user
*cout << "A = " << A << '\n';
*cout << "B = " << B << '\n';
* * *system("PAUSE");
* * *return 0;
}I am using Dev-C++ (I am currently in my Adv C++ class) and I am not understanding how to do increment and decrement. The book tells me that I do them underneith each 'cout', but when I do that, it doesnt process right, when compiled they just come out as 3. When I do it my way above, it works. Am I doing something wrong, or is the above the right way to do it? |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
If you want A to come out as 4, and B to come out to 2 then I think you have it correct in your post. If you want it to remain at three then you'd want to put it after the cout. I don't see why they'd want you to do that, though.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What happens if you use B++ instead of B--? What results do you get then?
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2004
Posts: 12
Rep Power: 0
![]() |
Mjordan2nd - Yeah, I figured I was doing it correctly, the book is just wrong. Thanks for your help
![]() Ooble - It works with either ++ or --, just not below cout |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 5
![]() |
you can pre-increment/pre-decrement the variables in the cout statement
#include <iostream>
using namespace std;
int main()
{
int A=3,B=3;
cout << "A = " << ++A << endl;
cout << "B = " << --B << endl;
return 0;
}
__________________
-- lostcauz Stepped in what?... Behind whose barn?... I didn't even know they had a cow! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2004
Posts: 12
Rep Power: 0
![]() |
when i do the includes, do i not need to add .h to the end of iostream?
is endl the same as '\n'? and one final question lol, is system pause not needed? is there another way to make it stop and still allow you time to view the outcomes? |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
if you're on windows use "conio.h" and getch() instead of system pause. As for the .h at the end that represents depreceated headers and ones that follow todays standards. You dont want .h at the end, thats depreceated
. endl the same as \n... No, but pretty much the same outcome, i think its more affective once you get more into C++ programming.
__________________
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Dec 2004
Posts: 12
Rep Power: 0
![]() |
okay, i see, thanks!
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>As for the .h at the end that represents depreceated headers
This is only true for the C library such as stdlib.h. For C++ libraries like iostream.h, it means they are completely non-standard, not deprecated. Deprecated means that the standard still supports a feature, but may not in future revisions. >if you're on windows use "conio.h" and getch() instead of system pause. This won't work on all Windows compilers. A better solution is cin.get() and force the user to hit enter as they probably would have done anyway. At least that way your code remains portable across all systems and compilers at a negligable inconvenience to the user. >is endl the same as '\n'? No. They both print a newline, but endl also flushes the stream. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|