I need help with my code!

My code isn't working, an error about uninitialized variable keeps showing up. SpaceVechiclesQuantity. Also want to know if i am heading the right direction with my code. I'm not asking for anyone to do my homework, i just need help.

*******************************************************************************
It is the year 2035, the MarsX Space Vehicles Company has recently made its MXSV-2 single use Space Transportation Systems (STS) available to the general public. Anyone can purchase the STSs to travel to the nearest celestial bodies (government permits and fees not included). The company has asked you to write a program that calculates the amount (in $) the customers owe for the purchase of the STSs. First, your program should welcome the customer with some fancy message. Then, the program must present the customer with a menu with two options as follows:
Please Enter One of the Following:
1- Purchase MXSV-2 STS
2- Quit
• If the customer chooses option 1, prompt him/her asking how many STSs he/she would like to purchase AND if he/she is a member of the Interplanetary Travelers Club. Display the amount he/she owes, including some fancy thank you message, keeping the following in mind:
o If customer purchases 4 STSs or less, the cost of each STS is $1.0 * 107.
o For all purchases above 4 STSs (5 and above), the cost of each STS is $8.0 * 106.
o If the customer is a member of the Interplanetary Travelers Club, he/she receives an extra 10% discount and free fuel for one of the STSs (make sure to display a message informing the customer of this).
o Customers cannot be allowed to enter a number smaller than 1.
• The program will redisplay the menu until the customer enters option 2 (Hint: use do…while loop) .
• If the customer enters option 2, Quit the program.
• Validate for incorrect inputs (Hint: use while loops). If the user enters incorrect answers 5 times quit the program.
• Enjoy!



Sample outputs (please don’t make yours exactly like this one, use some creativity):
**************************************************
***** Welcome to MarsX Space Vehicles Online Store *****
**************************************************
Please Enter One of the Following:
1- Purchase MXSV-2 STS
2- Quit
1 [Enter]
How Many MXSV-2 STSs Would You Like to Purchase?
10 [Enter]
Are You a Member of the Interplanetary Travelers Club (y/n)?
y [Enter]
Thank You for Your Purchase: Your Total is $72,000,000.00. You Saved $8,000,000.00 Today!!!
*** You Get Free Fuel for One of Your STSs ***
Your free fuel voucher will arrive with the delivery of your first STS.

Please Enter One of the Following:
1- Purchase MXSV-2 STS
2- Quit
2 [Enter]
Press any key to continue …

Please help
********************************************************************************

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int option;
int SpaceVechiclesQuantity;
double totalCost;


//constants for menu options
const int Purchase_option = 1,
Quit_option = 2;

//display a menu and get options for the users
cout << "*******************************************************************\n\n"
<< "*** <(^v^)> Welcome to MarX Space Vehicles Online Store <(^v^)> *** \n\n"
<< "******** Where shopping is an out of this world experience ********\n\n"
<< "*******************************************************************\n\n"
<< "1 - Purchase MXSV-2 STS\n"
<< "2 - Quit and miss a life time experience!\n\n"
<< "Please enter one of the following options: ";
cin >> option;

//set output formatting
cout << fixed << showpoint << setprecision(2);

//Respond to the users menu selection
if (option == Purchase_option && SpaceVechiclesQuantity <= 4)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 1.0 * pow(10.0, 7.0);
}
if (option == Purchase_option && SpaceVechiclesQuantity >= 5)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 8.0 * pow(10.0, 6.0);
}
if (option == Quit_option)
{
cout << "Program ending. \n";
}
while (option < 5)
{
cout << "***ERROR***PROGRAM_SHUTTING_DOWN***";
option++;
}

system("PAUSE");
return 0;
}
My code isn't working, an error about uninitialized variable keeps showing up. SpaceVechiclesQuantity.


So it's a golden rule to always initialise all of the variables to something. The best place to do this is at it's declaration. It's best to wait until you hava sensible value to assign to it, otherwise set it to something obliviously wrong. Zero is not always a good idea, but often can be ok.

SpaceVechiclesQuantity should be unsigned, negative values don't make sense there.

Please use code tags: http://www.cplusplus.com/articles/z13hAqkS/
Thank you for the help!!
If you are using Windows as the development platform, here's a tip:

Use the current version of Visual Studio (Visual Studio Community 2017, Desktop Development).

Set the warning level to -W4
Menu Project => Properties => Code Analysis => General: set 'Warning Level' to 'Level4 (/W4)

In the project properties, enable code analysis on build
'Menu Project => Properties => C++ => General: check Enable Code Analysis on Build'

Also enable CppCoreGuidelines checks
(guidelines in https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md )
Menu Project => Properties => Code Analysis => Extensions: set 'Enable C++ Core Check' to 'Yes'

And you would get a lot of help from the compiler.

Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    const int Purchase_option = 1 ;

    int option;
    int SpaceVechiclesQuantity;
    double totalCost;

    std::cin >> option;
    if( option == Purchase_option && SpaceVechiclesQuantity <= 4 ) { totalCost = 1.0 * pow( 10.0, 7.0 ); }

    char cstr[] = "abcdef" ;
    cstr[9] = 'a' ;
}


From Compiler Warning level 4 -W4
hello.cpp(12): warning C4700: uninitialized local variable 'SpaceVechiclesQuantity' used
hello.cpp(15): warning C4789: buffer 'cstr' of size 7 bytes will be overrun; 1 bytes will be written starting at offset 9


Form Code Analysis
hello.cpp(15): warning C6201: Index '9' is out of valid index range '0' to '6' for possibly stack allocated buffer 'cstr'.
hello.cpp(12): warning C6001: Using uninitialized memory 'SpaceVechiclesQuantity'.
hello.cpp(15): warning C6386: Buffer overrun while writing to 'cstr':  the writable size is '7' bytes, but '10' bytes might be written.


