C ++ help ?

compguy786

Platinum Member
May 26, 2005
2,141
3
81
Here is the question i have

Using a for loop, modify the code below so that it loops a total of 3 times.

#include <iostream>
using namespace std;

int main()
{
// Prompt the user to enter an integer
int number;
cout << "Enter an integer: ";
cin >> number;

if (number % 2 == 0)
cout << number << " is even.";
if (number % 2 != 0)
cout << number << " is odd.";
return 0;
}


what i dont understand is....where exactly should i put the loop, and in what format
(i = 0; i <= 3; i++) ?
 

law9933

Senior member
Sep 11, 2006
394
0
0
I also hate being so PC ignorant, I Have no idea what you are wanting, not that I should already know.
 

Pulsar

Diamond Member
Mar 3, 2003
5,224
306
126
while (I_am == "stupid")
{
printf("Go to the wrong forum and ask a completely non-specific question/r/n");
cout("Did I get a clue yet? (Y/N)";
cin >> clue
if (clue == "Y")
I_am == "smart";
else
I_am =="stupid";
//Try googling c++ and loop.
}
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
Well, first you might want to, oh I don't know, tell us what you need help with!
 

Munky

Diamond Member
Feb 5, 2005
9,372
0
76
void loop(int x){
printf("I hate loops!");
if(x==0)
printf("google C++ loop");
else
loop(x-1);
}
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
C++ isn't your problem, your problem is that you don't know how to program. The looping in C++ or any other programming language is almost identical.

You won't get very far talking about how bad c++ is, because, guess what, It is still one of the most popular programming languages around. It didn't get that way by sucking.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: munky
void loop(int x){
printf("I hate loops!");
if(x==0)
printf("google C++ loop");
else
loop(x-1);
}

Ouch with a negative value

 

eakers

Lifer
Aug 14, 2000
12,169
2
0
Did you even look at the code to see what it does?

Just get it to read in the number and print the statement 3 times.
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
Originally posted by: compguy786
Here is the question i have

Using a for loop, modify the code below so that it loops a total of 3 times.

#include <iostream>
using namespace std;

int main()
{
// Prompt the user to enter an integer
int number;
cout << "Enter an integer: ";
cin >> number;

if (number % 2 == 0)
cout << number << " is even.";
if (number % 2 != 0)
cout << number << " is odd.";
return 0;
}


what i dont understand is....where exactly should i put the loop, and in what format
(i = 0; i <= 3; i++) ?


#include <iostream>
using namespace std;

int main()
{
// Prompt the user to enter an integer
int number;
for (i = 0; i <= 3; i++) {
cout << "Enter an integer: ";
cin >> number;

if (number % 2 == 0)
cout << number << " is even.";
if (number % 2 != 0)
cout << number << " is odd.";
}
return 0;
}
 
Sep 29, 2004
18,656
67
91
If you need help with this, change majors now!

<== 8 years as a professional software engineer that does not want to hire you
 

slugg

Diamond Member
Feb 17, 2002
4,723
78
91
for(i = 0; i <= 3; i++)

^^ will loop/print it 4 times... Classic "logical off by one" error.

Correction:

for(i = 0; i < 3; ++i)

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

Originally posted by: IHateMyJob2004
If you need help with this, change majors now!

<== 8 years as a professional software engineer that does not want to hire you

^^ This man speaks the truth. MIS might be for you? It's a business degree with focus on computers.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I'm assuming that this question is from a n00b programmer. To that I say: Everybody starts learning somewhere. OP, if you're struggling with loops a month from now, then consider doing something else. But for now, ignore the flames above.

Edit: Can we get some moderation of this thread? I think blatantly insulting posts aren't a good thing to make a habit.

The thread was moved in from some other forum, likely one of the social forums. I'm happy to say we maintain a higher standard in here. While I was also struck by the character of the responses, and have edited imported threads for profanity in the past, I can't generally commit to launder every thread that another mod might move here. So let this stand as permanent notice that most of the replies above degibson's did not originate in this forum. If they had, they would have drawn a comment from me at the least.

Markbnj, Programming Mod
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...
 

Sadaiyappan

Golden Member
Nov 29, 2007
1,120
4
81
#include <iostream>
using namespace std;

int main()
{

for(int i = 0; i < 3; i++)
{
int number;
cout << "Enter an integer: ";
cin >> number;

if (number % 2 == 0)
cout << number << " is even.";
if (number % 2 != 0)
cout << number << " is odd.";
}

return 0;
}
 

slugg

Diamond Member
Feb 17, 2002
4,723
78
91
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
Originally posted by: slugg

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space..[/quote]

wow.. thx! i learn something today! ya that does make sense.
 

slugg

Diamond Member
Feb 17, 2002
4,723
78
91
^^ That goes the same way with the decrement operator, --.

Oh and fix your quote, I didn't say that
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: JACKDRUID
Originally posted by: slugg

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space..

Indeed, as C++ is defined, this is absolutely true. A good optimizing compiler will often notice when there is no functional difference between ++i and i++, at least for primitive values, and do ++i instead when possible.

However, it's still a good idea to 1) Know the difference and 2) Use ++i wherever you can, as ++i is indeed significantly faster for iterators.

Edit: Removed superfluous line of quoted text that was outside the quote.
 

deveraux

Senior member
Mar 21, 2004
284
0
71
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.


Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!
 

tuteja1986

Diamond Member
Jun 1, 2005
3,676
0
0
// thingy loops
#include <iostream>
using namespace std;

int main() {


for (i = 0; i <= 3; i++) {

code goes here

}

return 0;
}

Read your book :! read them :! memories every single thing from

int to for to while to array to if statements to functions to classes to fstream :!

Programming not that hard its only people look at it in a way "OMG I am confused" ... and i am like , that thing is less confusing then chicks or in some case , you find it much easier to learn the basic of c++ then to try to sleep with a AAA chick
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: tuteja1986
// thingy loops
#include <iostream>
using namespace std;

int main() {


for (i = 0; i <= 3; i++) {

code goes here

}

return 0;
}

Read your book :! read them :! memories every single thing from

int to for to while to array to if statements to functions to classes to fstream :!

Programming not that hard its only people look at it in a way "OMG I am confused" ... and i am like , that thing is less confusing then chicks or in some case , you find it much easier to learn the basic of c++ then to try to sleep with a AAA chick

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |