School assignment using while loops

Started by
25 comments, last by deathknight2005 14 years, 9 months ago
EDIT: I've fixed it, thanks for the help guys! :D On C++. Yeah.. our teacher basically just gives us homework and tells us to read the book. I've read the book we're supposed to read and it doesn't tell me anything about how to do this, so I've come to you guys for help. Here are the instructions: 1. Prompts the user to input 2 numbers - firstNum and secondNum. Make sure that firstNum is less than secondNum. If it is not, the user should get an error message and be prompted again to enter the values. ... uses while loops to perform the following steps: 2. Output all odd numbers between firstNum and secondNum. 3. Output the sum of all even numbers between firstNum and secondNum. 4. Outputs the numbers and their squares of all numbers between firstNum and secondNum. 5. Outputs the numbers from secondNum to firstNum as a countdown sequence Here is the code I have so far, I'm not sure if it's correct or not:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int firstNum, secondNum, squareNum, counter, counterT, counterR, counterU;
    int sum = 0;
    
    cout << "The first number must be less than the second number.\n\n";
    cout << "Enter 1st number: ";
    cin >> firstNum;
    cout << "Enter 2nd number: ";
    cin >> secondNum;
    
    counterT = secondNum;
    counterU = secondNum;
    if (secondNum % 2 == 1)
    {
       counter = secondNum - 2;
       counterR = secondNum - 1;
    }
    else
    {
       counter = secondNum - 1;
       counterR = secondNum - 2;
    }
        
    if (firstNum < secondNum)
    {
                 cout << "Odd numbers: ";
                 do
                 {
                               cout << counter << " ";
                               counter -= 2;
                 } while(counter >= firstNum);
                 cout << "\n";
                 cout << "Sum of even numbers: ";
                 do
                 {
                               sum+=counterR;
                               counterR -= 2;
                 } while (counterR >= firstNum);
                 cout << sum;
                 cout << "\n";
                 cout << "Squares of numbers: ";
                 do
                 {
                               cout << (counterU * counterU) << " ";
                               counterU--;
                 } while(counterU >= firstNum);
                 cout << "\n";
                 cout << "Countdown: ";
                 do
                 {
                               cout << counterT << " ";
                               counterT--;
                 } while(counterT >= firstNum);
                 cout << "\n";
    }
    else
    cout << "Your first number is not less than your second number.\n";  
    
    
    system("pause");
    return 0;
}
[Edited by - Ultimatepuff on July 21, 2009 4:05:44 PM]
Advertisement
What specifically are you having trouble with?
- Haptic
Quote:Original post by Haptic
What specifically are you having trouble with?


I don't know how to do parts 2-4, and I'm not sure if I did part 5 right either.
That isn't a complete program, I think you've only pasted as fragment of what you really have, if it's much longer than that though then I recommend you use source tags intead.
"int firstNum, secondNum, oddNum, sum, evenNum, betweenNum, squareNum, counter;
counter = secondNum;"

counter = secondNum is a bug because secondNum is initialized. This would cause
the program to crash
Our whole life is a opengl application.
Quote:Original post by dmatter
That isn't a complete program, I think you've only pasted as fragment of what you really have, if it's much longer than that though then I recommend you use source tags intead.


Well a fragment is actually all I have, I don't know where to start on the rest because I'm new/suck at programming like that.
Well you already have a while loop there that goes through all of the numbers (albeit backwards), so for question 4, why not just add a few lines that calculates and outputs the square of the current number.

To calculate if a number is odd or not you need to divide it by 2 and get the remainder. In C, this is done with the % operator. So 3%2 = 1.
If the answer is zero, it's even, if it's 1 its odd. You just need to add this condition to your loop.

As a side note - you are setting the value of 'counter' before you set the value of secondNum which is a bug.
- Haptic
Quote:Original post by tnutty
"int firstNum, secondNum, oddNum, sum, evenNum, betweenNum, squareNum, counter;
counter = secondNum;"

counter = secondNum is a bug because secondNum is initialized. This would cause
the program to crash


it wouldn't cause a crash, it would just set counter to an uninitialized value (secoondNum could be any number), but it won't crash.
First, might I say that being new to programming and looking for help would indicate you do not suck. Look at what you have not as incomplete, but small portions that are complete.

The way you seem to be setting up all the while loops in the if statement is a good way to go. Have you learned if/else blocks yet?

Also, as I have read my fair share of C++ textbooks I can understand there is a difference between syntax and explaination of how things work.

As others have pointed out 'counter = secondNum' is bugged, but for two reasons:
1. unless you specifically assign a value to your variables, the value that is stored is whatever happened to be at that memory location (usually refered to as garbage)

2. The second bug is a logic error. You assigned counter secondNum's value before the user actually defined what secondNum was equal to so you really have no control. Try assigning the value of secondNum after the user enters it to counter. That way you "know" that it is properly defined (assuming the user input was valid).

You have a good start. Just keep asking questions.
Vis
Exactly. Your code will not work because counter will not be equal to secondNum because the assignment is done before secondNum itself has a value!

int firstNum, secondNum, oddNum, sum, evenNum, betweenNum, squareNum, counter;counter = secondNum; //This is your problem right here


If you look at the code, you will see that you have declared all of those variables, however the user hasn't entered the two numbers yet, so counter will not be equal to the value of secondNum later on.

Just think about where you should move the assignment so that counter has the proper value of secondNum after the user has entered it.

For #2, you can see whether or not a number is odd by using the % operator, which returns the remainder between two numbers. Knowing that any even number divided by 2 has a remainder of zero, it is a simple task to see whether or not a number is odd or not.

All in the same loop, you can output all of the odd numbers by seeing if the remainer between the number and 2 is not 0, and if it is zero, then it is an even number, and for problem #3, you can add it to the sum variable (remember to initialize that variable at 0!).

Knowing that, I am sure you can figure out problem #4. You can either implement your own function for squaring numbers (as there is no operator for squaring a number in C++) or you can use the function in the math library.

This topic is closed to new replies.

Advertisement