My program seg faults and i\'m not sure why. Can you help me generate a program
ID: 3761104 • Letter: M
Question
My program seg faults and i'm not sure why. Can you help me generate a program that fufills the purpose below without seg faulting?
//Purpose: display the greeting message at the center of the bottom line of the computer screen.
// step 1: get the total number of lines and columns of the terminal window
// step 2: clear the terminal window
// step 3: diplay the greeting message at the center of the bottom line of the termial window
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
int lines = atoi(getenv("LINES"));
int columns = atoi(getenv("COLUMNS"));
string greetings = "Thank you to make the screen so clean!";
if (system("CLS"))//for windows
{
system("clear");//for linux
}
for (int n=1; n < lines; n++)
{
cout << endl;
}
for (int m=0; m < (columns - greetings.length())/2 ; m++)
{
cout << " ";
}
cout << "Thank you to make the screen so clean!" << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <sys/ioctl.h>
using namespace std;
int main()
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
int lines = w.ws_row;
int columns = w.ws_col;
string greetings = "Thank you to make the screen so clean!";
if (system("CLS"))//for windows
{
system("clear");//for linux
}
for (int n=1; n < lines; n++)
{
cout << endl;
}
for (int m=0; m < (columns - greetings.length())/2 ; m++)
{
cout << " ";
}
cout << "Thank you to make the screen so clean!" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.