|
PLEASE DISREGARD ALL OF THE FOLLOWING.
I AM USING SETW INCORRECTLY.
I need to check this more, but ...
with
cout << set(x) << left << "TEXT " << data << endl; ..
I expect TEXT to be output starting at column x and left justified.
As of now the data is output at unexpected column - as you pointed out.
Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column?
The "flush" ( clear cout buffer ) should have no effect on the actual output, it is there as an attempt to fix another issue.
However - endl at each cout line should also clear the cout buffer , hence each line should start independently - which it does.
The whole snippet is part of TWO processes output and I need to try this setw in single process FIRST! It appears that cout does not run in each process independently, so let me eliminate the child process.
I am also not sure if "\n" clears the cout buffer.
modified 6-Jan-20 0:49am.
|
|
|
|
|
The setw() manipulator (<iomanip> functions | Microsoft Docs[^]) just sets the minimum width of the next field to be output. So whatever is in that field will be output (after converting to text if necessary) in a field that is at least that many characters wide. If the text is wider than the setw value then it will overflow the field.
As to your other comments above, setw has nothing to do with which column the output will start at.
Vaclav_ wrote: It appears that cout does not run in each process independently It sort of does, but ultimately they both feed into the same stream that is managed by the console handler.
Vaclav_ wrote: I am also not sure if "\n" clears the cout buffer. No, it is just a character sent to the stream, but I think the console handler converts it to CR LF . The end of output is sensed by the presence of the endl manipulator.
|
|
|
|
|
Vaclav_ wrote: Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column?
Looks like you figured out where you went wrong with setw() , but I'd like to point out that setw sets the width of the next output field, but does not truncate, so cout << setw(5) << "Hello World"; will still print all of the string "Hello World".
If you really want to put output at a specific location on screen, maybe take a look at curses: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
|
|
|
|
|
Quote: usual note that I am looking for a solution to an issue, not for commentaries on my code style
|
|
|
|
|
Trying to convert a linux project to windows, I met an error (from the subject of the post). I have a chance to use something else in windows case ?
if(S_ISDIR(type))
....
where type is defined as mode_t type ... and mode_t is another error:
error C2061: syntax error : identifier 'mode_t'
how can I solve these errors ? I seek on internet and I found something like:
#if defined __WIN32__ || defined _WIN32 || defined _Windows
#if !defined S_ISDIR
#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
#endif
#endif
taken from here: c - Error: identifier "_S_IFDIR" is undefined - Stack Overflow[^]
|
|
|
|
|
googling s_isdir windows gives this hit: Porting To Win32!!!!. Seems like what you want. If not, maybe one of the other 27,000 results might help?
|
|
|
|
|
|
Hi.
I send a "TEXT" from Form1 to Form2.
Sometimes it's good, sometimes it's not good.
When you send "TEXT" from Form1 to Form2, TEXT disappears from Form2.show.
---------------------------------------------
Hide Expand Copy Code
Form1.h
private Burron1()
{
Form2::Form fm;
fm.viewText();
}
Form2.h
namespace Form {
public class Form
{
Form2()
{
void viewText();
}
}
}
void viewText()
{
textBox1->Text="TEXT";
}
-------------------------------------------------------------
Only when Text's Form, no problem. I cann't see other , only did mySql. This disappeared the "TEXT". textBox->Text is a Empty. I sent "TEXT" can't look for any.
I want to know how to make the appearing thing.
Say me , please.
Thank you.
|
|
|
|
|
private Burron1()
{
Form2::Form fm;
fm.viewText();
}
You create a local instance of Form2 in the above method, and then call viewText . But as soon as you return from that method, the variable fm goes out of scope and the form is destroyed. You need to create the form outside this method so it remains active until you decide to destroy it.
|
|
|
|
|
Is it a native or managed c++ code?
|
|
|
|
|
I am a newbie to C++ programming and currently working on a program using Abstract Factory design pattern and linked list to store the objects and display them.
The user is expected to select whether the entry is for a management staff or Junior staff then save the parameters in the linked list and display them too.
I have created the abstract factories and the linked list but I am stuck on how to save the objects and display them with the linked list.
I need help with how to pass the staff object to the linked list and display them.
The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp).
Thank you.
I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp
The codes are below
Staff.h
<pre>#pragma once
#include<string>
class Staff
{
private:
std::string Name;
std::string Address;
int age;
public:
Staff() {
Name = "";
Address = "";
age = 0;
}
Staff(std::string zName, std::string zAddress, int zage)
{
Name = zName;
Address = zAddress;
age = zage;
}
virtual void display() = 0;
virtual void input() = 0;
};
class JuniorStaff : public Staff {
private:
std::string Name;
std::string Address;
int age;
std::string staffLevel;
public:
JuniorStaff() {
Name = "";
Address = "";
age = 0;
staffLevel = "";
}
void input();
void display();
};
class MgtStaff : public Staff { private:
std::string Name;
std::string Address;
int age;
std::string mgrLevel;
public:
MgtStaff() {
Name = "";
Address = "";
age = 0;
mgrLevel = "";
}
void input();
void display();
};
class StaffFactory
{
public:
virtual Staff* staffDetails() = 0;
};
class JuniorStaffFactory :public StaffFactory
{
public:
Staff* staffDetails()
{
return new JuniorStaff();
}
};
class MgtStaffFactory :public StaffFactory
{
public:
Staff* staffDetails()
{
return new MgtStaff();
}
};
Staff.cpp
#include<iostream>
#include<string>
#include"Staff.h"
using namespace std;
void Staff::input()
{
}
void Staff::display()
{
cout << "The staff name is " << Name << endl;
cout << "The address of staff is " << Address << endl;
cout << "The staff age is " << age << "years" << endl;
}
void JuniorStaff::input()
{
cout << "Enter staff name: " << endl;
cin.ignore();
getline(cin, Name);
cout << "Enter address of staff: " << endl;
cin.ignore();
getline(cin, Address);
cout << "Enter staff age: " << endl;
cin.ignore();
cin >> age;
cout << "Enter staff level: " << endl;
cin.ignore();
getline(cin, staffLevel);
}
void JuniorStaff::display()
{
cout << "The staff name is " << Name << endl;
cout << "The address of staff is " << Address << endl;
cout << "The staff age is " << age << "years" << endl;
cout << "The staff level is " << staffLevel << endl;
}
void MgtStaff::input()
{
cout << "Enter staff name: " << endl;
cin.ignore();
getline(cin, Name);
cout << "Enter address of staff: " << endl;
cin.ignore();
getline(cin, Address);
cout << "Enter staff age: " << endl;
cin.ignore();
cin >> age;
cout << "Enter Management level: " << endl;
cin.ignore();
getline(cin, mgrLevel);
mgrLevel = mgrLevel;
}
void MgtStaff::display()
{
cout << "The staff name is " << Name << endl;
cout << "The address of staff is " << Address << endl;
cout << "The staff age is " << age << "years" << endl;
cout << "The Manager level is " << mgrLevel << endl;
}
StaffList.h
#pragma once
#include"Staff.h"
class List {
private:
class Node {
friend class List;
StaffFactory* newstaff;
Node* next;
public:
Node()
{
this->newstaff = NULL;
this->next = NULL;
};
Node(StaffFactory* c, Node* n)
{
this->newstaff = c;
this->next = n;
}
};
List::Node* head;
List::Node* curr;
public:
List();
void AddNode(StaffFactory* c);
void PrintList();
};
StaffList.cpp
#include<iostream>
#include<string>
#include"StaffList.h"
#include"Staff.h"
using namespace std;
List::List() {
head = NULL;
curr = NULL;
}
void List::AddNode(StaffFactory* c) {
List::Node* n = new List::Node;
n->next = NULL;
n->newstaff = c;
if (head == NULL) {
head = n;
}
else
{
curr = head; while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = n;
}
}
void List::PrintList() {
curr = head;
while (curr != NULL) {
curr->newstaff->staffDetails();
curr = curr->next;
}
}
StaffMain.cpp
#include <iostream>
#include<string>
#include"Staff.h"
#include"StaffList.h"
using namespace std;
int main()
{
List stafflist;
int choice;
cout << "Select type of staff: " << endl;
cout << "1: Junior Staff" << endl;
cout << "2: Management Staff" << endl;
cout << "Selection: ";
cin >> choice;
cout << endl;
StaffFactory* newStaff;
switch (choice)
{
case 1:
newStaff = new JuniorStaffFactory;
break;
case 2:
newStaff = new MgtStaffFactory;
break;
default:
cout << "Invalid selection!!!" << endl;
newStaff = NULL;
break;
}
if (newStaff != NULL)
{
Staff* c = newStaff->staffDetails();
c->input();
c->display();
stafflist.AddNode(newStaff);
Staff* d = newStaff->staffDetails();
d->input();
stafflist.AddNode(newStaff);
stafflist.PrintList();
}
}
|
|
|
|
|
EjireK wrote: The AddNode and PrintList functions of the Linked list aren't working. You will need to be much more specific than that. Exactly what does "not working" mean, and where in the code do the failures occur.
As a quick exercise you could write a simple linked list that holds basic types (integers for example) and work on that until you can see exactly how to make it work correctly. You can then adapt that skeleton to handle any object type. Next step is to create the basic staff type and process objects of that type into the list. Finally you can expand it to add the other child types. Taking things a step at a time is much better, and easier, than trying to create a full system in one go.
|
|
|
|
|
For clarity, my challenge is with passing the abstract factory class object to the Linked list to create the node and to display it. The program does not generate any error currently as it successfully prompts the user to select which type of staff to create then prompts the user to specify the parameters.The linked list works with creation of staff object without the abstract factory but the exercise specifies states that I should use abstract factory with linked list to save the staff objects and display it. I’ve been unable to use the linked list with the abstract factory class object. I think my problem is in the StaffMain.cpp where I’m to specify the factory object to pass to the linked list but can’t seem to get that working. Thank you.
|
|
|
|
|
I am afraid that I do not have the time to analyse all that code. As I mentioned above, get a working linked list first that you know can handle simple objects. Once you have that logic working, then adapting it to handle any other object type is fairly trivial.
|
|
|
|
|
Thank you for your reply.
As advised, I have worked on a simple linked list storing integer values.
My simple linked list program has add node and display functions. The add node is working fine when the parameters are passed in the main program but the display function only displays the last item added. the other integers added previously are not showing.
E.g for the program below, it only outputs 10 and the other integers are not displayed.
int List::add(int c)
{
cout << "Adding " << c << endl;
Link* temp;
temp = new Link(c);
if (temp == 0)
{
cout << "Memory allocation failed" << endl; }
head = temp;
return c;
}
void List::display()
{
Link* temp = head;
cout << "Displaying list:" << endl;
while (temp !=NULL)
{
cout << "Value of object is " << temp->i <<endl;
temp = temp->Next;
}
}
int main()
{
List list;
list.add(6);
list.add(8);
list.add(10);
cout << endl;
list.display();
cout << endl;
}
|
|
|
|
|
Why would you expect it to do anything else at no point do you chain the entries you just throw earlier entries away
I suggest you look at what List::add does because all you are doing is overwriting "head"
Contrast it to what List::AddNode does in the code above which does actually create a chain
In vino veritas
|
|
|
|
|
In addition to Leon's comments above ... think about the logic. A linked list must have a constant pointer to the start of the list, the 'head'. Each item in the list should contain one or more data items, and a pointer to the next item. The head pointer should only be allocated once, when the list is first created. Each subsequent item should be chained to the node at the end of the chain. You can keep a pointer to the last item, or let the add method iterate through the list to find it.
|
|
|
|
|
Quote: The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp).
I don't know about AddNode(), but your PrintList() function doesn't print anything: it calls the function staffDetails(), and maybe you intended that function to print out some information, but what it does instead is create a new object, which by the way creates a memory leak because you're not using the return value.
I think your problem is that your list stores factories, not staff, and that some(?) of the functions don't do what their name implies.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I am a beginner in programming and I am trying to solve this problem using queues. I can't seem to come with an idea to solve it.
Can you help please, it is a problem just to learn about how to approach queue problems.
This is the problem:
Tasks - Google Docs[^]
|
|
|
|
|
As with any problem, the approach is to examine the question carefully. Try to break it down into small parts and figure out how each part can be solved in order to feed into the next part. Draw diagrams on paper and do manual calculations to see what timings result.
I am not sure what is meant by " the quantity of rows and columns of the glade."
|
|
|
|
|
The solution is called a scheduler ... google "tutorial task scheduler in c"
In vino veritas
|
|
|
|
|
Hello
How do I check if XXX USB filter driver be installed by MFC?
Thanks
Vincent
|
|
|
|
|
|
I am trying to solve this problem through dynamic programming:
You are given a matrix of n rows and m columns. There’s an integer number on each cell of the board and the rabbit staying at the upper-left corner.
Collect the greatest sum possible, such that the rabbit can move in only two directions:
2 cells to the right and 1 cell down (x+2, y+1); 2 cells down and 1 cell to the right (x+1, y+2);
Input:
The first line contains two naturals n and m (1 ≤ n, m ≤ 10^3) – the quantity of rows and columns of the matrix.
The next n lines contain m numbers – the values of the matrix elements.
The upper-left corner’s coordinates are (1, 1), the lower-right corner’s – (n, m).
Output:
The greatest sum possibly collected. If the r rabbit can’t reach the lower-right corner, output «-».
Input1:
3 3
5 0 0
0 1 2
1 0 1
Output1:
-
Input2:
4 4
5 2 1 0
1 0 0 0
2 1 3 0
0 0 1 7
Output2:
13
This the code I tried to develop:
#include <iostream>
#include <algorithm>
using namespace std;
void findMaxSum(int *a[], int r, int c)
{
int **res = new int*[r];
for (int i = 0; i < r; i++) {
res[i] = new int[c];
for (int j = 0; j < c; j++)
res[i][j] = -1;
}
for (int i = 0; i < r-1; i++) {
for (int j = i; j < c-1; j++) {
res[i + 1][j + 2] = max(a[i][j] + a[i + 1][j + 2], res[i + 1][j + 2]);
res[i + 2][j + 1] = max(a[i][j] + a[i + 2][j + 1], res[i + 2][j + 1]);
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
cout << res[i][j] << " ";
cout << endl;
}
delete[] res;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int **a = new int*[r];
for (int i = 0; i < r; i++) {
a[i] = new int[c];
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
cin >> a[i][j];
}
findMaxSum(a, r, c);
delete[] a;
return 0;
}
Could you help which right for or while loop to use to calculate such sum?
|
|
|
|
|