defining user input

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?

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
  //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>
using namespace 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;
}
1
2
3
4
5
6
7
8
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 
closed account (48T7M4Gy)
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.
Thank you both for your response. They worked out for me.
Registered users can post here. Sign in or register to post.