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:
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.
#include <iostream>
usingnamespace std;
int main()
{
int employeeid;
int hoursworked;
float hourlyrate, grosspay, taxamount, netpay;
floatconst 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
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;
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int employeeid;
int hoursworked;
float hourlyrate, grosspay, taxamount, netpay;
floatconst 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 ;)
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.
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)
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.
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.