Hello everyone this is my code and I just help I managed to correct the first 3 questions but the rest I am still getting errors.
Below is the all question :
Complete the provided main() program with statements to accomplish each of the following. In each case you must use the appropriate I/O stream manipulators to produce the appropriate output wherever possible.
Output first first as an integer value, followed by a space, then in
its written form.
Output second as a base ten value, followed by a space, then as a
hexadecimal
value, followed by a space, then as an octal value. Make sure the
appropriate base indicator prefix is shown in the output.
Output third.
Output fourth with four digits, with the sign shown at the left, and
the value right aligned. The decimal point must also appear.
Output fourth with four significant figures.
Output fifth with seven significant figures. (Note: use left alignment here)
Output fifth with three digits to the right of the decimal point.
Output third.
Output fourth with two digits to the right of the decimal point.
Output sixth with no decimal portion showing
Output fourth with eight digits to the right of the decimal point.
Output sixth with six digits.
Here is my code so far :
#include <iostream>
#include <iomanip>
using namespace std;
int
main0()
{
bool first;
int second;
long third;
float fourth;
float fifth;
double sixth;
cout << "Enter bool, int, long, float, float, and double values: ";
cin >> first >> second >> third >> fourth >> fifth >> sixth;
cout << endl;
cout << noboolalpha << first;
cout << " ";
cout << boolalpha << first << endl;
cout <<left << dec << showbase;
cout << second;
cout << " ";
cout << internal << hex << showbase;
cout << second;
cout << " ";
cout <<right << oct <<showbase;
cout << second << endl;
cout << third<< scientific<< endl;
cout <<left << setw(4)<<fixed<< fourth <<endl;
cout <<setprecision(4)<< fourth <<endl;
cout <<left<<setw(7)<< fifth << endl;
cout <<right<<setprecision(3)<< fifth;
cout <<third<<endl;
cout <<right<<setw(2)<<fourth<<endl;
cout << fixed<<sixth<< endl;
cout << right << fixed<<setprecision(8)<< fourth<< endl;
cout <<left<<showpoint <<setprecision(6)<<sixth;
// ***** Solution ends here ****
cin.get();
return 0;
}
I answered 4-6:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long third = 123654123654123LL;
float fourth = 12335.67890;
std::ios initialState(nullptr);
initialState.copyfmt(std::cout);
// 4
cout << third << scientific<< endl;
// 5
cout << showpoint << fixed << setprecision(4) << right << showpos << fourth << endl;
cout.copyfmt(initialState);
// 6
cout << setprecision(4) << fourth << endl;
return 0;
}
Good luck with the rest.
Related
I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7
cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;
So, I'm trying to make a basic C++ program that converts Base 10 -> Base 16
while(true){
int input;
cout << "Enter the base 10 number\n";
cin >> input;
cout << "The hex form of " << input << " is: ";
int decimal = input;
cout << hex << uppercase << decimal << "\n";
decimal = NULL;
input = NULL;
Sleep(3000);
}
And on the first run through it works. For example, if I enter 7331 I get:
The hex form of 7331 is 1CA3
But if I try it a second time (with the same input) I get:
The hex form of 1CA3 is 1CA3
I can try this with any integer input and it works fine for the first run through but after that it puts the base 16 number in twice.
You need to reset your stream. When you apply std::hex to std::cout, it applies permanently to the stream (std::cout), and you need to reset it. You can simply change your program to this:
cout << "The hex form of " << std::dec << input << " is: ";
your FIX:
cout << "The hex form of "<< dec << input << " is: ";
You could even shorten your code:
while (true)
{
int input;
cout << "Enter the base 10 number: ";
cin >> input;
cout << "The hex form of " << dec << input << " is: ";
cout << hex << uppercase << input << "\n";
Sleep(3000);
}
I'm trying to make the program exit properly without it. I have '|' as my exit, if its the first thing I do when first running, it closes fine. But after entering values and printing them, afterwards entering '|' to exit.
It prints out:
"The smaller value is 0
The larger is previous second value" // want to remove this from showing
int main()
{
double first = 0, second = 0;
while(cin.good()){
char exit;
cout << "Enter '|' to exit.\n";
cout << "Enter two numbers:";
cin >> first >> second;
exit = cin.peek();
if(exit=='|'){
break;}
else{
if(first<second){
cout << "\nThe smaller value is " << first << "\nThe larger value is " << second << endl;
}
else if(first>second){
cout << "\nThe smaller value is " << second << "\nThe larger value is " << first << endl;
}
}
}
}
In your code, you've assumed that the input from your users will be limited to something usable as a double. This isn't necessarily the case. The issue that you're running into isn't related to the statement exit = cin.peak(); but to cin >> first >> second; You can test this by entering any non-numerical input into your program and watching it fail by assigning a 0 to the first and leaving second as is.
In short, because the conversion of the input into a double fails, you get an indeterminate value for first and then your program moves on.
You can use the following code as an example. In this, I first populate my variables as strings, then attempt a conversion after the fact.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
string str_first, str_second;
double first = 0, second = 0;
while(cin.good()){
cout << "Enter '|' to exit.\n";
cout << "Enter two numbers:";
cin >> str_first >> str_second;
if( (str_first.compare("|") == 0) || (str_second.compare("|") == 0) ){
cout << "\nThanks for playing\n" << endl;
break;}
else{
first = strtod (str_first.c_str(), NULL);
second = strtod (str_second.c_str(), NULL);
if(first<second){
cout << "\nFirst is small: The smaller value is " << first << "\nThe larger value is " << second << endl;
}
else if(first>second){
cout << "\nSecond is small: The smaller value is " << second << "\nThe larger value is " << first << endl;
}
}
}
}
So the below code works fine, with one exception:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int value1, value2, value3, value4;
float calcMean,calcStDev;
// Input values from keyboard
// prompt user for input
cout << "Enter 1st value. " << endl;
cin >> value1;
cout << "Enter 2nd value. " << endl;
cin >> value2;
cout << "Enter 3rd value. " << endl;
cin >> value3;
cout << "Enter 4th value. " << endl;
cin >> value4;
// Calculate the mean, mean = (value1 + value2 + value3 + value4)/4.0
calcMean = (value1 + value2 + value3 + value4) / 4.0;
// Calculate the standard deviation, standard deviation = squareroot((sum((input value-mean)*(input value-mean)))/number of input value - 1)
calcStDev = sqrt((pow((value1 - calcMean),2) + pow((value2 - calcMean),2) + pow((value3 - calcMean),2) + pow((value4 - calcMean),2))/(4-1));
// Output display
cout << fixed << setprecision(2) << endl;
cout << "Mean of the four values: " << setw(10) << calcMean << endl;
cout << "Standard deviation of the four values: " << setw(10) << calcStDev << endl;
cin.get();
cin.get();
return 0;
}
The output doesn't seem to be responding to the setw() like I expected (I wanted the number 18.50 to be directly over 19.64, one on top of the other).
Any idea what I am doing wrong?:
Enter 1st value.
2
Enter 2nd value.
36
Enter 3rd value.
35
Enter 4th value.
1
Mean of the four values: 18.50
Standard deviation of the four values: 19.64
I think that the problem is that string literal "Mean of the four values: " contains embedded tab characters. It is possible that to insert spaces you pressed the TAB key instead of SPACEBAR key.
I'm trying to do some simple output in formatted text. Setprecision is not printing my variables out to two decimal places.
For example if firstItemPrice = 2.20, the output is 2.2 instead of 2.20
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string firstitem = "";
string seconditem = "";
double firstItemNum;
double firstItemPrice = 0.00;
double secondItemNum;
double secondItemPrice = 0.00;
//first item
cout << "Enter the name of Item 1: ";
getline(cin, firstitem);
cout << "Enter the number of " << firstitem << "s and the price of each: ";
cin >> firstItemNum >> firstItemPrice;
cin.ignore();
//second item
cout << "Enter the name of Item 2: ";
getline(cin, seconditem);
cout << "Enter the number of " << seconditem << "s and the price of each: ";
cin >> secondItemNum >> secondItemPrice;
cout << left << setw(20) << "Item" << setw(10) << "Count"
<< setw(10) << "Price" << left << "\n";
cout << setw(20) << "====" << setw(10) << "====" << setw(10)
<< "====" << left << "\n";
cout << setw(20) << firstitem << setw(10)
<< firstItemNum << setw(10) << setprecision(2)
<< firstItemPrice << "\n";
cout << setw(20) << seconditem << setw(10) << secondItemNum
<< setprecision(2) << secondItemPrice << left << "\n";
return 0;
}
You need a fixed in there to do that.
cout << fixed;
Set it back using:
cout.unsetf(ios_base::floatfield);
In your case, changing the last bit of your program like this example should do it:
cout << setw(20) << firstitem << setw(10)
<< firstItemNum << setw(10) << fixed << setprecision(2)
<< firstItemPrice << "\n";
cout.unsetf(ios_base::floatfield);
cout << setw(20) << seconditem << setw(10) << secondItemNum
<< fixed << setprecision(2) << secondItemPrice << left << "\n";
Editorial aside: Don't use floating point numbers to represent currency values.
from http://www.cplusplus.com/reference/ios/ios_base/precision/
The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).
For the default locale:
Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.