Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,915 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,123 people online right now. Registration is fast and FREE... Join Now!




int help

 
Closed TopicStart new topic

int help

grimmben
4 Dec, 2007 - 06:20 AM
Post #1

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 27


My Contributions
CODE
//Ch7AppE08.cpp – displays the numbers 0 through 117, in increments of 9
//Created/revised by <your name> on <current date>

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int product = 9;

        for (int count = 117; count >= 9; count = count - 9) {
        product*=count;
             }
cout << count << endl;
     //display number's increments of 9
    return 0;
}   //end of main function

User is offlineProfile CardPM
+Quote Post

lockdown
RE: Int Help
4 Dec, 2007 - 06:38 AM
Post #2

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(grimmben @ 4 Dec, 2007 - 07:20 AM) *

CODE
//Ch7AppE08.cpp – displays the numbers 0 through 117, in increments of 9
//Created/revised by <your name> on <current date>

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int product = 9;

        for (int count = 117; count >= 9; count = count - 9) {
        product*=count;
             }
cout << count << endl;
     //display number's increments of 9
    return 0;
}   //end of main function



First please post your question because it helps us determine your issue quicker.

I think I found your issue.

Your for loop is not correctly created. Also based on what you are trying to do why start at 117 why not 0?

First the parameters for the for loop are not correct. 1. You do not need count = count -9 because count - 9 is setting count equal to that already. Also your equation inside the loop is incorrect. It should be count = count * product;

But I believe the way you created your code is not what you actually want. For loops can increment variables for you so if you start from 0 the for loop would look like for(int count=0;count<=117;count+9). That says count is = 1 for the first loop and when it comes back it adds 9 to that and sets it equal to count. It will do this until the count value is less then or equal to 117.

Also you need to have your cout << statement inside the loop so every time the for loop runs it will display the current count number. If it is outside it will display the last number the loop set count to in this case 117.

You do not need the product variable because the for loop takes care of calculating numbers and making sure they are in incriments of 9.

Also their is no need for using std::cout and endl. Just uses this using namespace std;

Here would be the corrrections

CODE


#include <iostream>

using namespace std; // Better to uses

int main()
{

        for (int count = 0; count <=117; count+9) // I would suggest starting with 1 and then working up
                        {
        cout << count << endl; // Displays count each time the loop runs and sets count to a new value.
             }
    return 0;
}   //end of main function


This post has been edited by lockdown: 4 Dec, 2007 - 06:40 AM
User is offlineProfile CardPM
+Quote Post

grimmben
RE: Int Help
4 Dec, 2007 - 07:20 AM
Post #3

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 27


My Contributions
Basically the code That I have provided has to display the numbers 0 thru 117 in the increments of 9

QUOTE(lockdown @ 4 Dec, 2007 - 07:38 AM) *

QUOTE(grimmben @ 4 Dec, 2007 - 07:20 AM) *

CODE
//Ch7AppE08.cpp – displays the numbers 0 through 117, in increments of 9
//Created/revised by <your name> on <current date>

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int product = 9;

        for (int count = 117; count >= 9; count = count - 9) {
        product*=count;
             }
cout << count << endl;
     //display number's increments of 9
    return 0;
}   //end of main function



First please post your question because it helps us determine your issue quicker.

I think I found your issue.

Your for loop is not correctly created. Also based on what you are trying to do why start at 117 why not 0?

First the parameters for the for loop are not correct. 1. You do not need count = count -9 because count - 9 is setting count equal to that already. Also your equation inside the loop is incorrect. It should be count = count * product;

But I believe the way you created your code is not what you actually want. For loops can increment variables for you so if you start from 0 the for loop would look like for(int count=0;count<=117;count+9). That says count is = 1 for the first loop and when it comes back it adds 9 to that and sets it equal to count. It will do this until the count value is less then or equal to 117.

Also you need to have your cout << statement inside the loop so every time the for loop runs it will display the current count number. If it is outside it will display the last number the loop set count to in this case 117.

You do not need the product variable because the for loop takes care of calculating numbers and making sure they are in incriments of 9.

Also their is no need for using std::cout and endl. Just uses this using namespace std;

Here would be the corrrections

CODE


#include <iostream>

using namespace std; // Better to uses

int main()
{

        for (int count = 0; count <=117; count+9) // I would suggest starting with 1 and then working up
                        {
        cout << count << endl; // Displays count each time the loop runs and sets count to a new value.
             }
    return 0;
}   //end of main function



User is offlineProfile CardPM
+Quote Post

lockdown
RE: Int Help
4 Dec, 2007 - 07:33 AM
Post #4

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Yes I understand that but 1. It dose not work correctly since if you run it will give you a error and with the correction of that error will give you only the 0.

The reason for this is your cout statement is outside the loop.

Also you did not state if you needed the number in descending order or ascending order. Normally most people put information in ascending order going from smallest to highest which I recommended.

I would recommend reading up on loop statements to make sure you understand how they work. They are really useful and powerful.

Also you are not being clear on what you are asking us to do. I pointed out the errors and the reasons why. Is their something you did not understand that I can ill berate on?


This post has been edited by lockdown: 4 Dec, 2007 - 07:39 AM
User is offlineProfile CardPM
+Quote Post

grimmben
RE: Int Help
4 Dec, 2007 - 08:25 AM
Post #5

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 27


My Contributions
It gives me a loop full of zero's

QUOTE(lockdown @ 4 Dec, 2007 - 08:33 AM) *

Yes I understand that but 1. It dose not work correctly since if you run it will give you a error and with the correction of that error will give you only the 0.

The reason for this is your cout statement is outside the loop.

Also you did not state if you needed the number in descending order or ascending order. Normally most people put information in ascending order going from smallest to highest which I recommended.

I would recommend reading up on loop statements to make sure you understand how they work. They are really useful and powerful.

Also you are not being clear on what you are asking us to do. I pointed out the errors and the reasons why. Is their something you did not understand that I can ill berate on?


User is offlineProfile CardPM
+Quote Post

lockdown
RE: Int Help
4 Dec, 2007 - 08:38 AM
Post #6

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
My mistake I messed up my correction and statement. I am actually working on some code similar to urs for a friend and got them confused.

Let me correct myself.

You can have count = count +9 in your for statement. I was thinking for some odd reason count+9 would work but I forget that dose not preform the equation properly.

Here is working code:
CODE

#include <iostream>

using namespace std; // Better to uses

int main()
{

        for (int count = 0; count <=117;count = count+9) // I would suggest starting with 1 and then working up
        {
        cout << count << endl; // Displays count each time the loop runs and sets count to a new value.
        
           }
    return 0;
}   //end of main function


This post has been edited by lockdown: 4 Dec, 2007 - 08:38 AM
User is offlineProfile CardPM
+Quote Post

grimmben
RE: Int Help
4 Dec, 2007 - 08:37 PM
Post #7

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 27


My Contributions
Sorry it took so long for me to reply THANKS FOR THE HELP, I appreciated it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
QUOTE(lockdown @ 4 Dec, 2007 - 09:38 AM) *

My mistake I messed up my correction and statement. I am actually working on some code similar to urs for a friend and got them confused.

Let me correct myself.

You can have count = count +9 in your for statement. I was thinking for some odd reason count+9 would work but I forget that dose not preform the equation properly.

Here is working code:
CODE

#include <iostream>

using namespace std; // Better to uses

int main()
{

        for (int count = 0; count <=117;count = count+9) // I would suggest starting with 1 and then working up
        {
        cout << count << endl; // Displays count each time the loop runs and sets count to a new value.
        
           }
    return 0;
}   //end of main function



User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/8/09 02:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month