Jump to content

Expression Must Have A Constant Value (Need Solution)

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Venix

Venix

    Newbie

  • Members
  • Pip
  • 4 posts
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;
}

#2
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
hmm - I don't like visual c++ and never used it,but a potential mistake I saw is:

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
Master674

Master674

    Newbie

  • Members
  • Pip
  • 4 posts
A float should be enough.

#4
Venix

Venix

    Newbie

  • Members
  • Pip
  • 4 posts

Master674 said:

A float should be enough.
So should i change int Car etc to a float data type

#5
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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.

#6
Venix

Venix

    Newbie

  • Members
  • Pip
  • 4 posts
Going to test some things might be able to fix it.

#7
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Your problem is pretty simple.

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
Venix

Venix

    Newbie

  • Members
  • Pip
  • 4 posts

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.

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
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
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.

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
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

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.
Not 100% true. Floats (4 bytes) can hold 7 or 8 decimal numbers and doubles (8 bytes) can hold 15.

@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