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

Here's what I have so far:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <conio.h>

int main()
{
	int beginning;
	int ending;
	int counter = 1;
	int counter2 = 0;
	int counter3 = 0;

	cout<<"Please enter a beginning and ending value"<<endl;
	cin>>beginning;
	cin>>ending;

for (int counter = 1; counter <= ending; counter >= beginning; counter++)
{
	int value = 0;
	int counter2;

	cout<<"Please enter another value"<<endl;
	cin>>value;

	while (value <= ending; value >= beginning; counter2++)
	{
		int sum = counter + counter2;
		cout<<"The total of all values in the range is: "<<endl;

	}

	if (counter <= beginning && counter >= ending)
	{
		int total = counter++;
	}

	if (counter % value = value)
	{

		while (counter3++)
		{
			cout<<"There are "<<counter3<<"multiples"<<endl;
		}

	}

}

getch();

}

Recommended Answers

All 16 Replies

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

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.

commented: Good! But can be little more descriptive. +0

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); ?????

which asks if****

There is a big difference between = and ==

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.....

commented: 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. -4

in this part
for (int counter = 1; counter <= ending; counter >= beginning; counter++)

you can also do
for( int counter = 1; counter <= ending && counter >= beginning; counter++)

flip ending and beginning and it should make sense

so it should read
for( int counter = 1; counter >= beginning && counter <= ending; counter++)

so it should read
for( int counter = 1; counter >= beginning && counter <= ending; counter++)

Why start counter at 1? The OP quite clearly want counter to start at beginning.

What happens with your code if counter starts off less than beginning? The loop will exit immediately. I know that every example you've ever seen started

for(int something = 1; ... )

but that doesn't mean you have to.

Try

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

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 :)

Update. Had it running, but counter2/3 are messing me up. Any advice ? :

#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;
    cin>>ending;

for (int counter = ending; counter = beginning; counter++)
{
    int value = 0;
    int counter2;

    cout<<"Please enter another value between your two numbers"<<endl;
    cin>>value;

    while (value >= beginning && value <= ending)
    {
        if (int counter2 >= beginng && counter2 =< ending)
        {

        int sum = counter + counter2;
        cout<<"The total of all values in the range is: "<<endl;

    }

    if (counter2 <= beginning && counter2 >= ending)
    {
        cout<<"There are "<<counter2<<"numbers in the range"<<endl;
    }

    if (value % counter == 0)
    {
        int counter3 = 0;

        while (counter3++)
        {
            cout<<"There are "<<counter3<<"multiples"<<endl;
        }

    }

}

getch();

}

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.

Should I replace the while loop with an if statement for the multiples instead?

It runs, only does multiples, has the multiples wrong, and has a continuous loop. Due Wednesday. Might have to throw in the towel on this one and just turn it in as is.

#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;
	cin>>ending;

for (int counter = ending; counter = beginning; counter++)
{
	int value = 0;
	int counter2 = 0;
	int counter3 = 1;

	cout<<"Please enter another value between your two numbers"<<endl;
	cin>>value;

	while (value > beginning && value < ending)
	{
		if (counter2 > beginning && counter2 < ending)
		{

		int sum = counter + counter2;
		cout<<"The total of all values in the range is: "<<endl;

	}

			if (value % counter == 0)
	{
		counter3 = (ending - beginning) / value + 1;

		while (counter3 > beginning && counter3 < ending)
		{
			cout<<"There are "<<counter3<<" multiples of "<<value<<endl;
		}

	}

	if (counter2 >= beginning && counter2 <= ending)
	{
		cout<<"There are "<<counter2<<" numbers in the range"<<endl;
	}


}

getch();

}
}
counter = beginning

Is that meant to be

counter == beginning

?

That said, what happens if beginning is less than ending, which is what most people would expect? Since you add to counter each time, you'll never stop.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.