Trouble shooting Nested Loops and statements

Having trouble with Odd Portion of this code. I am over looking how I have written the code to have the desired effect which is listed below. Ive nailed the even number portion of code, just cant figure out what I am on the Odd portion. Any tips will work? What am I over looking.

I also understand alot of the code can be re-written with more advanced C++ but in my class we are up to loop statements. This is the cap of my knowledge. No arrays etc.

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
  #include <iostream>

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!" << endl;
cin >> firstNumber >> secondNumber;
cout << endl << endl;

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);}
        firstNumber = firstNumber + 1;
     }
////////////////Having trouble with odd portion of this code below.
        else {                   //If firstNumber is odd
            if(firstNumber == inputFirstNumber){
            firstNumber = firstNumber + 2;
            sqrRoot = sqrRoot + (firstNumber * firstNumber);
                cout << firstNumber << " ";
                firstNumber++;
            }
            else{
                sqrRoot = sqrRoot + (firstNumber * firstNumber);
                cout << firstNumber << " ";
                firstNumber++;}
  }
}

    cout << endl;
    cout << "The sum of the even numbers is: " << sum << endl; // Sum of even numbers from between two inputted numbers

    for (int i = 1; i <= 10; i++){ // Square numbers between 1 and 10
    cout << i << " " << i * i << " ";
}
    cout << endl;
    cout << "Square roots of all odd numbers added up: " << sqrRoot << endl; // Display of odd numbers Square Root

    for(int j = 1; j <= 26; j++){ // Utilize For Loop To Print Alphabet Capitlized
    cout << static_cast <char>(j + 64) << " ";
}



    return 0;
}
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
Thank you for the feed back. I made some adjustments so I was able to answer all the questions wanted with your feed back. I did not want to copy and paste your code, but definitley learned some things from what you were doing. What I did was just created a second loop after initial loop to handle remaining questions. This allowed my code to solve each question required. Thank you Andy.

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
#include <iostream>

using namespace std;

int main() {
//Prompt the user to input two integers: firstNum and secondNum
//(firstNum must be less than secondNum).
//Output all odd numbers between firstNum and secondNum.
//Separate each number with a space
//Output the sum of all even numbers between firstNum and secondNum.
//Output the numbers and their squares between 1 and 10.
//Output the sum of the square of the odd numbers between firstNum and secondNum.
//Output all uppercase letters.
//Separate each letter with a space
int firstNumber;
int inputFirstNumber;
int inputSecondNumber;
int secondNumber;
int sqrRoot = 0;
int sum = 0;

cout << "Please input two integers, the second one being bigger than first one!" << endl;
cin >> firstNumber >> secondNumber;
cout << endl << endl;

inputFirstNumber = firstNumber;
inputSecondNumber = secondNumber;

    while(firstNumber <= secondNumber){
      if(firstNumber % 2 == 0){
        firstNumber++;
        sqrRoot = sqrRoot + (firstNumber * firstNumber);
        cout << firstNumber << " ";
      }
      else{   
      firstNumber = firstNumber + 2;
      if(firstNumber < secondNumber){
      sqrRoot = sqrRoot + (firstNumber * firstNumber);
      cout << firstNumber << " ";  
        }
      }
    }
    while(inputFirstNumber < inputSecondNumber){
      inputFirstNumber++;
      if(inputFirstNumber % 2 == 0 && inputFirstNumber != inputSecondNumber){
        sum = sum + inputFirstNumber;
      }
    }
    
 cout << "\nSum of all the even numbers is: " << sum << endl;       

for(int i = 1; i <=10; i++){
  cout << i << " " << i * i << " ";
}

cout << "\nSum of the square root of odd numbers between two numbers: " << sqrRoot;

cout << endl;
    for(int j = 1; j <= 26; j++){ // Utilize For Loop To Print Alphabet Capitlized
    cout << static_cast <char>(j + 64) << " ";
}

 


    return 0;
}
You really should take Andy's advice about indentation seriously. It will help you easily see the flow of control through your code, and well help you spot mistakes much more easily.
Hello kidd2100,

Your new code says the you have solved each question, but it is still not correct.

The first while loop is better and the first if statement will work, but the contents of the if statement are wrong. You should be summing all the even numbers not adding to "sqrRoot", which is a misleading name. Never mind the if statement will never be true, so it does not matter what code is inside there.

In the else statement you first add 2 to "firstNumber" skipping the first number that you should be checking. The next if statement I think works, but is not necessary. You are overthinking what you need to do.

When the else statement ends "firstNumber" has the value of 3 when it should have the value of 2.

Your variable "sqrRoot" leads me to believe that you want a square root of the number, but the code is the square of the number and in the end since you have missed the first number the answer is wrong.

The second while loop first is not necessary. This should have been done in the first while loop in the if statement that is never entered. The whole of this loop does not produce the correct answer because you are skipping the last even number.

With the changes I made to your original code and even some changes after I posted that code I have this output compared to your output.
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
Please input two integers,
the second one being bigger than first one!: 1 10

Odd  numbers are: 1 3 5 7 9

The sum of the even numbers is: 30

Num    Square
 1   -     1
 2   -     4
 3   -     9
 4   -    16
 5   -    25
 6   -    36
 7   -    49
 8   -    64
 9   -    81
10   -   100


The sum of all odd numbers squared is: 165

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 Press Enter to continue:
 Please input two integers, the second one being bigger than first one!
1 10


3 5 7 9
Sum of all the even numbers is: 20
1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
Sum of the square root of odd numbers between two numbers: 164
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Excuse the first line in the code block. I did this so the output block would not be to far to the right.

Check the numbers and see the difference.

The variables:
1
2
inputFirstNumber
inputSecondNumber

Are not needed because you are over thinking what you need to do.

I was thinking of something like this:

Get user input. 2 numbers.

Verify input. Is "secondNumber" > "firstNumber"?
  If not output error message.
  leave program.

While loop.

if even process even number
else process odd number.

add 1 to "firstNumber"

End while loop

Process the rest of the code.


Andy
Hello kidd2100,

You did not mention that you changed the comment section at the beginning of "main". When I finally noticed it I was able to make the changes needed to achieve the correct answers.

Andy
Andy,

I just wanted to take the time to thank you for helping me on this. I was very appreciative of the help you gave and it helped guide me in being able to complete project by utilizing tips given. I did not want to just copy and paste , so I did some thinking and reconfigured how I wrote my code. A lot of it was due to the statements you made on my thought process and that's what I was looking for.

I look forward to my future development.

Kidd2100
Hello kidd2100,

You are welcome. Glad it helped.

Andy
Topic archived. No new replies allowed.