Confused

Greetings,

I am new to C++ and am using Bloodshed. I have an assignment due tomorrow. We were kind of thrown into this and I cannot see what the issue with my code is. It compiles without errors, yet when I try to run the program through cmd.exe, it doesn't do anything. Double clicking on it on my desktop doesn't do anything, either. Here is my code:
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
#include <iostream.h>

using namespace std;
int main()

{  
       int employeeid;
       int hoursworked;
       float hourlyrate, grosspay, taxamount, netpay;
       float const TAXRATE = 0.10;
       
       cout<< "Sample Company Inc"<<endl;
       cout<< "Employee Payroll Program"<<endl;
       cout<< "________________________"<<endl;
       
       cout<<"Enter Employee ID Number:";
       cin>>employeeid;
       cout<<endl;
       
       cout<<"Enter Total Clocked Hours:";
       cin>>hoursworked;
       cout<<endl;
       
       cout<<"Enter Hourly Payrate: $";
       cin>>hourlyrate;
       cout<<endl;
       
       grosspay=hoursworked*hourlyrate;
       netpay=grosspay*TAXRATE;
       cout<<"Employee ID Number Is"<<employeeid<<endl;
       cout<<"Total Clocked Hours Are"<<hoursworked<<endl;
       cout<<"Hourly Payrate Is"<<hourlyrate<<endl;
       cout<<"Employee Grosspay This Week Is"<<grosspay<<endl;
       cout<<"Employee Netpay Is"<<netpay<<endl;
       return 0;
}//MAIN 
Last edited on
The code runs OK for me. It could be that the console window is just closing quickly after the program runs. There's a thread stickied at the top of the Beginner section (Console Closing Down) that discusses how to deal with this.

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

int main()
{
    int employeeid;
    int hoursworked;
    float hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.10;
    
    cout<< "Sample Company Inc"<<endl;
    cout<< "Employee Payroll Program"<<endl;
    cout<< "________________________"<<endl;
    
    cout<<"Enter Employee ID Number:";
    cin>>employeeid;
    cout<<endl;
    
    cout<<"Enter Total Clocked Hours:";
    cin>>hoursworked;
    cout<<endl;
    
    cout<<"Enter Hourly Payrate: $";
    cin>>hourlyrate;
    cout<<endl;
    
    grosspay=hoursworked*hourlyrate;
    netpay=grosspay*TAXRATE;
    cout<<"Employee ID Number Is"<<employeeid<<endl;
    cout<<"Total Clocked Hours Are"<<hoursworked<<endl;
    cout<<"Hourly Payrate Is"<<hourlyrate<<endl;
    cout<<"Employee Grosspay This Week Is"<<grosspay<<endl;
    cout<<"Employee Netpay Is"<<netpay<<endl;
    return 0;
}


Sample Company Inc 
Employee Payroll Program 
________________________ 
Enter Employee ID Number:4657
 
Enter Total Clocked Hours:40 

Enter Hourly Payrate: $10 

Employee ID Number Is4657 
Total Clocked Hours Are40 
Hourly Payrate Is10 
Employee Grosspay This Week Is400 
Employee Netpay Is40
either cin.get(); before return(0) or....

#include <fstream> (at the top!),
then create your output stream...
example: ofstream fout("output.txt");

and everywhere you typed cout, replace with fout. Then you can just look at the output file, instead of freezing the console.
Last edited on
First of all, switch out this line
#include <iostream.h>
with this line
#include <iostream>

And then at the end of your program, before return 0;
add these 2 lines
cin.ignore();
cin.get();

The first line will make sure that the Enter key is ignored when you input the payrate.
The second line will make the program wait until the user pushes the enter key, before ending the statement and jumping to the next which is return 0;
Thank you all for your help. Unfortunately, nothing is working.

#include <fstream> (at the top!),
then create your output stream...
example: ofstream fout("output.txt");

gives me errors. Perhaps I am putting ofstream on the wrong line.

Placing cin.ignore(); and cin.get(); before return0;, still does the same thing. I run payroll.exe in cmd.exe and it doesn't run /closes too fast.
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
#include <iostream>
using namespace std;

int main()
{
	int employeeid;
	int hoursworked;
	float hourlyrate, grosspay, taxamount, netpay;
	float const TAXRATE = 0.10;

	cout << "Sample Company Inc" << endl;
	cout << "Employee Payroll Program" << endl;
	cout << "________________________" << endl;

	cout << "Enter Employee ID Number:";
	cin >> employeeid;
	cout << endl;

	cout << "Enter Total Clocked Hours:";
	cin >> hoursworked;
	cout << endl;

	cout << "Enter Hourly Payrate: $";
	cin >> hourlyrate;
	cout << endl;

	grosspay = hoursworked*hourlyrate;
	netpay = grosspay*(1.0-TAXRATE); // the employee is not getting paid the taxes =P
	cout << "Employee ID Number Is " << employeeid << endl; // added spaces after so outputs nicer
	cout << "Total Clocked Hours Are " << hoursworked << endl;
	cout << "Hourly Payrate Is $" << hourlyrate << endl;
	cout << "Employee Grosspay This Week Is $" << grosspay << endl;
	cout << "Employee Netpay Is $" << netpay << endl;
	cin.get(); // if it does not pause with one, I just add another
	cin.get();
	return(0);
}


