Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 2nd, 2004, 1:22 PM   #1
Franklin509
Newbie
 
Join Date: Dec 2004
Posts: 12
Rep Power: 0 Franklin509 is on a distinguished road
// 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?
Franklin509 is offline   Reply With Quote
Old Dec 2nd, 2004, 4:00 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Dec 2nd, 2004, 6:00 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
What happens if you use B++ instead of B--? What results do you get then?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 2nd, 2004, 11:40 PM   #4
Franklin509
Newbie
 
Join Date: Dec 2004
Posts: 12
Rep Power: 0 Franklin509 is on a distinguished road
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
Franklin509 is offline   Reply With Quote
Old Dec 3rd, 2004, 6:25 AM   #5
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 5 lostcauz is on a distinguished road
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!
lostcauz is offline   Reply With Quote
Old Dec 3rd, 2004, 8:40 AM   #6
Franklin509
Newbie
 
Join Date: Dec 2004
Posts: 12
Rep Power: 0 Franklin509 is on a distinguished road
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?
Franklin509 is offline   Reply With Quote
Old Dec 3rd, 2004, 9:10 AM   #7
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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.
__________________

tempest is offline   Reply With Quote
Old Dec 3rd, 2004, 1:25 PM   #8
Franklin509
Newbie
 
Join Date: Dec 2004
Posts: 12
Rep Power: 0 Franklin509 is on a distinguished road
okay, i see, thanks!
Franklin509 is offline   Reply With Quote
Old Dec 4th, 2004, 10:54 AM   #9
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC