-
Need help
Hi
I have this cod :
cout << "Frukt\n";
cout << "Volym\n";
cout << "tal1 multiplicerad med tal2, tal3 giv"
<< tal1 * tal2 * tal3 << " \n\n";
cout << "weight\n";
cout << "tal1 multiplicerad med tal2, tal3, 2.7 giv"
<< tal1 * tal2 * tal3 * 2.7 << " \n\n";
cout << "price\n";
cout << "tal1 multiplicerad med tal2, tal3, 20 giv"
<< tal1 * tal2 * tal3 * 20 << "\n\n";
and i want the result it show like this instead of the up one:(exemple)
Material Volym(dm) weight(kg) price
Frukt 100 270 2000
How can i write the code
-
What? Please try to be a little bit more clear and I could help.
-
Please read the sticky at the top of the forum about what to do when posting code.
If you're asking how to print that text to the screen, and have it nicely formatted in columns, it looks like you're sending data to cout in the wrong order. You need to send all of the first row first, then all of the second, and so on.
-
// calk11.cpp //
// Blockkalkylatorn
#include <iostream>
using namespace std; //
int main ()
{
int tal1; // Deklaration of variabel inmatning av hod anges in decimeter
int tal2; // Deklaration of variabel inmatning av lang anges in decimeter
int tal3; // Deklaration of variabel inmatning av bed anges in decimeter
cout << "\nGive tal1 (dm) : ";
cin >> tal1;
cout << "\nGive tal2 : ";
cin >> tal2;
cout << "\nGive tal3 (dm) : ";
cin >> tal3;
cout << "(1)W!\n"; // Val av material Marmor
cout << "(2)Sten !\n"; // Val av material Sten
cout << "(3)Trä !\n"; // Val av material Trä
cout << "choose of material !\n\n";
cout << "\nGive nummer : ";
int material;
cin >> material;
if (material == 1)
{
cout << "Frukt\n";
cout << "Volym\n";
cout << "tal1 multiplicerad med tal2, tal3 giv"
<< tal1 * tal2 * tal3 << " \n\n";
cout << "weight\n";
cout << "tal1 multiplicerad med tal2, tal3, 2.7 giv"
<< tal1 * tal2 * tal3 * 2.7 << " \n\n";
cout << "price\n";
cout << "tal1 multiplicerad med tal2, tal3, 20 giv"
<< tal1 * tal2 * tal3 * 20 << "\n\n";
and i want the result it show like this instead of the up oneexemple)
Material Volym(dm) weight(kg) price
Frukt 100 270 2000
How can i write the code
-
[code]
[/code]
Put these around your code and try again (better yet, press the "edit" button at the bottom of your most recent post and add them)
-
Code:
// calk11.cpp //
// Blockkalkylatorn
#include <iostream>
using namespace std; //
int main ()
{
int tal1; // Deklaration of variabel inmatning av hod anges in decimeter
int tal2; // Deklaration of variabel inmatning av lang anges in decimeter
int tal3; // Deklaration of variabel inmatning av bed anges in decimeter
cout << "\nGive tal1 (dm) : ";
cin >> tal1;
cout << "\nGive tal2 : ";
cin >> tal2;
cout << "\nGive tal3 (dm) : ";
cin >> tal3;
cout << "(1)W!\n"; // Val av material Marmor
cout << "(2)Sten !\n"; // Val av material Sten
cout << "(3)Trä !\n"; // Val av material Trä
cout << "choose of material !\n\n";
cout << "\nGive nummer : ";
int material;
cin >> material;
if (material == 1)
{
cout << "Frukt\n";
cout << "Volym\n";
cout << "tal1 multiplicerad med tal2, tal3 giv"
<< tal1 * tal2 * tal3 << " \n\n";
cout << "weight\n";
cout << "tal1 multiplicerad med tal2, tal3, 2.7 giv"
<< tal1 * tal2 * tal3 * 2.7 << " \n\n";
cout << "price\n";
cout << "tal1 multiplicerad med tal2, tal3, 20 giv"
<< tal1 * tal2 * tal3 * 20 << "\n\n";
and i want the result it show like this instead of the up oneexemple)
Material Volym(dm) weight(kg) price
Frukt 100 270 2000
How can i write the code
-
Well since you want Volym to appear before Frukt, how about
Code:
cout << "Volym\n";
cout << "Frukt\n";
Instead of
Code:
cout << "Frukt\n";
cout << "Volym\n";
Seriously, you have all the information, and you're using all the right components.
Surely you can arrange them into an order which output information in the style you want.
-
Hi
I want the result it show in columns, how can i do this , thanks
-
maybe you're over thinking this
Just print your headings on one line then print your values below it. You might include <iomanip> and use setw to ensure they line up nicely.
Code:
cout.setf(ios::left);
cout << setw(15) << "Material" << setw(15) << "Volym(dm)" << setw(15) << "weight(kg)" << setw(15) << "price" << endl;
cout << setw(15) << "Frukt" << setw(15) << tal1 << setw(15) << tal2 << setw(15) << tal3 << endl;