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;
}
Related
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int d,m;
int districts=3;
int months = 12;
double sales[districts][months];
for (d=0 ; d < districts; d++)
{
for(m=0; m< months; m++)
{
cout << "Enter sales for District " << d+1 << ":" << " and Month " << m+1 << ": ";
cin >> sales[districts][months];
}
}
cout << "\n\n\n";
cout << setw(40) << "Months\n";
cout << setw(26) << "1 2 3 4 5 6 7 8 9 10 11 12\n";
for (d=0; d < districts ; d++)
{
cout << "District " << d+1;
for(m=0; m< months; m++)
{
cout << ": " << sales[districts][months];
}
}
return 0;
}
This code after running takes only two input values from user and after that a window appear displaying message a problem caused the program to stop working correctly.
There are no compilation errors and I am unable to find the problem. Is there anyone who can help?
You use variables d and m as counter-variables for your loops, but inside the loops you use the maximum value for both of them (districts and months) instead of d and m.
Change this: cin >> sales[districts][months]; to this: cin >> sales[d][m];
Also, this: cout << ": " << sales[districts][months]; to this: cout << ": " << sales[d][m];.
The term sales[districts][months] refers to a particular element sales[3][12], which also happens to be out of bounds for the 2-d array.
The reading loop is repeatedly reading a value to sales[districts][months], i.e. to sales[3][12], which - since array indexing starts at zero in all dimensions, doesn't exist. That gives undefined behaviour.
The output loops are repeatedly outputting the same value, which also gives undefined behaviour.
A common symptom (but not the only possible one) of undefined behaviour is abnormal program termination - and you are seeing an example of that.
There is also the wrinkle that
int districts=3;
int months = 12;
double sales[districts][months];
involves a variable length array (VLA) which is a feature of C (from the 1999 C standard or later) but is not valid C++. If that construct works for you, your compiler supports a non-standard extension.
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.
I just begin to learn C++, for the main method i do:
#include <iostream>
using namespace std;
int main ()
{
int d;
int n;
cout <<"Enter the denominator: " << endl;
cin >> d;
cout <<"Enter the numerator: " << endl;
cin >> n;
cout <<"The result of operation is: " << endl;
cout << (double)n/d << endl;
cout <<"Done";
return 0;
}
It doesn't produce output, but if I delete return 0. I will generate correct output. Shouldn't the main method in C++ always return an integer eventually?
Try cout.flush(); before return.
It forces the buffered data to be send to the output.
I went through your code and everything seems to be right. When I run it, it works fine. If you haven't solved it yet try to cut and past your code into a new project. I know that it sounds stupid but it should work.
I hope this will help you.
I am trying to write a program which asks for tuition credits and for undergraduate or graduate classes. User enters the number of credits, then must enter U for undergraduate or G for graduate. I am having issues with the conditional statement, if the user enters U, then the price of the undergraduate credits will compute and output, similar with graduate. I am trying to enter U at the IF condition, but either one price or the other outputs.
#include <stdlib.h>
#include <iostream.h>
int main ()
{
const double Under = 380.00 ;
const double Grad = 395.00 ;
char U , G ;
double First, Second, Third, Fourth, Fifth, Sixth ;
cout << endl << endl ;
cout << " To calculate your tuition enter the amount of credits, then enter type of" ;
cout << endl ;
cout << " classes." ;
cout << endl << endl ;
cout << " Enter number of credits. " ;
cin >> First ;
cout << endl << endl ;
cout << " Enter U for Undergraduate or G for Graduate: " ;
cin >> Second ;
cout << endl << endl ;
cout << " Your tuition total is: " ;
Third = First * Under ;
Fourth = First * Grad ;
if ( Second == U )
cout << Third ;
else
cout << Fourth ;
cout << endl << endl ;
system ("Pause");
}
Ok I see a few problems here.
The main one is that characters in C++ have single quotes, like this 'c'. This is most likely the cause of your error. Since you never initialized U anywhere, either do initialize it to 'U' or try
if ( Second == 'U' )
cout << Third ;
Second, though this is not necessarily an error typing cout<<endl<<endl; is a little wasteful as it flushes the buffer for cout twice with only 1 character added in between. typing cout<<'\n'<<endl; would fix that.
You never give a value to U. Right now its content is garbage which is why you get random behavior. Try either assigning 'U' to your variable U or changing the confiditional to:
if( Second == 'U' )
It is more or less all of the already stated:
Remove the declaration of char U since it is never used
Change the type of Second to char (remove from the double list and add char
Second;)
Change the if statement to if ( ( Second == 'U' ) || (
Second == 'u' ) )
I don't see U = 'U' anywhere. You is declared at beginning, but never initialized. You U is just a variable. You have to assign the character 'U' in it.
Second is declared as a double, but it looks like you are expecting the user to enter a character.
use
using namespace std;
under header file
I have the following program that generates a multiplication table. A formatting problem arises when the outputs reach the double digits. How do I straighten out the columns?
#include <iostream>
using namespace std ;
int main()
{
while (1 != 2)
{
int column, row, c, r, co, ro;
cout << endl ;
cout << "Enter the number of columns: " ;
cin >> column ;
cout << endl ;
cout << "Enter the number of rows: " ;
cin >> row ;
cout << endl ;
int temp[column] ;
c = 1 ;
r = 1 ;
for(ro = 1; ro < row ; ro ++ ){
for(co = 1; co < column ; co ++ ){
c = c ++ ;
r = r ++ ;
temp [c]= co * ro;
cout << temp[c] << " ";
}
cout << endl ;
}
system("pause");
}
}
C++ had setw and setfill for just this purpose. setw sets the width and setfill sets the fill character.
In your case, you can just use something like:
#include <iostream>
#include <iomanip>
int main (void) {
std::cout << std::setw(5) << 7 << std::endl; // will output " 7".
return 0;
}
You have a number of other problems with that code, at least some of which are listed below:
You don't allocate enough space for your array, it should be column*row (or use a two-dimensional array).
Array indexes start at 0, not 1.
c = c++ is not a good idea, c++ will be enough to increment c.
You may be trying to increment c twice in each iteration, once if the for statement itself and once in the for body.
system("pause"); is an ugly hack where the language provides a perfectly good getchar or cin equivalent.
while (1 != 2) just looks plain wrong :-) since 1 will never equal 2. Just use while (1) or for(;;) - any coder worth their salt will know what you mean.
use the setw output manipulator:
cout << setw(3) << temp[c];
By default, this uses spaces to fill, which it looks like you want.
You will need to include iomanip as the documentation says.
You can set width of your column elements by using stream manipulators like this:
cout << setw(3) << temp[c]
But this is something you need to fix besides: c = c++; does not increment the variable!
This is one of those situations where the old-fashioned printf is a lot easier than cout. Replace cout << temp[c] << " " with printf("%2d ", temp[c]).
And I hope you've discovered the bug in your c calculation.
You could use "\t" instead of " ".