Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is supposed to output 3, 6, 9, and so forth. It is outputting 3, 4, 5 ,6 etc.
Can you tell me how to whats wrong and how to fix it?
cout<<"counting by 3"<<endl;
int b=1, e=2;
for (int d=1; b<10; b++) {
d=b+e;
cout<<" "<< d<<" ";}

What I have tried:

This code is supposed to output 3, 6, 9, and so forth. It is outputting 3, 4, 5 ,6 etc.
Can you tell me how to whats wrong and how to fix it?
cout<<"counting by 3"<<endl;
int b=1, e=2;
for (int d=1; b<10; b++) {
d=b+e;
cout<<" "<< d<<" ";}
Posted
Updated 27-Jan-19 7:15am

Quote:
Can you tell me how to whats wrong

Your code is a mess, use the debugger to see what each part is doing.

Your code do not behave the way you expect, or you don't understand why !
There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Or ... there is the readable version:
C++
int limit = 12;
int start = 3;
string separator = "";
for (int value = start; value <= limit; value += 3)
   {
   cout << separator << value;
   separator = ", ";
   }
cout << endl;
 
Share this answer
 
Comments
Nelek 27-Jan-19 7:59am    
nope... you only give 4 integers out :) BTW I am copying a bit of your answer... I am lazy to write all down :P
OriginalGriff 27-Jan-19 8:10am    
I didn't want to do all of his homework!
Nelek 28-Jan-19 1:42am    
Good point
Here is another possibility:
C++
for (int i = 0; i < 5; i++)
{
   cout << separator << i*3 << endl;
}
// Result: 
//3
//6
//9
//12
 
Share this answer
 
v5
Comments
OriginalGriff 27-Jan-19 8:09am    
Plagiarism! Hah! Off to S&A I go! :laugh:
Nelek 28-Jan-19 1:41am    
You are right.. I forgot to quote you :)
Nelek 28-Jan-19 1:46am    
You frightened me... I have changed my code to avoid plagiarism :P
I was doing it wrong. Here is my homemade solution:
C++
for (int b=1, d=0, e=2; b<10; b++) {
d=e+b; e+=2;
 cout<<d<<" ";}
 
Share this answer
 
v2
Comments
Patrice T 27-Jan-19 4:22am    
it look complicated in your head.
Nelek 27-Jan-19 8:05am    
you are still doing it way too complicated, look Solution 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900