446,227 Members | 1,315 Online
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

# Multi Variable For statements

 P: 4 I need to come up with a For statement that would allow me to do something like this... c:\> countup Enter a start value: 10 Enter an ending value: 100 Enter an increment: 20 Counting: 10 30 50 70 90 I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you Jun 8 '07 #1
9 Replies

 Expert 100+ P: 1,764 I need to come up with a For statement that would allow me to do something like this... c:\> countup Enter a start value: 10 Enter an ending value: 100 Enter an increment: 20 Counting: 10 30 50 70 90 I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you Yes,we have a lot of ideas,but we cannot realise them if u don't show us ur code. Savage Jun 8 '07 #2

 Expert 100+ P: 844 I need to come up with a For statement that would allow me to do something like this... c:\> countup Enter a start value: 10 Enter an ending value: 100 Enter an increment: 20 Counting: 10 30 50 70 90 I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you That's pretty simple: Expand|Select|Wrap|Line Numbers int start = 10; int end = 100; int incr = 20;   for (int i = start; i < end; i += incr){     //code }   Is that what you need? Jun 8 '07 #3

 P: 4 well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code Jun 8 '07 #4

 Expert 100+ P: 844 well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code What do you mean? You don't know how to get input or you don't know how to code the for loop? Jun 8 '07 #5

 Expert 100+ P: 1,764 well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code Can we see that thing?(code) Savage Jun 8 '07 #6

 P: 4 #include main () { int a, w=0, x, y, z; // Stetting up the Starting Values printf("Enter a Starting Value:"); scanf("%d", &x); printf("Enter an Ending Value:"); scanf("%d", &y); printf("Enter an Increment:"); scanf("%d", &z); for( a; a <= y; ++w) { //setting up the conditions for the program and making it count accurately x + (w * z) == a; if( y < x ){ printf("Invalid Option Starting Value must be smaller than Final Value\n"); break; //If person makes a mistake on which value is bigger } if (z < 0){ printf("Increments Cannot Be a Negative Number\n"); break; } //If user forgets that increments cannot be a negative number } printf("Counting: %d\n",a);//For Statement so the program can count out loud system("pause"); return(0); } This is what i have idk how much help it will be Jun 8 '07 #7

 Expert 100+ P: 844 #include main () { int a, w=0, x, y, z; // Stetting up the Starting Values printf("Enter a Starting Value:"); scanf("%d", &x); printf("Enter an Ending Value:"); scanf("%d", &y); printf("Enter an Increment:"); scanf("%d", &z); for( a; a <= y; ++w) { //setting up the conditions for the program and making it count accurately x + (w * z) == a; if( y < x ){ printf("Invalid Option Starting Value must be smaller than Final Value\n"); break; //If person makes a mistake on which value is bigger } if (z < 0){ printf("Increments Cannot Be a Negative Number\n"); break; } //If user forgets that increments cannot be a negative number } printf("Counting: %d\n",a);//For Statement so the program can count out loud system("pause"); return(0); } This is what i have idk how much help it will be Did you see the for loop I posted earlier? I think that might help you. Jun 9 '07 #8

 P: 4 yes i saw that, but the start value, end value, and the increment value are all variables ...when using the program you can insert any numbers you would like...that is my main problem with this program Jun 9 '07 #9

 Expert 100+ P: 844 yes i saw that, but the start value, end value, and the increment value are all variables ...when using the program you can insert any numbers you would like...that is my main problem with this program Ok, so you just input the variables like you did in your program. Just change the for loop. Like this: Expand|Select|Wrap|Line Numbers //input for x (start) //input for y (end) //input for z (increment)   for (int i = x; i < y; i += z) {     //code here }   // or another version   for (; x < y; x += z) {     //code here }   Does that help? For loops need to have at least three statements but the computer doesn't care what they are: Expand|Select|Wrap|Line Numbers for (;;) {     //code here }   That is a valid for loop. Jun 9 '07 #10