Program stops with a binary error - c++

I have been asked to create a petrol pump program for my referral coursework and I'm having an issue with running it, at the moment this is the main thing the compiler is outputting while trying to compile the code full build dialogue here
1>m:\visual studio 2010\projects\referral\referral\main.cpp(56): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <string>
#include <Windows.h>
using namespace std;
int reciept();
int pump;
int petrol;
int main()
{
bool exit = false;
int code;
string p1w ("Waiting");
string p2w ("Waiting");
string p3w ("Waiting");
string p4w ("Waiting");
string p1r ("Ready");
string p2r ("Ready");
string p3r ("Ready");
string p4r ("Ready");
if (GetAsyncKeyState(VK_ESCAPE))
{
exit = true;
}
cout << "***************************************************" << endl;
cout << "*Liquid Gold v1.0.0 Last revised 18/07/13 *" << endl;
cout << "*The process of processing transactions is simple,*" << endl;
cout << "*activate a pump by entering its code shown below.*" << endl;
cout << "*After pump operation is verified (generally 10 *" << endl;
cout << "*seconds though this may vary) the attendant *" << endl;
cout << "* will be able to enter the amount of petrol to 3 *" << endl;
cout << "*decimal places which will then be converted based*" << endl;
cout << "*on a predetermined price (which can be altered in*" << endl;
cout << "*price.txt) and once this process is complete a *" << endl;
cout << "*receipt will be created (you will need seperate *" << endl;
cout << "*software in order to print this recipt) and the *" << endl;
cout << "*transaction will be terminated. *" << endl;
cout << "*© Simple Software Solutions 2013 *" << endl;
cout << "***************************************************" << endl << endl;
system("Pause");
while (exit == false)
{
cout << " Pump (1) - " << p1w << " Pump (2) - " << p2w << endl << endl << endl;
cout << " Pump (3) - " << p3w << " Pump (4) - " << p4w << endl << endl << endl;
cin >> "Please enter a pump code:" >> code;
if (code == 1)
{
swap (p1w, p1r);
pump = 1;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 2)
{
swap (p2w, p2r);
pump = 2;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 3)
{
swap (p3w, p3r);
pump = 3;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 4)
{
swap (p4w, p4r);
pump = 4;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else
{
cout << "Invalid pump code entered";
}
reciept();
{
ofstream transactions;
transactions.open ("reciept.txt");
transactions << "****************************************************/n";
transactions << " SALE /n";
transactions << "****************************************************/n /n";
}
}
return 0;
}
I've looked around and the only solution I can find for that error is including which I've already done and I can't see any other solution.
Anyone more observant than me care to have a look through and tell me where I'm going wrong?
Also I know my code is an inefficient mess and I apologise for that.

Change
cin >> "Please enter a pump code:" >> code;
to
cout << "Please enter a pump code: ";
cin >> code;
You need to change all the cin >> "string" in your code. This does not mean prompting user for input. Instead, you're actually trying to write into a string literal.

Just for some added color on top of Yang's answer, this is not a "binary error" as suggested in the title. The error message refers to binary'>>'. >> is a binary operator, and binary operators take two operands, one on each side. + and - are functioning as binary operators in the following:
1 + 2
var1 - var2
A unary operator takes just one operand. & and - are functioning as unary operators in the following:
my_pointer = &n;
int var3 = -5;
The important part in the error message you're getting:
binary '>>' : no operator found which takes a left-hand operand of
type 'std::istream' (or there is no acceptable conversion)
is the last bit, "or there is no acceptable conversion". There certainly is a >> operator which takes a left hand operand of std::istream, but there is no >> operator defined which takes a string literal on the right hand side, since string literals can't be assigned to. In this case, std::cin >> myvar takes stuff from std::cin and tries to put it into the variable myvar, but there's no way you can stuff anything into a string literal like "Please enter a pump code:", as that would be like trying to do:
"Please enter a pump code:" = 5;
which is obviously nonsense.

Related

cin.get() causing "no instance of overloaded function" error

I'm having a bit of issues with a C++ Project that I'm working on for a class. I keep getting an error message stating 'no instance of overloaded function'. I did some googling and it seems that everyone says that this error is caused by passing a string into the cin.get() function, but I'm using this function with a char, not a string. Visual Studio says the error is at: "cin.get(nameFull);" but I've defined nameFull as a char, not a string. Any help would be greatly appreciated, thank you for your time.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int MONTHS = 12;
const int RETRO_MONTHS = 6;
char nameFull[30]; // INPUT - Employee's full name
float salaryCurrent; // INPUT - Current annual salary
float percentIncrease; // INPUT - Percent increase due
float salaryNew; // OUTPUT - New salary after increase
float salaryMonthly; // OUTPUT - New monthly salary
float retroactivePay; // OUTPUT - Retroactive pay due employee
int count; // CALC - Counter for loop
for (int count = 0; count < 3; count++) {
cout << "What is your name?" << endl;
cin.get(nameFull);
cout << "What is your current salary?" << endl;
cin >> salaryCurrent;
cout << "What is your pay increase?" << endl;
cin >> percentIncrease;
salaryNew = salaryCurrent + (salaryCurrent * percentIncrease);
salaryMonthly = salaryNew / MONTHS;
retroactivePay = (salaryNew - salaryCurrent) * RETRO_MONTHS;
cout << nameFull << "'s SALARY INFORMATION" << endl;
cout << "New Salary"
<< setw(20) << "Monthly Salary"
<< setw(20) << "Retroactive Pay" << endl;
cout << setprecision(2) << fixed << setw(10) << salaryNew
<< setw(20) << salaryMonthly
<< setw(20) << retroactivePay << endl;
cout << "<Press enter to continue>" << endl << endl;
cin.get();
}
return 0;
}
nameFull is an array of char (more specifically char[30]) which decays to a pointer to character (char*). There is no overload of std::istream::get which accepts a single pointer to character, but there is one that accepts a pointer + the size of the buffer that you would like to read into.
So all you need to do is pass an additional parameter:
cin.get(nameFull, 30);

Why Is My Program Skipping The cin > > after the first cycle of the For Loop?

My program runs the first For loop correctly then skips the Cin's on the 2nd and 3rd cycle. Then when the loop is finished it goes on to calculate the BMI of the first index [0] and does this correctly and gives the right answer but then nothing for the index's 1 and [2] because no information was inputted because the cin's were skipped.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct Patient
{
double height;
double weight;
int age;
bool isMale;
};
int main()
{
Patient Patients[3];
for (int i = 0; i < 3; i++) {
cout << "Patient "<< i << " Height: ";
cin >> Patients[i].height;
cout << "Patient " << i << " Weight: ";
cin >> Patients[i].weight;
cout << "Patient " << i << " Age: ";
cin >> Patients[i].age;
cout << "Is Patient " << i << " Male True or False: ";
cin >> Patients[i].isMale;
cout << endl << endl;
}
cout << endl << endl;
for (int i = 0; i < 3; i++) {
float BMI = Patients[i].weight / (Patients[i].height *Patients[i].height);
cout << "Patient " << i << " Has A BMI of: " << BMI << endl << endl;
}
return 0;
}
This is the console where you can see after the first loop all the cin's are skipped but the first loop was correctly stored as it couted the BMI of the first index:
You see that you are having an error at the end of the loop. You can see from iterations 2 and 3 that cin is not behaving the same way each time. There are a couple of error state flags that come from ios that will help you see what's going wrong here. See iso::good for details. If you add those checks:
for (int i = 0; i < 3; i++) {
cout << "Patient " << i << " Height: ";
cin >> Patients[i].height;
cout << "Patient " << i << " Weight: ";
cin >> Patients[i].weight;
cout << "Patient " << i << " Age: ";
cin >> Patients[i].age;
cout << "Is Patient " << i << " Male True or False: ";
cin >> Patients[i].isMale;
cout << cin.good() << '\n';
cout << cin.eof() << '\n';
cout << cin.fail() << '\n';
cout << cin.bad() << '\n';
cout << endl << endl;
}
What you will see if that cin is no longer good, it is not eof it is fail, and it is not bad. While the fail bit is set, cin will not work. Hence you see the result. Looking at the chart in the link you see this:
Logical error on i/o operation
You were preforming an i/o operation of inserting "true" into a bool. The word true is probably stored as a character array or string, not a boolean. How should cin convert this to a boolean? You need to trap your input and convert it into a bool or switch to use an input that can be explicitly converted into a bool.
For example:
cout << "Is Patient " << i << " Male? (1 for Male, 0 for Female):";
cin >> Patients[i].isMale;
In this case the cin recognizes 1 and 0 as integers and can convert an integer into a boolean. 0 is false, everything else is true. Another option is to let the library do it and use boolalpha. You can read about it here.
This shows a larger issue. What happens if I write "two point five" as the answer to height? In this case we can assume some intelligence on the part of the user, but thinking about things like this will help write more robust code in the future.
You can fix your program in two ways.
Just input "0" or "1" in the male/female question instead of "true" or "false".
Change this line and continue to input "true" or "false":
cin >> boolalpha >> Patients[i].isMale;
Sources:
Cin and Boolean input
http://www.cplusplus.com/reference/ios/boolalpha/

