I'm new to c++ I'm trying to do a simple programme that calculates the weekly pay. See source code below. When I run the programme it lets me enter the workHours but when I click enter the programme closes n does not proceed to the rest of the code. Not sure where I'm going wrong please help.
#include <iostream>
using namespace std;
int main ()
{
int workDays;
float workHours, payRate, weeklyPay;
workDays = 5;
payRate = 38.55;
cout<< "Enter the number of hours worked: ";
cin >> workHours;
cin.ignore ();
weeklyPay = workDays * workHours * payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << '\n';
return 0;
}
The DevC++ doesn't pause the program after it ends you have to add this feature by yourself:
First include conio.h
#include <conio.h>
Then at the bottom of the main add getch()
getch();
What compiler you are using as i am to run this code?
I am using visual studio 10 as IDE and it is working for me.
Related
Ive been following along with some c++ tutorials on youtube and I cant seem to get this code to work, I use code blocks and this person uses visual c++. Is there a difference between them somehow? before I did cin >> std::Asalary; she asked what whould happen if you ran this code, she got an error and I got zero which makes sense, so im kinda lost on both of these problems. any help whould be epic, thanks in advance.
#include <iostream>
using std::cout;
using std::endl;
using std::string;
namespace main1 {
double Asalary;
double MonthSal = Asalary/12;
}
int main()
{
cout << "enter your annual salary" << endl;
cin >> main1::Asalary;
cout << main1::MonthSal << endl;
}
You have at global scope:
namespace main1
{
double Asalary;
double MonthSal = Asalary/12;
}
and then you do:
cin >> main1::Asalary;
cout << main1::MonthSal << endl;
So, you seem to expect main1::MonthSal to magically be annual salary divided by twelve, because you told the program once.
That's not how C++ works. double MonthSal = Asalary/12; is executed only once, before annual salary is entered.
Then, if you change annual salary, the monthly salary will not update.
I know that's not the question you are asking, but this is important and will hinder your understanding of C++ in a significant way.
I used to make basic program of C/C++ in vs code. After 2-3 weeks. in it's terminal it is not showing variable in output window. after making program when i run code.when i hit enter key it shows in terminal--it is not an error but code is not working.
PC C:\User\Owner\Desktop\C-C++>
WHEN I HIT ENTER IT AGAIN SHOW
PS C:\User\Owner\Desktop\C-C++>
IT DOES NOT SHOW 'ENTER VALUE OF B=='
IN THE TERMINAL OUTPUT WINDOW:-
for ex-:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "ENTER VALUE OF A==";
cin >> a;
cout << "ENTER VALUE OF B==";
cin >> b;
c = a + b;
cout << "ADDITION OF VALUES" << c;
return 0;
}
IN THE TERMINAL WINDOW IT SHOWS.
ENTER VALUE OF A==5 ( WHEN I PRESS ENTER )
THEN IT SHOWS
PS C:\User\Owner\Desktop\C-C++>5
5
AGAIN
PS C:\User\Owner\Desktop\C-C++>
PLEASE ANSWER THIS PROBLEM :(
So I perused some of the other articles, but I can't seem to find a reason to why this won't work. I am new to C++ so be kind please.
// User Pay.cpp : Defines the entry point for the console application.
// This program calculates the user's pay.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
cout << "how many hours did you work? ";
cin >> hours;
// Get the hourly pay rate.
cout << "How much do you get paid per hour? ";
cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
cout << "You have earned $" << pay << endl;
return 0;
}
You dont need to include #include "stdafx.h".
Also a better practice for the future is not to include the whole std library ("using namespace std"). Instead of this you can call directly std::cout, std::cin etc...
Also a system("PAUSE") call at the end of the code before "return 0" would be helpful (in your example). So the console doesn't close when the program execute and you can see your result.
Code example:
#include <iostream>
//using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
std::cout << "how many hours did you work? ";
std::cin >> hours;
// Get the hourly pay rate.
std::cout << "How much do you get paid per hour? ";
std::cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
std::cout << "You have earned $" << pay << std::endl;
system("PAUSE");
return 0;
}
Try creating an empty project (uncheck precompiled headers). then copy your code but delete #include "stdafx.h".
It looks as if you had an error, and then added:
`using namespace std;`
There should be no error now.
I'm trying to write a simple c++ program that takes a number of miles as input and returns a conversion to kilometers. Here is my kilo.cpp:
#include <iostream>
using namespace std;
int main()
{
double miles;
double km_to_mile = 1.609;
cout << "Please enter number of miles: ";
cin >> miles;
miles *= km_to_mile;
cout << "That is " << miles << " kilometers.";
}
The program compiles, but when I run it it crashes when it tries to output the double. The last line I get in console is "That is " and I get the Windows error message "kilo.exe has stopped working."
I've tried a few other code samples and whenever I try to use cout to output a double value, the program crashes with the same error. I assume this is some problem with my compiler (mingw on Windows 8.1), but I've tried reinstalling the compiler a few times now to no avail.
My following basic C++ code isn't working. When I run it in Visual Studio express, it just closes the command prompt after I enter the second number. Thanks in advance.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, answer;
cout << " Enter a number: ";
cin >> num1;
answer = num1 * num1;
cout << " the sum of the number is: " << answer << endl;
cout << "Enter a 2nd number";
cin >> num2;
answer = answer + num2;
cout << "The sum of the two numbers is: " << answer << endl;
cin.get();
return 0;
}
The problem is that there are characters left in the input buffer.
Anyway you shouldn't add "tricky" commands to keep the console open (you have to remember to remove them from the "production" code).
You can run your program Without debugger mode (CTRL + F5) and Visual Studio will keep the console application window open until you press a button (just check the settings in Project -> Properties -> Linker -> System -> Sub System -> Console (/SUBSYSTEM:CONSOLE)).
Of course, if you are debugging (F5), a breakpoint on the return 0; is the best option.
This is what is probably happening:
Last cin.get() reads the Enter key that you press after entering the second number. You need one more cin.get() so that the command prompt waits for you to press another key.