For Loop

I've got a worksheet which asks me to ask the user to enter a beginning and ending value. Create a for loop using these numbers. Also ask the user to enter another number. The program will search for multiples of this value.
a. The total of all values in the range
b. How many multiples of the specified number occurred within the range
c. How many numbers are in the range

This is wrong. The for loop takes only three statements in the brackets. I suspect you meant something like:

for (int counter = beginning; counter<= ending; counter++)

while (value <= ending; value >= beginning; counter2++)

This is wrong. The while loop takes only one statement. Think about what you're trying to do - you want this loop to keep looping while something is true. What's that something? That something is what goes in the while's brackets.

if (counter % value = value)

This won't even compile. Did you mean

if (counter % value == value)

There is a big difference between = and ==

while (counter3++)

This is insane and will loop forever unless counter3 begins as a negative number, but I doubt very much that you meant this.

if you're counting from 0 to whatever then couldn't you make an if else statement look it asks if counter == 0 and if counter2 == 0 then you make an array or whatever or even vector<int> multiples and just do multiples.push_back(counter2); ?????

Yeah! = is an assignment operator which off course can't be used with if & not used in the way OP have written it (If used in some other place), the variable to which value should be assigned must be at the left of the operator.

Like this; value=counter % value Not like this counter % value = value Notice no "if" here, because i didn't mean to...

And about ==
It is the logical operator to compare two values, just as the OP wanted it to work.....

Comments

WaltP:There is no reason to explain someone else's explanation of a problem. Especially with a very confusing addition that has no bearing on the original comment.

This is wrong. The for loop takes only three statements in the brackets. I suspect you meant something like:

for (int counter = beginning; counter<= ending; counter++)

while (value <= ending; value >= beginning; counter2++)

This is wrong. The while loop takes only one statement. Think about what you're trying to do - you want this loop to keep looping while something is true. What's that something? That something is what goes in the while's brackets.

if (counter % value = value)

This won't even compile. Did you mean

if (counter % value == value)

There is a big difference between = and ==

while (counter3++)

This is insane and will loop forever unless counter3 begins as a negative number, but I doubt very much that you meant this.

Thanks! I see what I did wrong now, adding more than necessary in those loops was something I missed. I'm still new to programming :)

You never set a value for counter2, so it is starting with some random value.

int counter3 = 0;
while (counter3++)

Let's have a good look at this code:

while (counter3++)
Right, so this is a loop that will run as long as (counter3++) evaluates to anything except zero. So, what's the first evaluation? counter3 has been set to zero, so the first evaluation is zero (after which you add one), so the while loop finishes instantly.

Think about what you're trying to do. A while loop is for making something happen over and over and over and over again, until some condition is met. What is it that you want to happen over and over again? I don't think that having that while loop makes any sense at all.

Last shot. Updated. Fixed the continuous looping, now it only does multiples and skips over the total number of values in the range and the sum of all values in the range. Asks for a number, finds the multiple, then asks for another number.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <conio.h>
int main()
{
int beginning;
int ending;
int counter = 1;
cout<<"Please enter a beginning and ending value"<<endl;
cin>>beginning; //takes in beginning value for loop to start at
cin>>ending; //takes in ending value for loop to end at
for (int counter = ending; counter = beginning; counter++) //counter has to be from beginning to ending, or the range
{
int value = 0;
int counter2 = 0;
int counter3 = 1;
cout<<"Please enter another value between your two numbers"<<endl;
cin>>value; //takes in the value that will be in the range
if (counter2 > beginning && counter2 < ending)
{
int sum = counter + counter2; //finds the total of the numbers in the range
cout<<"The total of all values in the range is: "<<sum<<endl; //outputs the sum of all the numbers in the range
}
if (value % counter == 0) //finds multiples of value by using modulus
{
counter3 = (ending - beginning) / value + 1; //finds the multiples of the value that the user inputs
cout<<"There are "<<counter3<<" multiples of "<<value<<endl; //outputs the multiples of the value
}
if (counter2 >= beginning && counter2 <= ending)
{
cout<<"There are "<<counter2<<" numbers in the range"<<endl; //outputs the total amount of numbers in the range
}
}
getch();
}

for (int counter = ending; counter = beginning; counter++) //counter has to be from beginning to ending, or the range

Counter has to be from beginning to ending? Well then it seems a shame that you've made it from ending to beginning.

Your code is badly organised, and you're suffering because of it. For example, counter2 is set to the value zero, and then never ever changes. Given that you're using it to make decisions, this seems rather wrong, and it's why your code skips over the total number of values in the range and the sum of all values in the range.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...