C++ loop error that my professor can't figure out

I am currently taking a C++ programming class and am working on a project in which I have to create a fairly simple movie database. My code essentially works as intended yet in certain cases it causes the main menu to loop infinitely and I cannot figure out why. I brought this to my teacher and he cannot explain it either. He gave me a workaround but I would like to know if anyone can see the cause of the problem. Full code is as follows:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct MovieType
{
string title;
string director;
int year;
int length;
string rating;
};
MovieType addMovie() {
MovieType newMovie;
cout << "Movie title :";
getline(cin, newMovie.title);
cout << "Director :";
getline(cin, newMovie.director);
cout << "Year :";
cin >> newMovie.year;
cout << "Length(in minutes) :";
cin >> newMovie.length;
cout << "Rating :";
cin >> newMovie.rating;
cout << endl;
return newMovie;
}
void listMovie(MovieType movie) {
cout << "______________________________________" << endl;
cout << "Title : " << movie.title << endl;
cout << "Director : " << movie.director << endl;
cout << "Released : " << movie.year << endl;
cout << "MPAA Rating : " << movie.rating << endl;
cout << "Running time : " << movie.length << " minutes" << endl;
cout << "______________________________________" << endl;
}
void search(vector<MovieType> movieVector) {
string strSearch;
cout << endl << "Search title: ";
getline(cin, strSearch);
for (int c = 0; c < movieVector.size(); c++) {
if (movieVector.at(c).title == strSearch)
listMovie(movieVector.at(c));
}
}
int main() {
bool quit = 0;
vector<MovieType> movieVector;
while (quit == 0) {
char selection = 'f';
cout << "Main Menu:" << endl;
cout << "'a' - Add movie" << endl;
cout << "'l' - List movies" << endl;
cout << "'s' - Search by movie title" << endl;
cout << "'q' - Quit" << endl;
cout << "Please enter one of the listed commands:";
cin >> selection;
cin.ignore();
cout << endl;
if (selection == 'a')
movieVector.push_back(addMovie());
else if (selection == 'l') {
for (int c = 0; c < movieVector.size(); c++) {
listMovie(movieVector.at(c));
}
}
else if (selection == 's') {
search(movieVector);
}
else if (selection == 'q')
quit = 1;
}
return 0;
}
When an unexpected input type is entered during the addMovie function(like entering text for the int type year), it just runs through the function then loops through the menu infinitely. It appears to me that the code just stops even looking at the input stream. I have tried using cin.ignore() in many different places but it doesn't matter if there is nothing left in the stream it just keeps going.
I am using NetBeans to compile my code.
I really have no idea why it behaves like this otherwise I would offer more information but I am just curious as to why this happens, because as I said before, my professor doesn't even know why this is happening.
Any help or insight is greatly appreciated.
cin enters an error state where cin.fail() is true. In this state it just ignores all input operations. One fix is to clear the error state, but better, only use getline operations on cin, not formatted input.
E.g., instead of
cin >> newMovie.year;
… do
newMovie.year = stoi( line_from( cin ) );
… where line_from can be defined as
auto line_from( std::istream& stream )
-> std::string
{
std::string result;
if( not getline( stream, result ) )
{
// Throw an exception or call exit(EXIT_FAILURE).0
}
return result;
}
Disclaimer: code untouched by compiler.

