Need solution to this problem
So i am coding a simple C++ application for college that needs to accept a user number and name for security reasons and then it will allow someone to calculate their allowance depending on how many miles they have traveled in a certain type of vehicle during the week. Whilst coding the application i have come across an error that i have no idea how to solve visual c++ is stating that the expression must have a constant value.
Code Below I have Highlighted The Error
#include <iostream>
#include "conio.h"
#include <string>
#include <array>
using namespace std;
int Miles;
int Usernumber;
int Car = 0.45;
int Motorcycle = 0.55;
int Public_Transport = 0.65;
int Bicycle = 0.80;
int Travel_Type;
int Allowance(int x, int y);
string Name;
void main (){
string Username[20] = {"Mark", "John", "Jack", "Tom", "Sam", "George", "Lewis", "Jacob", "Ryan", "Adam",
"Sophie", "Olivia", "Emily", "Morgan", "Lucy", "Zoey", "Katherine", "Caroline","Jessica","Amy"};
a:;
cout << "What is your usernumber?: ";
cin >> Usernumber;
if (Usernumber >19){
cout << "Error - Highest usernumber value is 19" << endl;
goto a;
}
cout << "What is your name?: ";
cin >> Name;
if (Username[Usernumber] == Name){
cout << "How many miles did you travel this week?: ";
cin >> Miles;
cout << "How did you travel: Car, Motorcycle, Public_Transport, Bicycle?: ";
cin >> Travel_Type;
}
else{
cout << "Error - Incorrect usernumber or name!" << endl;
goto a;
_getch();
}
switch (Travel_Type) {
case Car: Allowance(Miles, Car);
case Motorcycle: Allowance(Miles, Motorcycle);
case Public_Transport: Allowance(Miles, Public_Transport);
case Bicycle: Allowance(Miles, Bicycle);
}
cout << "Your allowance is: " << Allowance(Miles,Travel_Type);
_getch();
}
int Allowance(int x, int y){
int Allowance = x * y;
return Allowance;
}
10 replies to this topic
#1
Posted 05 December 2011 - 10:31 AM
|
|
|
#2
Posted 05 December 2011 - 11:11 AM
hmm - I don't like visual c++ and never used it,but a potential mistake I saw is:
The type of the variables must be double.You can't assign 0.55 to an integer.
Idk if it helps.
int Car = 0.45; int Motorcycle = 0.55; int Public_Transport = 0.65; int Bicycle = 0.80;
The type of the variables must be double.You can't assign 0.55 to an integer.
Idk if it helps.
#3
Posted 05 December 2011 - 11:16 AM
A float should be enough.
#4
Posted 05 December 2011 - 11:20 AM
Master674 said:
A float should be enough.
#5
Posted 05 December 2011 - 11:31 AM
If you're planning to use numbers like 5.2;53.2 etc.. you have to use float.
If you need numbers like 5.36;6.33 you have to use double.
If you need numbers like 5.36;6.33 you have to use double.
#6
Posted 05 December 2011 - 11:32 AM
Going to test some things might be able to fix it.
#7
Posted 05 December 2011 - 12:02 PM
Your problem is pretty simple.
Read the error and look at the code which you have highlighted and you will see that in switch you have used variables instead of constants. It must be some constant value.
Quote
i have come across an error that i have no idea how to solve visual c++ is stating that the expression must have a constant value.
Read the error and look at the code which you have highlighted and you will see that in switch you have used variables instead of constants. It must be some constant value.
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#8
Posted 05 December 2011 - 12:15 PM
AKMafia001 said:
Your problem is pretty simple.
Read the error and look at the code which you have highlighted and you will see that in switch you have used variables instead of constants. It must be some constant value.
Read the error and look at the code which you have highlighted and you will see that in switch you have used variables instead of constants. It must be some constant value.
Thanks for the response, i still haven't completely fixed the application however the error i did have is solved. Ill have to probably re code my solution in a slightly different way however at least i understand what i did wrong before hand.
#9
Posted 05 December 2011 - 12:30 PM
Besides that,
Why you are using global variables? Better to make them local. Also you have declared several int type but assigned some fractional value which will be truncated and lose it's fraction. Use either float or double for fractional values.
No more void main() it's int main().
Instead of label & goto try some other logic. Like,
Use the strcmp() function to compare the strings
Remember that no variables in switch cases shall be used and even no strings. Use break statement to break from a case, if not, it will cause the following cases to execute after the matched case.
Use the default case -- so, if no case matches the default will execute
Hope this Helps!
Why you are using global variables? Better to make them local. Also you have declared several int type but assigned some fractional value which will be truncated and lose it's fraction. Use either float or double for fractional values.
int Miles; int Usernumber; int Car = 0.45; int Motorcycle = 0.55; int Public_Transport = 0.65; int Bicycle = 0.80; int Travel_Type; string Name;
No more void main() it's int main().
void main ()
Instead of label & goto try some other logic. Like,
bool choice = false;
do {
cout << "What is your usernumber?: ";
cin >> Usernumber;
if (Usernumber >19)
{
cout << "Error - Highest usernumber value is 19" << endl;
choice = true; // will make the [B]while[/B] loop
}
} while(choice);
Use the strcmp() function to compare the strings
if (strcmp(Username[Usernumber], Name) == 0) // returning 0 means they are equal
Remember that no variables in switch cases shall be used and even no strings. Use break statement to break from a case, if not, it will cause the following cases to execute after the matched case.
switch (Travel_Type) {
case 'a':
Allowance(Miles, Car);
break;
case 1:
Allowance(Miles, Motorcycle);
break;
case '+':
Allowance(Miles, Public_Transport);
break;
case ' ':
Allowance(Miles, Bicycle);
break;
}
Use the default case -- so, if no case matches the default will execute
default: cout << "Nothing!"; break; // is optional here
int Allowance(int x, int y)
{
return (x*y);
}
Hope this Helps!
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#10
Posted 05 December 2011 - 12:59 PM
Venix use code tags. Select you code and click #.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#11
Posted 05 December 2011 - 07:47 PM
alex1 said:
If you're planning to use numbers like 5.2;53.2 etc.. you have to use float.
If you need numbers like 5.36;6.33 you have to use double.
If you need numbers like 5.36;6.33 you have to use double.
@Venix
You can't have a function and a variable inside with the same name:
int Allowance(int x, int y){
int Allowance = x * y;
return Allowance;
}
Either change the name of the function, or better, change variable's name.strcmp function is not needed for std::strings because they already have overloaded == (comparison) operator.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









