0

info: the majority of my problem is solved so far. please scroll to the very bottom to "here is my progress so far" to help me further, if you want to. thanks!

i have the following code and i would like to display the compiled results of this code in a clean table-like order to make it more clear for the users looking at the compiled command line program.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{double x;
for(x=-10;x<=10;x+=0.5)
    cout << "x=" << x << 3*x*x-7*x+5 << endl;
    return 0;
}

i was a little bit in a hurry so i couldnt really see what the teacher wrote on the board how to add a table-like ordering to it. i only remember something like this:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{double x;
for(x=-10;x<=10;x+=0.5)
    cout << "x=" << x << "|*|" << 3*x*x-7*x+5 << endl;
    return 0;
}

i know the 2nd code is probably totally wrong, but it was a code with this symbol: |

does that make any sense for someone? maybe someone can figure out what exactly my teacher tried. or maybe there is another way to display the values in a good and clear order.

let me give you an example what i exactly mean by clear order. the following example will show you compiled results (in cmd) of the first code (chaotic, no order etc)

x=-10 375 
x=-9.5 465 
x=-9 123 
x=-8.5 124

instead of the result above, i would like to have something like..

x=  -  10.0     375 
x=  -   9.5     465 
x=  -   9       123 
x=  -   8.5     124

unfortunately i dont know how to do that. i looked up some methods, but i was thinking: maybe what my teacher tried to show is easier than the methods i looked up. so maybe someone here knows how it is done the way i tried to do in the 2nd code. if not, i would be glad if someone could tell me the shortest and easiest way to sort the output.

thanks in advance

.---------------------------------- .

here is my progress so far. i used setw to add width to the lines. the changed code would look like this (with include iomanip added):

cout << std::setw(10) << x << std::setw(10) << 3*x*x-7*x+5 << endl;

problem is, the number ranges dont fit. like.. the single digits, ten-step digits, hundred-step digit are not in the same row. they kind of look like this:

      -10.0         375  
      -9.5       342.25

etc..

i would like the make the result into something like this, you can clearly see the first digit, 2nd digit etc... of the numbers:

      - 10.0         375  
      -  9.5         342.25

someone maybe knows how i give the command to order it like that?

pogos
  • 45
  • 8
  • Take a look at [std::setw](https://en.cppreference.com/w/cpp/io/manip/setw) – Ted Lyngmo Mar 03 '19 at 21:05
  • @TedLyngmo thanks, thats what i was looking for! just a last question. now I made this: `cout << std::setw(10) << x << std::setw(10) << 3*x*x-7*x+5 << endl;` how exactly can i tell the code, that it should order single, tenth, hundreds numbers below the same line? as displayed in the 2nd output in the original post. (sorry formatting is a bit hard in a comment. i will edit the original post to show my progress) – pogos Mar 03 '19 at 21:34

1 Answers1

0

You combine std::setw with std::setprecision and std::fixed:

#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setprecision(2) << std::fixed;
    for(double x = -10.0; x <= 10.; x += 0.5) {
        std::cout << std::setw(10) << x << std::setw(10) << 3 * x * x - 7 * x + 5 << "\n";
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • thanks, thats awesome, it worked. now i jsut try to understand what each command you just told me is doing. so let me tell you my guess: setprecision tells you how many number it shall display after a comma (like if there is the number= 45.6789 and prescision is set to 2, then it will only show you 45,67) what i dont quite understand is, why it shows 6 digits after the comma when i just delete the part "std::setprecision(2)". and the std::fixed probably makes them in a nice row. well and to be honest i didnt really get why i need to add "std::" – pogos Mar 03 '19 at 22:17
  • Yes, you can use [std::setprecision](https://en.cppreference.com/w/cpp/io/manip/setprecision) to change from the default 6 digits to something else. [std::fixed](https://en.cppreference.com/w/cpp/io/manip/fixed) is used to set a fixed number of digits, so that `0.1` becomes `0.10` (with precision 2). For the last question: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Mar 04 '19 at 06:23
  • thanks for that link. that makes it more clear. but when do i need to use " std::" now? for every single command i use? like.. std::for ? std::if ? std::case? etc etc? or whre exactly should std:: be used to prevent future conflict by possible updates. i mean right now i am only using one namespace, thats namespace std. in this case its not needed to use std::cout etc.? so only using the namespace tags in code when you use multiple libraries? – pogos Mar 04 '19 at 14:37
  • No, you don't need `std::` for any of the [keywords](https://en.cppreference.com/w/cpp/keyword) and if you use `std::cout` and `std::cin` a lot, you can bring those in with `using std::cout, std::cin;` and skip `std::` for those in your code. It's a matter of taste I suppose, but I prefer having the `std::` there to make it clear that it's a `std` function/object/lib. – Ted Lyngmo Mar 04 '19 at 20:16