Simple calculator issues (Not outputting answers)

just started reading a C++ book and one of the practice problems was to write a small calculator that takes as input one of the four arithmetic operations, the two arguments to those operations, and then prints out the results.
Sadly, the program works up until the user inputs the arithmetic option.
So if I chose to do multiplication, id write "Multiplication" and it was just stay there and not do anything after.
Image of the problem im having
#include <iostream>
#include <string>
using namespace std;
int main(){
// Simple calculator program
// Declaring three variables
float numberOne;
float numberTwo;
string operationOption;
// Asking the user which two numbers he/she will use
cout << "Enter the first number you would like to apply a arithmetic operation to: ";
cin >> numberOne;
cin.ignore();
cout << "Now enter the second number: ";
cin >> numberTwo;
cin.ignore();
// Using cin to input users selection
cout << "Enter the operation you want to perform." << endl;
cout << "The options you have are: " << endl;
cout << "Multiplication, Subraction, Division and Addition: " << endl;
cin >> operationOption;
cin.ignore();
cin.get();
// Where it all happens
if ( operationOption == "Multiplication" ) {
cout << "The first number multiplied by the second number is: " << numberOne * numberTwo << endl;
} else if ( operationOption == "Division" ) {
cout << "The first number divided by the second number is: " << numberOne / numberTwo << endl;
} else if ( operationOption == "Subtraction" ) {
cout << "The first number subtracted by the second number is: " << numberOne - numberTwo << endl;
} else if ( operationOption == "Addition ") {
cout << "The first number added to the second number is: " << numberOne + numberTwo << endl;
} else {
cout << "You entered an invalid option.";
};
}
Remove line :
cin.get();
will solve your problem

Check for non-numeric inputs in a C++ program

How do you check for non-numeric input using C++? I am using cin to read in a float value, and I want to check if non-numerical input is entered via stdin. I have tried to use scanf using the %d designator, but my output was corrupted. When using cin, I get the correct format, but when I enter, a string such as "dsffsw", I get an infinite loop.
The commented code was my attempt to capture the float, and type cast it as string, and check if it is a valid float, but the check always comes up false.
I have tried using other methods I have found on the message boards, but they want to use scanf in C and not cin in C++. How do you do this in C++? Or in C if it is not feasible.
while (!flag) {
cout << "Enter amount:" << endl;
cin >> amount;
cout << "BEGIN The amount you entered is: " << strtod(&end,&pend) << endl;
//if (!strtod(((const char *)&amount), NULL)) {
// cout << "This is not a float!" << endl;
// cout << "i = " << strtod(((const char *)&amount), NULL) << endl;
// //amount = 0.0;
//}
change = (int) ceil(amount * 100);
cout << "change = " << change << endl;
cout << "100s= " << change/100 << endl;
change %= 100;
cout << "25s= " << change/25 << endl;
change %= 25;
cout << "10s= " << change/10 << endl;
change %= 10;
cout << "5s= " << change/5 << endl;
change %= 5;
cout << "1s= " << change << endl;
cout << "END The amount you entered is: " << amount << endl;
}
return 0;
}
int amount;
cout << "Enter amount:" << endl;
while(!(cin >> amount)) {
string garbage;
cin.clear();
getline(cin,garbage);
cout << "Invalid amount. "
<< "Enter Numeric value for amount:" << endl;
}
I think you task relates to the so called defensive programming, one of it`s ideas is to prevent situations like one you described (function expects one type and user enters another).
I offer you to judge whether input is correct using method that returns stream state , which is good(),
so I think it will look something like this:
int amount = 0;
while (cin.good()) {
cout << "Enter amount:" << endl;
cin >> amount;