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

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




having problem with 'for' loop

 
Reply to this topicStart new topic

having problem with 'for' loop

miensta
2 Jun, 2008 - 05:15 PM
Post #1

New D.I.C Head
*

Joined: 3 May, 2008
Posts: 28


My Contributions
i want my code to show a set of ten integers, each seperated by an inputted interval, (e.g. input 3, the set is {1, 4, 7, 10, 13, 16, 19, 21, 24, 27} ). When i input 1, the number of intergers in my set is correct. However, the number I input also limits the number of integers in my set. For 3, I only see 4 integers. For 15, I only see 1. Whats the deal???

Here's my code:
CODE

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{

        string response;  
  
    cout << "Would you like to run a case? (y/n) \n";  
    cin >> response;  
  
    while (response == "y") {
   cout << "Please enter the desired step size. ";
int size;


   cin >> size;


   int i;
   for (i=1; i <=10; i= i + size)
   {
    

     cout << i << " ";


   }
    }
return 0;
}



This post has been edited by miensta: 2 Jun, 2008 - 05:23 PM
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 05:36 PM
Post #2

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
You're making it a bit hard for yourself. You don't need that much code for this at all.

cpp

#include <iostream>

using namespace std;

int main()
{
char cInput('y');
int iSteps(0);

while (true) {
cout << "Would you like to run a case? (y/n): ";
cin >> cInput;

if (cInput == 'y' || cInput == 'Y') {
cout << endl << "Please enter the desired step size: ";
cin >> iSteps;
cout << endl;

for (int i = 1; i <= 10; i += iSteps) // ** Change 10 to another value if you want longer lists. **
cout << i << " ";

cout << endl << endl;
} else if (cInput == 'n' || cInput == 'N') {
return 0;
} else {
cout << endl << "Invalid entry! Please try again." << endl << endl;
continue;
}
}

return 0;
}


The commented line in the code indicates that your program will never print a number that is higher than 10. If that 10 was a 100, the program wouldn't print any number higher than 100. Up to you to find out how to fix it.

This post has been edited by skater_00: 2 Jun, 2008 - 05:39 PM
User is offlineProfile CardPM
+Quote Post

chaosTechnician
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 05:39 PM
Post #3

New D.I.C Head
*

Joined: 27 May, 2008
Posts: 20



Thanked: 1 times
My Contributions
Your loop is set up to stop when i <= 10. Then you're adding size to it. The loop is stopping when i exceeds 10.
If size = 3, it'll do this:
CODE
Run   i   size   cout
1     1     3     1     1 + 3 = 4
2     4     3     4     4 + 3 = 7
3     7     3     7     7 + 3 = 10
4    10     3    10     10 + 3 = 13;  13 > 10, loop stops here

As soon as i gets greater than 10, it'll stop. You want your loop control variable to be independent from the number you're displaying. Have your loop be a simple for(i = 1; i<=10; i++). Then there are two ways you could handle it:
1. Make another variable that stores the actual data to be displayed. Change your other variable by adding size to it.
Or 2. Just multiply i-1 by size to get the ith element of the list and display that result.
User is offlineProfile CardPM
+Quote Post

miensta
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 06:01 PM
Post #4

New D.I.C Head
*

Joined: 3 May, 2008
Posts: 28


My Contributions
QUOTE(chaosTechnician @ 2 Jun, 2008 - 06:39 PM) *

Your loop is set up to stop when i <= 10. Then you're adding size to it. The loop is stopping when i exceeds 10.
If size = 3, it'll do this:
CODE
Run   i   size   cout
1     1     3     1     1 + 3 = 4
2     4     3     4     4 + 3 = 7
3     7     3     7     7 + 3 = 10
4    10     3    10     10 + 3 = 13;  13 > 10, loop stops here

As soon as i gets greater than 10, it'll stop. You want your loop control variable to be independent from the number you're displaying. Have your loop be a simple for(i = 1; i<=10; i++). Then there are two ways you could handle it:
1. Make another variable that stores the actual data to be displayed. Change your other variable by adding size to it.
Or 2. Just multiply i-1 by size to get the ith element of the list and display that result.



what would be the second loop to store the actual data to be displayed? would i introduce another variable?

CODE


int j;
    for (j=1; j <=10; j++)
     cout << j << " ";

   }
return 0;

}



sorry kinda clueless on how to code in the ith integer, but i know exactly what you're talking about conceptually.
User is offlineProfile CardPM
+Quote Post

miensta
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 06:12 PM
Post #5

New D.I.C Head
*

Joined: 3 May, 2008
Posts: 28


My Contributions
tried doing something different, where you could input the number of integers you wanted instead, but im getting my variables back as 'unitializd' (for j and i)

CODE


#include <iostream>  
  
using namespace std;  
  
int main()  
{  
    char cInput('y');  
    int iSteps(0);  
    int set;
    
    while (true) {  
        cout << "Would you like to run a case? (y/n): ";    
        cin >> cInput;    
        
        if (cInput == 'y' || cInput == 'Y') {  
            cout << endl << "Please enter the desired step size and number of integers in a set desired: ";  
            cin >> iSteps >> set;
            cout << endl;  

int j;
for (int j = 1; j <=set; j++)
{

int i;
            for (int i = 1; i <=10; i += iSteps)
                cout << i << " ";  
}

  
            cout << endl << endl;  
        } else if (cInput == 'n' || cInput == 'N') {  
            return 0;  
        } else {  
            cout << endl << "Invalid entry! Please try again." << endl << endl;  
            continue;  
        }  
    }  
  
    return 0;  
}


User is offlineProfile CardPM
+Quote Post

Ambercroft
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 06:30 PM
Post #6

D.I.C Head
Group Icon

Joined: 5 Jan, 2007
Posts: 107



Thanked: 3 times
Dream Kudos: 25
My Contributions
The problem is that your index is part of the value you want to output. As soon as the output value exceeds 10 the loop quits.

CODE

int initial=0;
int size;
cin >> size;
int i;
for (i=1; i <=10; i++){
   cout << initial<< " ";
   initial = initial + size;

   }


This is your first post code modified.
print starts at 0 or whatever initial is set for.

cool.gif
User is offlineProfile CardPM
+Quote Post

miensta
RE: Having Problem With 'for' Loop
2 Jun, 2008 - 06:47 PM
Post #7

New D.I.C Head
*

Joined: 3 May, 2008
Posts: 28


My Contributions
biggrin.gif thanks so much!!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:39PM

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