hi!!
I need a help with part of my assignment. My professor asks to write a program that uses switch, and while loop statements..
this is the Exercise

Write a program that uses switch, and while loop statements to present the following menu and perform the operations.

Our program is able to offer the following:
1.Output all odd numbers between two integers.
2.Output the sum of all even numbers between two integers.
3.Output the integer numbers and their squares between two integers.
4.Output the sum of the square of the odd numbers between two integers.
5.Exit this program.

and I have done the First , the second and the fourth
in the fifth I don't understand what does it mean to "exit this program" so I did it like I always do >>return 0;<<...

in the third one i did it <<with no errors>>like this

``````i=firstNum;
j=secondNum;
cout<<"\nNumber\tSquares";
//Begin loop (while)
while (i<=j)
{
cout<<"\n"<<i<<"\t"<<i*i;
i++;
}//end loop (while)``````

when I run it it does not give me anything ..

but when I do it like this

``````i=1;
j=10;
cout<<"\nNumber\tSquares";
//Begin loop (while)
while (i<=j)
{
cout<<"\n"<<i<<"\t"<<i*i;
i++;
}//end loop (while)``````

when I run it is gives me the right answer

and Thank you so much

this is the program that I wrote ... and if there is anything wrong I did<<without realizing>> please inform me

``````//header file
#include<iostream>
using namespace std;

int main()// Begin Function
{
//declare variables
int firstNum, secondNum;
int sum_even,odd_sumsquare,i,j;
sum_even=0;
odd_sumsquare=0;

//Prompt and read the First Number and The Secondfrom the user
cout<<"\nEnter First Number: ";
cin>>firstNum;
cout<<"\nEnter Second Number: ";
cin>>secondNum;

//and now the Calculations
cout<<"\nODD Numbers: ";
//Begin loop (while)

while (firstNum<=secondNum)
{
switch (firstNum%2)//Begin switch
{
case 0:
sum_even+=firstNum;
break;
case 1:
cout<<"  "<<firstNum;
odd_sumsquare+=firstNum*firstNum;
}//end switch
firstNum++;
}//end loop (while)

//output Numbers and squares between The First Number and The Second.
i=firstNum;
j=secondNum;
cout<<"\nNumber\tSquares";
//Begin loop (while)
while (i<=j)
{
cout<<"\n"<<i<<"\t"<<i*i;
i++;
}//end loop (while)

cout<<"\nSum of all even Numbers: "<<sum_even;
cout<<"\nSquares of Odd Numbers: "<<odd_sumsquare<<"\n";

return 0;
}//End Function``````

Thanks a lot....

You are changing `firstNum` in the loop above your problem code so by the time it gets there `firstNum` and `secondNum` will be equal, meaning the loop will never run.

Also your comments are excessive. This is common with people learning but you should think about removing things like "// Begin Function", "//declare variables", "//and now the Calculations", "//Begin loop (while)", "//end loop (while)", etc. Anything that can be seen by just looking at the code itself. Otherwise it clutters your code, making it more difficult to read, which is the opposite of what you want.

"//output Numbers and squares between The First Number and The Second." is closer to the kinds of comments you want. You want to describe what a piece of code does, not that you are using a while loop to do it (we can see the while statement).

You are changing `firstNum` in the loop above your problem code so by the time it gets there `firstNum` and `secondNum` will be equal, meaning the loop will never run.

Also your comments are excessive. This is common with people learning but you should think about removing things like "// Begin Function", "//declare variables", "//and now the Calculations", "//Begin loop (while)", "//end loop (while)", etc. Anything that can be seen by just looking at the code itself. Otherwise it clutters your code, making it more difficult to read, which is the opposite of what you want.

"//output Numbers and squares between The First Number and The Second." is closer to the kinds of comments you want. You want to describe what a piece of code does, not that you are using a while loop to do it (we can see the while statement).

so what should I do to make it run ?!

and Thank you so much for the advice
I will keep it in mind ^_^

You could declare a new variable that is equal to firstNum and use that in the first loop so that by the time you reach the second loop firstNum hasn't been changed.

it worked for me XD
Thank you so much =)

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.