simple program tells u how much milk costs whatever i dont get why i get this error "no match for 'operator>>' in 'std::cin??" im a beginner at c++ but still what the hell.
also this error: "In function 'int main()':"
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const double CARTONLOAD = 3.78;
const double CARTONCOST = 3.78 * .38;
const double CARTONPROFIT = 0.27;
int main()
{
double totalmilk = 0;
double milkcartonsneeded = 0;
double milkcost = 0;
cout << "Enter total amount of milk produced in the morning in Liters" << endl;
cin >> totalmilk >> endl;
milkcartonsneeded = totalmilk/CARTONLOAD;
cout << " Number of milk cartons needed to hold milk: " << milkcartonsneeded << endl;
milkcost = milkcartonsneeded * CARTONCOST;
cout << " The cost of producing milk is: " << milkcost << endl;
cout << " The profit for producing milk is: " << milkcartonsneeded * CARTONPROFIT - milkcost << endl;
return 0;
}
endl is an output stream manipulator. cin is an input stream. I'm not sure what you expect endl to do here:
cin >> totalmilk >> endl;
But it's wrong.
This is the problem
cin >> totalmilk >> endl;
It is giving error because of endl. Remove it.
operator<< has an overload that takes a function pointer to a function that receives an std::basic_ostream. This allows you to use "stream manipulators", i.e. std::endl, in a operator<< chain. This allows you to do the following for example:
std::cout << "hey.";
std::endl(std::cout);
std::cout << "hello.";
Because std::endl is just a function that takes a std::basic_ostream. However, it also returns one by reference (similar to operator<<), meaning it can appear in a chain, i.e. std::cout << std::endl.
Since std::cin is a std::basic_istream, you have incompatible arguments.
Related
How do I multiply a function to another function? and how do I properly use parameters?
I'm not exactly sure, I am really new to C++ with only about 14 weeks of class time.
Something I've tried would be creating a new function meant to multiply other functions and in the arguments I would put in the functions names.
For example:
float mealMath(numberOfAdults, mealChoosing){
//Rest of function
}
but I always get an error.Please explain how to fix this, this is a big obstacle in programming for me and I can't seem to grasp how to fix this or go about doing these things. Don't be to harsh on me for this.Thanks!
int numberOfAdults(){
int totalAdults;
cout << "Now how many adults will there be?: ";
cin >> totalAdults;
cout << "It seems there will be: " << totalAdults << " Adults." << endl;
while(totalAdults < 1){
cout << "Sorry there has to be a minimum of 1 adult!" << endl;
cout << "How many adults: ";
cin >> totalAdults;
}
return 0;
}
int numberOfKids(){
int totalKids;
cout << "Now how many Kids will there be?: ";
cin >> totalKids;
cout << "It seems there will be: " << totalKids << " kids." << endl;
while(totalKids < 0){
cout << "Sorry there has to be a minimum of 1 Kid!" << endl;
cout << "How many Kids: ";
cin >> totalKids;
}
return 0;
}
float mealChoosing(){
float cost;
string mealChoise;
cout << " " << endl;
cout << "Now, What meal will you be getting(D/S): ";
cin >> mealChoise;
if(mealChoise == "D"){
cout << "It seems you have selected the Deluxe Meal plan for everyone!" << endl;
cost = 25.95;
}
if(mealChoise == "S"){
cout << "It seems you have selected the Standard Meal plan for everyone!" << endl;
cost = 21.75;
}
cout << " " << endl;
return cost;
}
One expected result is I want to multiply the input that the user gives in function "numberOfAdults" to the input a user gives for "mealChoosing"
So I want numberOfAdults * mealChoosing but I want that done in a different function so
"float total(){
float totalBill;
totalBill = numberOfAdults * mealChoosing;
cout << totalBill;"
or something along those lines. I can't complete this project because I can't for some reason properly give the functions the proper information needed in parameters.
In this case(and most) you should not declare a function whose parameters are functions. Instead declare mealMath with an integer and a float input:
float mealMath(int a, float b){/*Your code here*/}
And then later call mealMath with the other two functions passed as arguments.
float x = mealMath(numberOfAdults(), mealChoosing());
Alternatively you can have no function parameters for mealMath() and instead call numberOfAdults() and mealChoosing() from inside of the function.
It's important to note that most of the time you'll be calling a function and using its output as an argument, and therefore you'll need to put the () after the function's identifier, instead of just typing the identifier alone.
Like mealChoosing() return totalAdults and totalKids (although its not needed here) from numberOfAdults(), numberOfKids() respectively.
int numberOfAdults() {
//...
return totalAdults;
}
int numberOfKids() {
//..
return totalKids;
}
float mealChoosing() {
//..
return cost;
}
Now on mealMath(numberOfAdults, mealChoosing)
float mealMathOutput = mealMath(numberOfAdults(), mealChoosing());
this is my first question here on stackoverflow so I hope it all comes out formatted correctly. I'm working on an assignment for my programming course and we have to write a program that takes two inputs and then produces a readout of results. I had to create two functions that ask which concert a person would like to attend and then how many tickets they would like to purchase for that concert. I've created the functions but now I'm having trouble calling the functions in order to print my results.
#include <iostream>
using namespace std;
char GetConcert()
{
char Concert;
cout << "The following concerts are available:\n";
cout << " B for Beyonce\n";
cout << " L for Lady Gaga\n";
cout << " T for Taylor Swift\n";
cout << "Enter the letter for the concert you want:\n";
cin >> Concert;
return Concert;
}
int GetNumTickets()
{
int NumTickets;
cout << "Enter the number of tickets you want:\n";
cin >> NumTickets;
while ((NumTickets < 0) || (NumTickets > 10))
{
if (NumTickets < 0)
cout << "You can not sell tickets here.\n";
else if (NumTickets > 10)
cout << "You may not purchase more than 10 tickets.\n";
cout << "Enter the number oftickets you want:\n";
cin >> NumTickets;
}
return NumTickets;
}
int main()
{
// Declare Variables
char Concert;
int NumTickets;
// Call function to find out the concert they want to attend
// Call function to find out how many tickets they want
// Print out the information you have collected.
cout << "\nThe customer has placed the following order:\n";
cout << "Concert: " << Concert << endl;
cout << "Number of Tickets: " << NumTickets << endl;
return 0;
}
I've declared my variables in the main section, but when I try to call the function, it says that I can't switch from integer to character.
In order to call a function in c++ you need () at the end of the function name. In this example GetNumTickets() and GetConcert(). Since these functions are also returning values, it is important to save the returned values in order to use them later or to use them right away. You could try:
char Concert = GetConcert();
int NumTickets = GetNumTickets();
My teacher gave me this and I got an error when adding
«setprecision(3) <<setiosflags(ios::fixed) »
Could you tell me why?
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
main()
{
float x1,y1,x2,y2,x3,y3,x4,y4,PQ,QR,RS,SP,Keliling;
cout << "Masukkan koordinat empat titik berbeda (x,y) :\n";
cout<< "P(x,y):" ;
cin >> x1>>y1 ;
cout <<"\n" ;
cout << "Q(x,y) :" ;
cin >> x2>>y2 ;
cout <<"\n" ;
cout << "R(x,y) :" ;
cin>> x3>>y3 ;
cout <<"\n" ;
cout << "S(x,y) :" ;
cin >>x4>>y4;
cout <<"\n" ;
PQ = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
QR = sqrt(pow(x3-x2,2)+pow(y3-y2,2));
RS = sqrt(pow(x4-x3,2)+pow(y4-y3,2));
SP = sqrt(pow(x1-x4,2)+pow(y1-y4,2));
Keliling = PQ+QR+RS+SP;
cout << "Kelilingnya adalah " << Keliling <<" satuan";
«setprecision(3) <<setiosflags(ios::fixed) »
return 0 ;
}
Because you misunderstood the instruction. You can't just dump it onto its own line in code at the end of your program and expect it to magically work, and you can't keep the French quotation marks on it.
You need to think logically about what your program does, and in what order. What do you want to accomplish, and how can you tell the computer how to help you accomplish it?
A good start would be Googling setprecision to find out what it is and how to use it.
Furthermore, your program has other problems, such as the missing return type for main.
sestprecision is a manipulator that you can pass into cout stream to define how many digits after comma should be displayed so for example:
cout << setprecision(2) << 0.122312 << endl;
will result with
0.12
it causes error because you have not put it into stream but nowhere - it is not a statement!
The problem with the line is that it is not a valid cpp statement
the setprecision statement need to be connected to an output stream and since you are using the standard output. The correction will go like this
cout << setprecision(2) << fixed << endl;
Again just as preferencebean stated, your main function should include a return type.
int main()
{
//code;
}
I'm trying to create a command menu where the user will be able to perform as many commands as he/she wants until pressing "q" which will end the loop. I think I have everything I need to do this except I realized mid-way that my professor asked to use string. When I included string into the program, I began to get error messages saying "could not convert string to bool" wherever there was a while or if statement. What can I do to fix this problem and get my program working. Thanks in advance.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char option;
char number=0;
string s;
string n;
string p;
string q;
char number2;
cout << " Please enter a number: "<< endl;
cin >> number;
do {
cout << " Please enter a command: " << endl;
cout << " s- square the number " << endl;
cout << " n- add the number and (number +1) " << endl;
cout << " p- add the number and (number -1) " << endl;
cout << " q- quit" << endl;
cin >> option;
if (option=s) {
s= number*number;
cout << "Square of this number is : " << s;
}
else if ( option=n){
number2= number+1;
n= number+number2;
cout << "Sum of" << number << "+" << number2 << "is: " << n;
}
else if (option=p) {
number2= number-1;
p= number+number2;
cout << "Sum of" << number << "+" << number2 << "is" << p;
}
else if (option=q)
cout << "Terminating Program";
} while(option);
return 0;
}
you're assigning in the if and else if rather than comparing.
if (option=s) {
should be
if (option=='s') {
note the double =
Also, you need to put single quotes (') around the character choice.
It's a common mistake that even experienced developers make.
These declarations
char number=0;
string s;
string n;
string p;
string q;
char number2;
should all be int
int number=0;
int s;
int n;
int p;
int q;
int number2;
Let me answer as if I were who will evaluate your homework. You have several issues here:
You are asked to use string. Avoid the use of char and string together.
char option; // professor asked to use string: (-1) point
string option; // ok
When you use a single =, like in option="a", you are assigning the value "a" to the variable option. But in the if-else statements you want to compare, so you should use the == comparison operator. Also, you can't compare a char with a string.
if(option = "a") // error: expression must have bool type: (-2) points
if(option == 'a') // error: no operator "==" matches std::string == char; (-2) points
if(option == "a") // ok
You use while(option), but option is declared as a char, not as a bool. Replace this line to while(option!="q") to finish when you enter q.
while(option); // error: expression must have bool type; (-2) points
while(option != "q"); // GOOD!
Also, your program will finish when you scape from the while-statement; so, try to put the "Terminating Program" message after this.
You do not need to declare such many variables (s, n, p, q, number2). Try to use temporary variables inside each scope, for example:
if (option=="s")
{
cout << "Square of this number is : " << number*number << endl;
}
else if ( option=="n")
{
int number2= number+1;
cout << "Sum of " << number << "+" << number2 << " is : " << number+number2 << endl;
}
In the form you write this code, every time you type a new option you will obtain an output like:
Sum of 10+11 is : 21 Please enter a command:
This is ugly to me (-1 point). Try to put a newline (<< endl;) after every cout lines.
Finally, what if I type any other letter not listed in the menu? I would expect a message like Enter a valid option (-1 point).
I have a question regarding using getline with and If/ Else If statement.
Currently, my code looks like this:
int yourAge = 13;
cout << "What's your age dude? ";
if(yourAge < 21) {
cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;
} else if(yourAge >= 21) {
cout << "Cool!" << yourAge << "? You are good to go. Don't drink and drive!" << endl;
}
return 0;
}
This works fine. yourAge is 13 and the result is that it says "You're too young to drink".
However, I want to introduce the getline function into the code, so that the result depends upon the user's input. I attempted to change code as below:
int yourAge;
cout << "What's your age dude? ";
getline(cin, yourAge);
if(yourAge < 21) {
cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;
} else if(yourAge >= 21) {
cout << "Cool!" << yourAge << "? You are good to go. Don't drink and drive!" << endl;
}
return 0;
}
This, in turn, results in this error message whenever I try to compile:
"ctut.cpp: In function ‘int main()’:
ctut.cpp:25:25: error: no matching function for call to ‘getline(std::istream&, int&)’
getline(cin, yourAge);
^
ctut.cpp:25:25: note: candidates are:
In file included from /usr/include/wchar.h:90:0,
from /usr/local/include/c++/4.9.2/cwchar:44,
from /usr/local/include/c++/4.9.2/bits/postypes.h:40,
from /usr/local/include/c++/4.9.2/iosfwd:40,
from /usr/local/include/c++/4.9.2/ios:38,
from /usr/local/include/c++/4.9.2/ostream:38,
from /usr/local/include/c++/4.9.2/iostream:39,
from ctut.cpp:1:
/usr/include/stdio.h:442:9: note: ssize_t getline(char**, size_t*, FILE*)
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);....."
That's just the beginning, it goes on for quite a while.
Any help would be appreciated on how to modify. I would like to get the user input on user age, and depending upon the input, to spit out the correct message on the screen.
Thank you!
Quoting cppreference.com,
getline reads characters from an input stream and places them into a string.
Hence getline() will work only if your variable yourAge is a std::string.
For reading int, std::cin is more than enough.
if you must use "getline()" for whatever reason, you have to convert the string to int:
int yourAge;
string age;
cout << "What's your age dude? ";
getline(cin, age);
yourAge = stoi(age);
if(yourAge < 21) {
cout << "What? " << yourAge << "? You're too young to drink!!! " << endl;
} else if(yourAge >= 21) {
cout << "Cool!" << yourAge << "? You are good to go. Don't drink and drive!" << endl;
}
return 0;
getline() is used to read strings not integers. You are better off using cin>>yourAge; for this program. Read these links to learn more about getline link1 link2
You can use getline() to read integers, but it is not advisable. It is better to read integers using cin.
If you want to read integers using getline(), don't forget to convert them into integers using stoi().