From CppCoreGuidelines Check:
hello.cpp(11): warning C26494: Variable 'totalCost' is uninitialized. Always initialize an object. (type.5: http://go.microsoft.com/fwlink/p/?LinkID=620421 )
hello.cpp(9): warning C26494: Variable 'option' is uninitialized. Always initialize an object. (type.5: http://go.microsoft.com/fwlink/p/?LinkID=620421 )
hello.cpp(10): warning C26494: Variable 'SpaceVechiclesQuantity' is uninitialized. Always initialize an object. (type.5: http://go.microsoft.com/fwlink/p/?LinkID=620421 )

Last edited on
I managed to stop my first error but now when it runs and i input 1 or 2, nothing happens. It just keeps on saying please "enter one of the following options" and eventually quits.

Please forgive me if this seems stupid but I am new at this :(

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int option;
unsigned SpaceVechiclesQuantity;
double totalCost;


//constants for menu options
const int Purchase_option = 1,
Quit_option = 2;

//display a menu and get options for the users
cout << "*******************************************************************\n\n"
<< "*** <(^v^)> Welcome to MarX Space Vehicles Online Store <(^v^)> *** \n\n"
<< "******** Where shopping is an out of this world experience ********\n\n"
<< "*******************************************************************\n\n"
<< "1 - Purchase MXSV-2 STS\n"
<< "2 - Quit and miss a life time experience!\n\n"
<< "Please enter one of the following options: ";
cin >> option;

//set output formatting
cout << fixed << showpoint << setprecision(2);
{
cout << "How many MXSV-2 STS would you like to purchase? " << endl;
cin >> SpaceVechiclesQuantity;

//Respond to the users menu selection
if (Purchase_option == SpaceVechiclesQuantity >= 1 && SpaceVechiclesQuantity <= 4)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 1.0 * pow(10.0, 7.0);
}
else if (Purchase_option == SpaceVechiclesQuantity >= 5)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 8.0 * pow(10.0, 6.0);
}
else if (option == 2)
{
cout << "Program ending. \n";
}
while (option < 5)
{
cout << "***ERROR***PROGRAM_SHUTTING_DOWN***";
option++;
}
}
system("PAUSE");
return 0;
}
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int option;
unsigned SpaceVechiclesQuantity;
double totalCost;


//constants for menu options
const int Purchase_option = 1,
Quit_option = 2;

//display a menu and get options for the users
cout << "*******************************************************************\n\n" 
<< "*** <(^v^)> Welcome to MarX Space Vehicles Online Store <(^v^)> *** \n\n"
<< "******** Where shopping is an out of this world experience ********\n\n"
<< "*******************************************************************\n\n"
<< "1 - Purchase MXSV-2 STS\n"
<< "2 - Quit and miss a life time experience!\n\n"
<< "Please enter one of the following options: ";
cin >> option;

//set output formatting
cout << fixed << showpoint << setprecision(2);
{
cout << "How many MXSV-2 STS would you like to purchase? " << endl;
cin >> SpaceVechiclesQuantity;

//Respond to the users menu selection
if (Purchase_option == SpaceVechiclesQuantity >= 1 && SpaceVechiclesQuantity <= 4)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 1.0 * pow(10.0, 7.0);
}
else if (Purchase_option == SpaceVechiclesQuantity >= 5)
{
cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
cin >> SpaceVechiclesQuantity;
totalCost = 8.0 * pow(10.0, 6.0);
}
else if (option == 2)
{
cout << "Program ending. \n";
}
while (option < 5)
{
cout << "***ERROR***PROGRAM_SHUTTING_DOWN***";
option++;
}
}
system("PAUSE");
return 0;
}
What is this if statement for? It doesn't seem to make sense. Purchase_option
Line 35: if (Purchase_option == SpaceVechiclesQuantity >= 1 && SpaceVechiclesQuantity <= 4)

lines 31/32 and 37/38 ask the same thing.

Line 41: else if (Purchase_option == SpaceVechiclesQuantity >= 5) What is this doing as well? If you can explain the if statements to me i may be able to help. They dont look completely correct to me.
Okay so its supposed to be that if they purchase 4 or less items they get it at a certain price or if they purchase 5 or more they get it at another price.
If you want to know if they want to purchase one or not it would be better to use an if else statement.

Also what is the opening bace on line 30 for? it looks like it finally closes on line 56. Indenting your code (like in my example) makes the code easier to read as well as spot errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(option == Purchase_option)
{
     //Do all your purchase coding here
     //Like asking how many they want to buy
     cout << "How Many MXSV-2 STSs Would You Like to Purchase?";
     cin >> SpaceVechiclesQuantity;
     //Check to see if they purchased more than 4
     if(SpaceVehicleQuantity > 4)
     {
          //You get discount!
     }
     else if (SpaceVehicleQuantity > 0 && SpaceVehicleQuantity < 5) //must buy more than 0
     {
          //You get a different price
     }
}
else
{
     //leave some sort of bye message because they aren't interested in buying. 
}
Last edited on
sorry it was an extra brace on my part. So for the final else i would put that for my option 2 so the user would be able to quit. Thank you for the help
Nooo This if statement should be at the very beginning because what if they immediately say they are not interested? They would have to go through your entire program until the final else statement.

This should be used every time you ask if they want to purchase something.

Feel free to make the changes and post the code if you get any errors.
Thank you so much for your help and I will because every time I feel I've figured something out another thing comes up on the assignment. :)
Topic archived. No new replies allowed.