Question

I've spent like 3 hours creating with the ASCII characters a table, and at the end i've correctly done one...But the problem is really weird...if i copy the table code and paste it again in the same program it formats differently! Why is it doing like this?

I've really spent a lot doing this and it's literally driving me insane! It's the exact same code...Please help needed

https://i.stack.imgur.com/MB3MO.png

The code is the following

  #include <iostream>   // Cout e cin
  #include <cstdlib>   // System Pause
  #include <iomanip>

  using namespace std;

  int main ()
  {

    cout<<char(218)<<setfill(char(196))<<setw(12)<<char(194)<<setfill(char(196))<<setw(20)<<char(194)<<setfill(char(196))<<setw(20)<<char(194)<<setfill(char(196))<<setw(20)<<char(191)<<endl;

    cout<<char(179)<<left<<setfill(' ')<<setw(11)<<"Algoritmo"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Numero confronti"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Numero copie"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Tempo di esecuzione"<<char(179)<<endl;
    cout<<char(195)<<setfill(char(196))<<setw(11)<<char(196)<<setfill(char(196))<<setw(20)<<char(197)<<setfill(char(196))<<setw(20)<<char(197)<<setfill(char(196))<<setw(20)<<char(197)<<char(180)<<endl;
    cout<<char(192)<<setfill(char(196))<<setw(11)<<char(196)<<setfill(char(196))<<setw(20)<<char(193)<<setfill(char(196))<<setw(20)<<char(193)<<setfill(char(196))<<setw(20)<<char(193)<<char(217);

    cout<<endl;

    cout<<char(218)<<setfill(char(196))<<setw(12)<<char(194)<<setfill(char(196))<<setw(20)<<char(194)<<setfill(char(196))<<setw(20)<<char(194)<<setfill(char(196))<<setw(20)<<char(191)<<endl;

    cout<<char(179)<<left<<setfill(' ')<<setw(11)<<"Algoritmo"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Numero confronti"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Numero copie"<<char(179)<<left<<setfill(' ')<<setw(19)<<"Tempo di esecuzione"<<char(179)<<endl;
    cout<<char(195)<<setfill(char(196))<<setw(11)<<char(196)<<setfill(char(196))<<setw(20)<<char(197)<<setfill(char(196))<<setw(20)<<char(197)<<setfill(char(196))<<setw(20)<<char(197)<<char(180)<<endl;
    cout<<char(192)<<setfill(char(196))<<setw(11)<<char(196)<<setfill(char(196))<<setw(20)<<char(193)<<setfill(char(196))<<setw(20)<<char(193)<<setfill(char(196))<<setw(20) <<char(193)<<char(217);



    cout<<endl;
    system ("PAUSE");
    return 0;

}
Was it helpful?

Solution

It has to do with the justification of your text. The first time you print the table, you use left to set the justification. You should use cout << right before printing the second table.

Each stream has a set of format flags. At the start of the program, standard streams (for example, cout) have their adjustfield set to right. That is why the first time you print your table, everything works: the first line of your table code correctly assumes right-justified text. Once you set the justification to left on the second line, it remains that way until you change it. So you can actually remove the other lefts on the second line of your table code, because the first one is enough.

For the second table code to work correctly, you need to revert back to right justification. So right after the empty line between the two tables, you should insert cout << right;.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top