console/cmd shuts down instantly !

Hi programmers ! I have resorted to everything from system ("pause") to cin.get() to even sleep. Yet I am unable to have the average at the end of the cmd screen. Help ?!

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;

//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);

int main()
{
//Declaring variables
int employees;
int total;
double average;

//Function call for first function
employees = numberEmployees();

//Function call for second function
total = numberDays(employees);

//Function call for last function prototype
average = averageDays(employees, total);


//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
return 0;
}

//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;

//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}

//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;

//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}

}
return total;
}

//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;

aver=(w*365)/t;
return aver;
}
http://cplusplus.com/forum/beginner/1988/
this post has been a permanent post since I-don't-remember, take a look at it.
Bring cin.ignore and cin.get functions before the return statement of you main function.

Aceix.
Try cleaning input buffer at the beginning, it is possible you have leftovers from previous execution, so when you run it that value makes problems. Also, cleaning input stream at the beginning of that while loop as well might do the work.
Try using something like this:
1
2
cin.get();
cin.ignore(1000,'\n');

I couldn't see any obvious errors in your code, but I found it extremely hard to read.
it works good for me i just add system pause and its good , idk
Works fine now guys thank you ! :)
Also, I am trying to write the output to a .txt file. I have the following code now for my main function:

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
#include <iostream>
#include <fstream>
using namespace std;

//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);

int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;

//Function call for first function
employees = numberEmployees();

//Function call for second function
total = numberDays(employees);

//Function call for last function prototype
average = averageDays(employees, total);


//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\CS140\Project3\output.txt");
system ("pause");
return 0;
}


But have the following errors when debug/compiling:
1>------ Build started: Project: DaysOut, Configuration: Debug Win32 ------
1> DaysOut.cpp
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'C' : unrecognized character escape sequence
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'P' : unrecognized character escape sequence
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'o' : unrecognized character escape sequence

1> DaysOut.vcxproj -> C:\Users\Nelson\documents\visual studio 2010\Projects\DaysOut\Debug\DaysOut.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I tried going to my C:/ drive and I don't find it. Any idea why ?
closed account (Dy7SLyTq)
anything \ is an escaped character so replace all of \'s with \\. also remove system("pause"). its horrible
Thanks. It does run and compile perfectly now. But I still don't see the output file when I navigate to the C:\ drive. I have also tried refining my search to output. still no luck. any suggestions ? Have I placed the code in the right area ?
use '/' to separate directories.

Also, use the console to execute your console program (or get a better IDE).

By the way, learn to indent.
I still don't get it :(
closed account (Dy7SLyTq)
post ur code
// Days Out is the programm that calculates the average number of days a company's
// employees are absent

#include <iostream>
#include <fstream>
using namespace std;

//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);

int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;

//Function call for first function
employees = numberEmployees();

//Function call for second function
total = numberDays(employees);

//Function call for last function prototype
average = averageDays(employees, total);


//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\\CS140\\Project3\\output.txt");
return 0;
}

//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;

//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}

//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;

//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}

}
return total;
}

//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;

aver=(w*365)/t;
return aver;
}
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Days Out is the programm that calculates the average number of days a company's
// employees are absent

#include <iostream>
#include <fstream>
using namespace std;

//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);

int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;

//Function call for first function
employees = numberEmployees();

//Function call for second function
total = numberDays(employees);

//Function call for last function prototype
average = averageDays(employees, total);


//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\\CS140\\Project3\\output.txt");
return 0;
}

//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;

//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}

//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;

//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}

}
return total;
}

//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;

aver=(w*365)/t;
return aver;
}
Thank You :)
Topic archived. No new replies allowed.