CIN, COUT Undelcared Identifier - c++

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.

Related

namespace questions and difference between IDE's

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.

How can I fix my source code in my C++ program called Flix for Fun Profit Report?

I have coded a program in C++ for an assignment in my C++ Intro class but I have multiple errors in the program but I can't seem to figure out how to get all of the bugs out.
The program is supposed to ask the user for the name of the movie, the number of adult and child tickets sold, and calculate the gross box office profit, net box office profit and the amount paid to the distributor.
I can't seem to figure out how to initialize the variables adultTicketPrice and
childTicketPrice and I thought I declared them and am trying to figure out if they need to get initialized if I already declared them?
And how is the childTicket price out of scope?
And why am I getting the other errors and how can I fix them?
// Michael VanZant
// Flix for Fun Profit Report
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Create all double variables
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
adultTicketPrice = 12;
childTicketPrice = 7;
cout << fixed << setprecision(2);
// Create the string variable
string movieName;
// Get the name of the movie and store it in the movieName string
cout << "What is the name of the movie?";
getline(cin, movieName);
cout << "\n";
// Cin.ignore to ignore the string variable type
cin.ignore();
// Get the amount of adult and child tickets sold
cout << "How many adult tickets do you want to buy?";
cin >> adultTicketsSold;
cout << "\n";
cout >> "How many child tickets did you want to buy?";
cin >> childTicketsSold;
cout << "\n";
// Calculate the amount of gross box office profit and display it
grossBoxProfit = (childTicketsSold * childTicketPrice) + (adultTicketsSold * adultTicketPrice);
cout << "Gross Box Office Profit: $" << grossBoxProfit;
cout << "\n";
// Calculate the net box profit amount and display it
netBoxProfit = grossBoxProfit * .20;
cout << "Net Box Profit Amount: $" << netBoxProfit;
cout << "\n";
// Calculate the amount paid to distributor and display it
amtPaidDist = grossBoxProfit - netBoxProfit;
cout << "Amount Paid to Distributor is: $" << amtPaidDist;
return 0;
}
When the compiler says "expected initialiser", it has nothing to do with these lines:
adultTicketPrice = 12;
childTicketPrice = 7;
which are actually assignments, not initialisations (though some old C terminology would call the first assignment an initialisation).
No, it's because it thinks you're still on this line, providing declarations and (optionally) initialisers:
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
That's because you didn't put a ; at the end of it.
Also:
cout >> "How many child tickets did you want to buy?";
You meant <<.
Fixing those two little typos, the code compiles.

Why is this program outputting this number?

I just starting learning C++. Here is my code:
#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 did you get paid per hour?";
cin>> pay;
// calculates the pay
pay = hours * rate;
// Display the pay
cout<<"You have earned $" << pay <<endl;
return 0;
}
I have no idea why this program is outputting the wrong numbers:
How many hours did you work?19
How much did you get paid per hour?15
You have earned $4.03179e-313
Maybe I installed the IDE wrong (I am using Eclipse)?:
I think your cin >> pay line is wrong, because you follow it up with pay = hours * rate. Since rate is never assigned to, it just gets junk data in memory, so the output is undefined. Change cin >> pay to cin >> rate
firstly, initializing variables before using them is a good practice so you shoud try this :
double hours = 0, rate = 0, pay = 0;
secondly, you need to replace pay by rate in :
//Get the hourly pay rate
cout << "How much did you get paid per hour?";
cin >> rate;
Amran AbdelKader.
There are two issues with your code.
Your second cin>> call is initializing pay when it should be initializing rate instead:
cin >> rate;
Or, if you are using C++11 or later, you can use std::get_money() instead:
cin >> get_money(rate);
Your cout<< is outputting a double (a floating-point data type) using its default formatting, which may not be suitable for your needs. To display money values, you should be explicit about the formatting, eg:
cout << "You have earned $" << fixed << setprecision(2) << pay << endl;
Or, if you are using C++11 or later, you can use std::put_money() instead:
cout << "You have earned " << put_money(pay) << endl;

