Hello, I have been wrestling with a problem code that keeps popping up when I run the program. It says "uninitialized local variable 'guest'". In this program guest is user input. So I don't have a number value for it until the user puts it in. I have tried getline, and just assigning a value to 'guest'. I have also rearranged the code so I can use cin.ignore(). I am not sure what else to try. Can someone give clues to what I am doing wrong?
//Enter the name of the event Fall Dinner
//Enter the customers first and last name
//Enter number of guest
//Enter number of minuets.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
usingnamespace std;
int main()
{
string event;
string name;
int minutes;
int guest;
int numberOfServers = guest / 20; //number of servers needed
double costServers = 18.50; //cost per server
double costPerson = 26.70; //adverage cost per person
double costFood = costPerson * guest; // total cost for food
double serversPay = numberOfServers * costServers; // total pay for servers
double total = costFood + serversPay;
cout << "Please enter the number of guest attending: ";
cin >> guest;
cin.ignore();
cout << "Enter the name of your event: "; //Name of the Event
getline(cin, event);
cout << "Please enter your name: ";
getline(cin, name);
cout << "Number of minutes for the event: ";
cin >> minutes;
cout << setw(36) << event << endl;
cout << "Event estimate for " << name << endl;
cout << "Number of servers " << numberOfServers << endl;
cout << "Cost of Food " << setprecision(2) << fixed << costFood << endl;
cout << "Cost for servers " << setprecision (2) << fixed << costServers << endl;
cout << "Total " << setprecision (2) << fixed << total << endl;
system("pause");
return 0;
}
int guest; // **** uninitialised
// *** assign a value to guest before trying to use its value
cout << "Please enter the number of guest attending: ";
cin >> guest;
cin.ignore();
int numberOfServers = guest / 20; // ok; guest has some valid integer value now
Your code runs on the shell here but for you just change line 18 to int guest = 0; and the (warning?) message should go away. It is good programming practice to initialize all variables.