OR USING OUTPUT FILE

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

int main()
{
	int employeeid;
	int hoursworked;
	float hourlyrate, grosspay, taxamount, netpay;
	float const TAXRATE = 0.10;
	ofstream fout("output.txt");

	cout << "Sample Company Inc" << endl;
	fout << "Sample Company Inc" << endl;
	cout << "Employee Payroll Program" << endl;
	fout << "Employee Payroll Program" << endl;
	cout << "________________________" << endl;
	fout << "________________________" << endl;

	cout << "Enter Employee ID Number:";
	cin >> employeeid;
	cout << endl;

	cout << "Enter Total Clocked Hours:";
	cin >> hoursworked;
	cout << endl;

	cout << "Enter Hourly Payrate: $";
	cin >> hourlyrate;
	cout << endl;

	grosspay = hoursworked*hourlyrate;
	netpay = grosspay*(1.0-TAXRATE); // the employee is not getting paid the taxes =P
	fout << "Employee ID Number Is " << employeeid << endl; // added spaces after so outputs nicer
	fout << "Total Clocked Hours Are " << hoursworked << endl;
	fout << "Hourly Payrate Is $" << hourlyrate << endl;
	fout << "Employee Grosspay This Week Is $" << grosspay << endl;
	fout << "Employee Netpay Is $" << netpay << endl;
	cout << "Contents written to \"output.txt\"" << endl;
	cout << "Press enter to continue...";
	cin.get();
	cin.get();
	return(0);
}


"output.txt":
1
2
3
4
5
6
7
8
Sample Company Inc
Employee Payroll Program
________________________
Employee ID Number Is 1
Total Clocked Hours Are 40
Hourly Payrate Is $12.25
Employee Grosspay This Week Is $490
Employee Netpay Is $441


Now include <iomanip> and get that output looking fancy ;)

$5 please, I will send you my paypal.
Last edited on
Thank you. I directly copied the code and it still doesn't work. I would think that something is wrong with my compiler, though a simple program from my textbook is working. I will reboot to see if that makes a difference.
What is the error? That says a lot.
There is no error. It compiles without incident. I run cmd.exe and run payroll.exe, and it fails to run. For example:

c:\users\me\desktop>

payroll(exe)

c:\users\me\desktop>

What is frustrating is wildblue was able to compile and run it while I cannot and I do not understand the cause.

I do appreciate everyone's efforts. I want to figure this out and move on, learning more about coding. I will not let this discourage me.
Try visual studio 2k13. It's free if you are a student in college.
Visual Studio is downloading. In the meantime, I installed Code Blocks and everything is working. Thank you all for your help!

I have another question. how can I have the output value from this line,

cout << "Current Tax Rate Is " << TAXRATE << endl;

have a percentage symbol appear after it?
add << "%" before endl;
Awesome, thanks again!
Keep codeblocks.
Bloodshed dev-c++ is incredibly old and discouraged from our most respected users.
Another choice is to use a raw g++ (MinGW on windows) installation and use the command line or a compatible IDE (Codeblocks is, but I find it old and uncomfy. I use QT creator, with a x64 MinGW 4.9.1 installation, which correctly supports many C++11 features. Sadly enough, schools are ALWAYS behind (using namespace std? way to clutter your namespace: the std namespace is there exactly to avoid this, iostream.h? That's so pre-2003, and consider C++14 is coming out) which means you'll never use C++11 features)
using namespace std? way to clutter your namespace: the std namespace is there exactly to avoid this


Is there a chance you could explain this?
Polluting a namespace just to save a few keystrokes is generally a bad idea.

For more: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=using+namespace+std+c%2B%2B+bad
Thank you, although I think someone is splitting hairs here when we are talking about a simple school project. I can understand in the scope of a large program with intent to distribute, "using namespace std" COULD cause issues. Either way, any knowledge is power and I'm glad I learned this. Thanks again.

Scythifuge, what's the word?
I apologize for this late response, I did not see these last posts. I was review these threads to try to cement what i have learned so far. This is my last college term before graduation and it is the term with the largest amount of school work that i have ever experienced and I am getting wiped out.

I was able to get things worked out. Communication with my professor is difficult due to this being an online class. There are sometimes misunderstandings with assignment intructions which of course bring me back to these awesome and helpful forums.

I'm still utilizing "using namespace std;" as I ran into a couple issue without it. My professor wrote the textbooks we use and it seems odd that he is having us use antiquated techniques. It is almost discouraging as I want to learn in an up-to-date fashion.
It is not antiquated at all. The namespace eases the monotony of std::cin and std::cout for newbies. You'll understand why it's not the best idea in the future when working on a large program but you're still starting out. You'll probably be fine keeping it there the entire class if it was anything like my intro course.

Also, read C++ Primer. They go over it in the first chapter or two and on top of that, it'll teach you a lot that your professor probably skipped.
Last edited on
Topic archived. No new replies allowed.