Need help

This is a discussion on Need help within the C++ Programming forums, part of the General Programming Boards category; Hi I have this cod : cout << "Frukt\n"; cout << "Volym\n"; cout << "tal1 multiplicerad med tal2, tal3 giv" ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    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 oneexemple)

    Material Volym(dm) weight(kg) price
    Frukt 100 270 2000

    How can i write the code
    Last edited by sd03eee; 09-19-2005 at 05:42 PM.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    What? Please try to be a little bit more clear and I could help.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    // 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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    [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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Hi
    I want the result it show in columns, how can i do this , thanks

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    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;
    Last edited by spydoor; 09-20-2005 at 02:11 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21