Hey guys,
I need help with the switch statement at the bottom of the program. Error is that the expression must have a constant value and that the value of the variable 'physics' cannot be used as a constant.
Switch statement is incomplete btw.
In this case, would you recommend me to use an if statement instead?
Here's the code anyway.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int physics;
int maths;
int acc;
int phy_points;
int maths_points;
int acc_points;
int num = 1;
char option = 'N';
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
if (physics > 100 || physics < 0)
{
for (;;)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
if (100 >= physics && physics >= 0)
{
break;
}
}
}
cout << "Please enter your mark for maths: ";
cin >> maths;
cout << "\n";
if (maths > 100 || maths < 0)
{
for (;;)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for maths: ";
cin >> maths;
cout << "\n";
if (100 >= maths && maths >= 0)
{
break;
}
}
}
cout << "Please enter your mark for acc: ";
cin >> acc;
cout << "\n";
if (acc > 100 || acc < 0)
{
for (;;)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for acc: ";
cin >> acc;
cout << "\n";
if (100 >= acc && acc >= 0)
{
break;
}
}
}
switch (physics)
{
case (100 >= physics && physics >= 90):
}
system("pause");
return 0;
}
I realize you are just learning loops and such. Just thought I'd mention that placing an empty for loop inside the if statement can be accomplished a little bit more cleanly with a single while loop:
1 2 3 4 5 6 7 8 9 10 11 12
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
while (physics > 100 || physics < 0)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
}
Also you could avoid repeating your code and your condition multiple times. Generally speaking, when you have multiple lines doind the same you're doing something wrong. If you don't know the do-while you could do this:
1 2 3 4 5 6 7 8 9 10
int physics = -1;
while (physics > 100 || physics < 0)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
}
This way you don't have to repeat your if condition and the entire input request.
If you have done the do while it can get even better:
1 2 3 4 5 6 7 8 9 10 11
int physics;
do
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for physics: ";
cin >> physics;
cout << "\n";
}
while (physics > 100 || physics < 0);
With a do while you will perform the action inside the cycle before checking its result, which allows you to get the input at least once before the loop condition is verified.
Anyway since you're already using break, there's no reason at all to follow your structure
1 2 3 4 5 6 7 8 9 10 11
//pseudocode
get input
if input not correct
{
forever
{
get input
if input correct
{break}
}
}
You're repeating the same things over and over, which is not good. Sticking with a forever loop you could do way better.
1 2 3 4 5 6 7 8 9 10 11 12
int physics;
for(;;)
{
cout << "Data value is out of range, please try again.";
cout << "\n";
cout << "Please enter your mark for physics: ";
cin >> physics;
if (100 >= physics && physics >= 0)
{
break;
}
}
As you can see there's no reason to repeat the if and the input request twice even using forever with break.