Code has error at the last function

#include <iostream>
using namespace std;
void calculate_bill(double& pizza_price);
int main()
{
cout << "Welcome to Domino's Pizzaria!\n";
cout << "\nEnter the price of the pizza: ";
double price;
cin >> price;
calculate_bill(price);
}
void add_tax(double& pizza_price)
{
pizza_price *= 0.085;
}
void calculate_tip(double& pizza_price)
{
pizza_price *= 0.15;
}
void calculate_bill(double& pizza_price)
{
add_tax(pizza_price);
calculate_tip(pizza_price);
double price = pizza_price * add_tax * calculate_tip;
cout << "The pizza with taxes and tip, your total comes to " << "$" << fixed << setprecision(2) << price;
}
The output should be like this:
Welcome to Domino's Pizzaria!
Enter the price of the pizza: 12.99
The pizza with taxes and tip, your total comes to $16.21
The problem is that every time I run the code it says I have an error at the "void calculate_bill". It also says that I have an error at setprecision in which I have no clue why. Any ideas on what I did wrong? I am still learning on how to call functions so could someone tell me if I called it correctly?
For using the std::setprecision you need to #include <iomanip>.
Also, you are sending the pizza price as a reference, this means you are actually changing it value on every function call. You then, recalculate it at the last statement in calculate_bill(), which is wrong. Try calling all functions but then just display the price from main().
And btw, you surely meant pizza_price *= 1.085; and pizza_price *= 1.15 didn't you? Otherwise the price will drop extremely fast for every pizza. Which we may like but definitely not your professor..

double functions, fstreams, and more fun

