Hello kidd2100,
Your topic says
Trouble shooting Nested Loops and statements
, but there are no nested loops.
Your indenting is not very good and is hard to follow.
Just to give you an idea of something different:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//Prompt user to input two numbers. First
//number smaller than second number. Out put all
//all odd numbers between the numbers and seperate
//with a space. Utilize while loop. Out put the
// sum of all even numbers between.
//Out put the numbers and their squares between 1
//and 10. Out put the sum of the square of the odd
//numbers. Output all uppercase letters.
int firstNumber;
int inputFirstNumber;
int secondNumber;
int sqrRoot = 0;
int sum = 0;
cout << "Please input two integers, the second one being bigger than first one!: ";
cin >> firstNumber >> secondNumber;
cout << '\n' << '\n';
inputFirstNumber = firstNumber;
while (firstNumber != secondNumber)
{
if (firstNumber % 2 == 0)
{ //If firstNumber is even
if (firstNumber + 2 < secondNumber)
{ //Adds the sum of even numbers
//sum = sum + (firstNumber + 2);
sum += firstNumber;
}
firstNumber = firstNumber + 1;
}
////////////////Having trouble with odd portion of this code below.
else if (firstNumber == inputFirstNumber) //If firstNumber is odd
{
firstNumber = firstNumber + 2;
//sqrRoot = sqrRoot + (firstNumber * firstNumber); // <--- This ia sdding the square of the number not the square root.
// <--- I think what you want is
sqrRoot += sqrt(firstNumber);
cout << firstNumber << " ";
firstNumber++;
}
else
{
//sqrRoot = sqrRoot + (firstNumber * firstNumber);
// <--- I think what you want is
sqrRoot += sqrt(firstNumber);
cout << firstNumber << " ";
firstNumber++;
}
}
//cout << '\n';
cout << "\n\nThe sum of the even numbers is: " << sum << "\n\n"; // Sum of even numbers from between two inputted numbers
std::cout << "Num Square\n";
for (int i = 1; i <= 10; i++)
{ // Square numbers between 1 and 10
cout << std::setw(2) << i << " - " << std::setw(5) << i * i << "\n";
}
//cout << '\n';
cout << "\n\nSquare roots of all odd numbers added up: " << sqrRoot << " should be 9 Actually 9.61387...\n\n"; // Display of odd numbers Square Root
//for (int j = 1; j <= 26; j++)
for (char ltr = 'A'; ltr <= 'Z'; ltr++)
{ // Utilize For Loop To Print Alphabet Capitlized
cout << ltr << " ";
}
return 0;
}
|
I use a different style of {}s. I find it easier to read and follow. You are free to choose whichever style you like.
The whole section for dealing with the even numbers does not work 100%, but is on the right track.
I think the section for odd numbers is what you want, but you are going about it wrong.
You output statement says "Square roots of all odd numbers added up: ", but you are adding the squares of the number not the square root of the number. 1 or the other is wrong.
I think the biggest problem is
firstNumber++;
is being done in to many places and giving "firstNumber" the wrong next number when you reach the top of the while loop.
The last for loop is an alternative that makes it easier to work with. This way you do not have to memorize the decimal value of "A" or "a" to use in the for loop. Also the static_cast is not need, but good job on using it. Also be careful of using "<=". The next thing you know the for loop starts at (0) zero and the "<=" will do 1 more loop than you are expecting.
When the while loop first starts "firstNumber" should be the first number entered by the user. The last line of the loop should be adding 1 to "firstNumber" for the start of the next iteration.
A note:
For some reason when posting code here the tab key used for indenting tends to be exaggerated. If you are using an IDE for you code change the setting for the tab key to use spaces, 4 is usually normal.
A really good start to the program. It just need a few adjustments.
Andy