The big picture is to take information from one file to another file. I've done that and it works well. The next thing I need to do is find the highest value from the new file. The basic format is like this: I have a bunch of "workers", I have the days they have worked, the hours & minutes they've worked. In the new file I have it formatted to show the pay rate (I enter the value as a cin) and then I have the total amount of money they made (everyone makes the same amount.. that which is in the cin). For some reason, I cannot make a function (that works) that will extract the persons name that makes the most money, and how much money that is. I got a little frustrated and was trying whatever I could so the new function is sloppy and ridiculous, but I was hoping you could help. To keep this a general question and not a specific question, I was wondering if you guys could just explain how to do something like this (finding the highest value & outputting it WITH the name of the person.. so essentially a double for the money and a string with the persons name) using parts of my code as an example:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void getandprintaddress (ifstream&, ofstream&); // this function will be used to get the data from one file and input it into the next with the total amount of money made from each person as well as the next function
double highestpaid(double& highesttotal, double& totalpay, string highestpaidemp, string name); // this function is what will be used to find the highest amount of money and the person who has it
void name(); // This is to input my name in the file.
struct employees // This is the structure with all of the information that will be used to calculate everything.
{
string name; // person's name
string highestpaidemp; // highest paid employee
string address; // the address of the employee
string address2;
int days; // days worked
int hours; //hours worked
double minutes; // minutes worked
int total_hours; // total hours worked
double totalpay; // total pay
double payrate; // pay rate
double total_minutes; // minutes worked
double total_time; // total hours and minutes worked
double highesttotal; // the highest total amount of money made between all employees.
};
int main(){
ifstream inputFile;
ofstream outputFile;
getandprintaddress(inputFile, outputFile);
return 0;
}
void getandprintaddress(ifstream& inputFile, ofstream& outputFile) // the main function that will be used to get and output all the information.
{
struct employees();
ifstream getdata;
ofstream outdata;
int employees_t;
employees employeeinfo;
string inputfile;
string outputfile;
cout << "What is the name of your input file? "<<endl; // this will be the file you open
cin>>inputfile;
getdata.open(inputfile.c_str());
if(getdata.fail()){ // this is meant to be used if someone enters a file that isn't there
cout << "The input file has failed to open. \n";
exit(1);
}
cout << "What is the name of your output file? \n"; // what you want the title of the new file to be.
cin>>outputfile;
outdata.open(outputfile.c_str());
if(outdata.fail())
{
//This is if the new file is invalid
cout << "The outputfile failed to open. \n";
exit(1);
}
cout << "How many employees are there? \n" ;
cin >> employees_t; // Enter how many employees there are
cout << "What is the employees hourly payrate? \n";
cin >> employeeinfo.payrate; // how much each employee makes.
for ( int info = 0; info < employees_t; info++)
{
employeeinfo.highesttotal = 0.0; // this will be needed for calculating the highest paid employee
employeeinfo.total_minutes = 0.0; // This is needed to calculate total minutes
employeeinfo.total_hours = 0; // Same as the total_minutes, but for hours instead.
employeeinfo.total_time = 0.0; // Needed to calculate total time
string line1;
getline(getdata, employeeinfo.name);
outdata << "Name: " << employeeinfo.name <<endl; // employees name
getline(getdata, employeeinfo.address);
outdata << "Address: \n"; // Employees address
outdata<< employeeinfo.address <<endl;
getline(getdata, employeeinfo.address2);
outdata <<employeeinfo.address2 <<endl;
getdata >> employeeinfo.days;
outdata << "Days worked: " <<employeeinfo.days << endl; // Days worked
for (int totalworked=0; totalworked<employeeinfo.days; totalworked++)
{
// Because the employees work different amount of days, this loop is needed to post the individual information from each employee
getdata >> employeeinfo.hours >> employeeinfo.minutes;
employeeinfo.minutes = employeeinfo.minutes / 60;
employeeinfo.total_hours = employeeinfo.total_hours + employeeinfo.hours;
employeeinfo.total_minutes = employeeinfo.total_minutes + employeeinfo.minutes;
employeeinfo.total_time = employeeinfo.total_minutes + employeeinfo.total_hours;
employeeinfo.totalpay = employeeinfo.total_time * employeeinfo.payrate;
outdata << employeeinfo.hours <<" hours "<< employeeinfo.minutes*60 << " minutes"<< endl;
}
outdata << fixed << showpoint << setprecision(1); // Setting the total hours worked to 1 decimal so that to include the minutes
outdata << "Total hours worked: "<<employeeinfo.total_time<<endl; // Total hours worked
outdata << fixed << showpoint << setprecision(2); // Setting the decimal to two places
outdata << "Hourly pay: $"<<employeeinfo.payrate << endl; //Hourly pay
outdata << "Gross pay: $" <<employeeinfo.totalpay <<endl; // Gross pay
getline(getdata,line1);
getline(getdata,line1);
outdata << "\n";
double highestpaid(employeeinfo.highesttotal, employeeinfo.totalpay);
}
};
double highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name)
{
if (highesttotal < totalpay)
{
highesttotal = totalpay;
highestpaidemp = name;
}
return highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name);
};
Ok, I'm going to answer your general question rather than spend too long searching through your code, but hopefully should work for both.
You have your struct employee
Stick them all in a vector of employees (which i'll call workers)
vector<struct employee> workers
using a vector means you can just keep on adding more to the end, but an array would work too if you had a fixed number of workers
now each worker can be identified by an integer index, 0 to n-1, where n is number of workers. Now can just iterate through (quicker methods if very large number of workers but probably fine here) to find highest salary
double highestPay = 0;
int highestPaidIndex = 0;
for(int i = 0; i++; i < n)
{
if(workers[i].totalpay > highestPay)
{
highestPay = workers[i].totalpay;
highestPaidIndex = i;
}
}
Now highestPaidIndex is the index of the highest paid employee, who has name workers[highestPaidIndex].name and total earnings workers[highestPaidIndex].totalpay
Hope that solves both your general and specific question