How can i display the array without other array with no value? C++











up vote
-1
down vote

favorite












So i set my array value as 100 and when i want to display my array,the others array with no value will also display. Any ways to remove it ? Thanks!



#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<fstream>
#include<limits>
#include<sstream>
using namespace std;

char add();
char list();


struct Book //structure for the book
{
char title[50];
char author[50];
int price;
void display() {
cout << "Name:" << title << endl
<< "Author:" << author<< endl
<< "Price:" << price << endl << endl;
}

};
Book book[100];

void main()
{

int selection;


do {
cout << "Activity Selection: " << endl;
cout << "1. Add book" << endl;
cout << "2. List all books" << endl;
cout << "3. Search book" << endl;
cout << "4. Check total book purchased and total spent" << endl;
cout << "5. Quit" << endl;
cout << "Please enter your selection(1/2/3/4/5): ";
cin >> selection;
cin.ignore();


switch (selection)
{

case 1:
add();
break;
case 2:
list();
break;
case 3:
cout << "number 3" << endl;
break;
case 4:
cout << "number 4" << endl;
break;
default:
exit(1);
break;
}
} while (selection <= 4);



system("pause");
}

char add()
{
int i;


ofstream outFile;
outFile.open("Records.dat", ios::app);
if (!outFile.is_open())
{
cout << "Can’t open file.";
system("pause");
exit(1);
}


cout << "Please enter the book title:";
cin.getline(book[i].title, 51);
cout << "Please enter the author name of the book:";
cin.getline(book[i].author, 51);
cout << "Please enter the price of the book RM: ";
cin >> book[i].price;
cout << "nThe book""<< book[i].title <<""has been added to the system.n" <<
endl;
outFile << book[i].title << "," << book[i].author << "," << book[i].price <<
endl;
outFile.close();



}

char list() //second solution but still doesnt work need some fixing here i think
{
int i;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile,line))
{

inFile.get(data);
if (!(data == 0));
cout << data;
}
cout << endl;
inFile.close();
}


when i debug something like this come out :
ss



this is the second solution i come out with but still does not work



 char list()
{
int i , count = 0;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile, line))
{

stringstream ss(line);
getline(ss, token, ',');
stringstream(token) >> book[i].title;
getline(ss, token, ',');
stringstream(token) >> book[i].author;
getline(ss, token, ',');
stringstream(token) >> book[i].price;
i++;

}
cout << endl;
inFile.close();
for (int count = 0; count < i; count++)
book[i].display();


}


The problem is i dont really know how to display the array using the dat file and i successfully display it but theres error










share|improve this question




















  • 1




    It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
    – Zhe Chen
    Nov 20 at 8:43






  • 1




    What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
    – Thomas Sablik
    Nov 20 at 8:44






  • 1




    if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
    – Scheff
    Nov 20 at 8:44






  • 1




    The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
    – Serge Ballesta
    Nov 20 at 8:45












  • oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
    – nowifiidie
    Nov 20 at 8:58















up vote
-1
down vote

favorite












So i set my array value as 100 and when i want to display my array,the others array with no value will also display. Any ways to remove it ? Thanks!



#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<fstream>
#include<limits>
#include<sstream>
using namespace std;

char add();
char list();


struct Book //structure for the book
{
char title[50];
char author[50];
int price;
void display() {
cout << "Name:" << title << endl
<< "Author:" << author<< endl
<< "Price:" << price << endl << endl;
}

};
Book book[100];

void main()
{

int selection;


do {
cout << "Activity Selection: " << endl;
cout << "1. Add book" << endl;
cout << "2. List all books" << endl;
cout << "3. Search book" << endl;
cout << "4. Check total book purchased and total spent" << endl;
cout << "5. Quit" << endl;
cout << "Please enter your selection(1/2/3/4/5): ";
cin >> selection;
cin.ignore();


switch (selection)
{

case 1:
add();
break;
case 2:
list();
break;
case 3:
cout << "number 3" << endl;
break;
case 4:
cout << "number 4" << endl;
break;
default:
exit(1);
break;
}
} while (selection <= 4);



system("pause");
}

char add()
{
int i;


ofstream outFile;
outFile.open("Records.dat", ios::app);
if (!outFile.is_open())
{
cout << "Can’t open file.";
system("pause");
exit(1);
}


cout << "Please enter the book title:";
cin.getline(book[i].title, 51);
cout << "Please enter the author name of the book:";
cin.getline(book[i].author, 51);
cout << "Please enter the price of the book RM: ";
cin >> book[i].price;
cout << "nThe book""<< book[i].title <<""has been added to the system.n" <<
endl;
outFile << book[i].title << "," << book[i].author << "," << book[i].price <<
endl;
outFile.close();



}

char list() //second solution but still doesnt work need some fixing here i think
{
int i;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile,line))
{

inFile.get(data);
if (!(data == 0));
cout << data;
}
cout << endl;
inFile.close();
}


when i debug something like this come out :
ss



this is the second solution i come out with but still does not work



 char list()
{
int i , count = 0;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile, line))
{

stringstream ss(line);
getline(ss, token, ',');
stringstream(token) >> book[i].title;
getline(ss, token, ',');
stringstream(token) >> book[i].author;
getline(ss, token, ',');
stringstream(token) >> book[i].price;
i++;

}
cout << endl;
inFile.close();
for (int count = 0; count < i; count++)
book[i].display();


}


The problem is i dont really know how to display the array using the dat file and i successfully display it but theres error










share|improve this question




















  • 1




    It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
    – Zhe Chen
    Nov 20 at 8:43






  • 1




    What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
    – Thomas Sablik
    Nov 20 at 8:44






  • 1




    if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
    – Scheff
    Nov 20 at 8:44






  • 1




    The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
    – Serge Ballesta
    Nov 20 at 8:45












  • oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
    – nowifiidie
    Nov 20 at 8:58













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











So i set my array value as 100 and when i want to display my array,the others array with no value will also display. Any ways to remove it ? Thanks!



#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<fstream>
#include<limits>
#include<sstream>
using namespace std;

char add();
char list();


struct Book //structure for the book
{
char title[50];
char author[50];
int price;
void display() {
cout << "Name:" << title << endl
<< "Author:" << author<< endl
<< "Price:" << price << endl << endl;
}

};
Book book[100];

void main()
{

int selection;


do {
cout << "Activity Selection: " << endl;
cout << "1. Add book" << endl;
cout << "2. List all books" << endl;
cout << "3. Search book" << endl;
cout << "4. Check total book purchased and total spent" << endl;
cout << "5. Quit" << endl;
cout << "Please enter your selection(1/2/3/4/5): ";
cin >> selection;
cin.ignore();


switch (selection)
{

case 1:
add();
break;
case 2:
list();
break;
case 3:
cout << "number 3" << endl;
break;
case 4:
cout << "number 4" << endl;
break;
default:
exit(1);
break;
}
} while (selection <= 4);



system("pause");
}

char add()
{
int i;


ofstream outFile;
outFile.open("Records.dat", ios::app);
if (!outFile.is_open())
{
cout << "Can’t open file.";
system("pause");
exit(1);
}


cout << "Please enter the book title:";
cin.getline(book[i].title, 51);
cout << "Please enter the author name of the book:";
cin.getline(book[i].author, 51);
cout << "Please enter the price of the book RM: ";
cin >> book[i].price;
cout << "nThe book""<< book[i].title <<""has been added to the system.n" <<
endl;
outFile << book[i].title << "," << book[i].author << "," << book[i].price <<
endl;
outFile.close();



}

char list() //second solution but still doesnt work need some fixing here i think
{
int i;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile,line))
{

inFile.get(data);
if (!(data == 0));
cout << data;
}
cout << endl;
inFile.close();
}


when i debug something like this come out :
ss



this is the second solution i come out with but still does not work



 char list()
{
int i , count = 0;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile, line))
{

stringstream ss(line);
getline(ss, token, ',');
stringstream(token) >> book[i].title;
getline(ss, token, ',');
stringstream(token) >> book[i].author;
getline(ss, token, ',');
stringstream(token) >> book[i].price;
i++;

}
cout << endl;
inFile.close();
for (int count = 0; count < i; count++)
book[i].display();


}


The problem is i dont really know how to display the array using the dat file and i successfully display it but theres error










share|improve this question















So i set my array value as 100 and when i want to display my array,the others array with no value will also display. Any ways to remove it ? Thanks!



#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<fstream>
#include<limits>
#include<sstream>
using namespace std;

char add();
char list();


struct Book //structure for the book
{
char title[50];
char author[50];
int price;
void display() {
cout << "Name:" << title << endl
<< "Author:" << author<< endl
<< "Price:" << price << endl << endl;
}

};
Book book[100];

void main()
{

int selection;


do {
cout << "Activity Selection: " << endl;
cout << "1. Add book" << endl;
cout << "2. List all books" << endl;
cout << "3. Search book" << endl;
cout << "4. Check total book purchased and total spent" << endl;
cout << "5. Quit" << endl;
cout << "Please enter your selection(1/2/3/4/5): ";
cin >> selection;
cin.ignore();


switch (selection)
{

case 1:
add();
break;
case 2:
list();
break;
case 3:
cout << "number 3" << endl;
break;
case 4:
cout << "number 4" << endl;
break;
default:
exit(1);
break;
}
} while (selection <= 4);



system("pause");
}

char add()
{
int i;


ofstream outFile;
outFile.open("Records.dat", ios::app);
if (!outFile.is_open())
{
cout << "Can’t open file.";
system("pause");
exit(1);
}


cout << "Please enter the book title:";
cin.getline(book[i].title, 51);
cout << "Please enter the author name of the book:";
cin.getline(book[i].author, 51);
cout << "Please enter the price of the book RM: ";
cin >> book[i].price;
cout << "nThe book""<< book[i].title <<""has been added to the system.n" <<
endl;
outFile << book[i].title << "," << book[i].author << "," << book[i].price <<
endl;
outFile.close();



}

char list() //second solution but still doesnt work need some fixing here i think
{
int i;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile,line))
{

inFile.get(data);
if (!(data == 0));
cout << data;
}
cout << endl;
inFile.close();
}


when i debug something like this come out :
ss



this is the second solution i come out with but still does not work



 char list()
{
int i , count = 0;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile, line))
{

stringstream ss(line);
getline(ss, token, ',');
stringstream(token) >> book[i].title;
getline(ss, token, ',');
stringstream(token) >> book[i].author;
getline(ss, token, ',');
stringstream(token) >> book[i].price;
i++;

}
cout << endl;
inFile.close();
for (int count = 0; count < i; count++)
book[i].display();


}


The problem is i dont really know how to display the array using the dat file and i successfully display it but theres error







c++ arrays file-io char






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 9:15

























asked Nov 20 at 8:33









nowifiidie

12




12








  • 1




    It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
    – Zhe Chen
    Nov 20 at 8:43






  • 1




    What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
    – Thomas Sablik
    Nov 20 at 8:44






  • 1




    if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
    – Scheff
    Nov 20 at 8:44






  • 1




    The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
    – Serge Ballesta
    Nov 20 at 8:45












  • oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
    – nowifiidie
    Nov 20 at 8:58














  • 1




    It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
    – Zhe Chen
    Nov 20 at 8:43






  • 1




    What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
    – Thomas Sablik
    Nov 20 at 8:44






  • 1




    if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
    – Scheff
    Nov 20 at 8:44






  • 1




    The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
    – Serge Ballesta
    Nov 20 at 8:45












  • oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
    – nowifiidie
    Nov 20 at 8:58








1




1




It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
– Zhe Chen
Nov 20 at 8:43




It's hard to answer your question without more context. What is Records.dat exactly? Probably you should remove the semicolon after if (!(data == 0));, which seems to be the bug.
– Zhe Chen
Nov 20 at 8:43




1




1




What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
– Thomas Sablik
Nov 20 at 8:44




What and where is your array? What is the others array? To debug means to use a tool like gdb where you can analyze each step of your program flow.
– Thomas Sablik
Nov 20 at 8:44




1




1




if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
– Scheff
Nov 20 at 8:44




if (!(data == 0)); looks very suspicious. The compiler should've warned you that it has no side-effect.
– Scheff
Nov 20 at 8:44




1




1




The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
– Serge Ballesta
Nov 20 at 8:45






The code contains no array but only a file, and we cannot know what the file is supposed to contain. How can we answer here. You really should read How to Ask and think about what a Minimal, Complete, and Verifiable example should be... Just one additional point while (!inFile.eof()) is always wrong because you process one unread value.
– Serge Ballesta
Nov 20 at 8:45














oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
– nowifiidie
Nov 20 at 8:58




oh im sorry im a new user and i dont really know how to post the code, i will try to post the whole code here
– nowifiidie
Nov 20 at 8:58












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are reading the CSV as a text file and just printing it.
So, the solution is to make a record class, read it from stream, then check it whether it is empty or no.



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

class Record {
private:
std::string title;
std::string shortTitle;
int number;
public:
Record(std::string title, std::string shortTitle, int number): title(title),
shortTitle(shortTitle),
number(number){

}
inline std::string getTitle() const { return title; }
inline std::string getShortTitle() const { return shortTitle; }
inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
// an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
std::string title;
std::string shortTitle;
int number;
std::string line;
// Read a line from the stream
getline(str, line);
// Make a new stream from that line
std::stringstream lineStream(line);
// Read until the comma
getline(lineStream, title, ',');
getline(lineStream, shortTitle, ',');
// The last one is an integer, simply read it
lineStream >> number;
return Record(title, shortTitle, number);
}

int main() {
Record rec = readFromStream(cin);
// Check for emptiness, if is NOT empty write it
if (rec.getTitle().size() && rec.getShortTitle().size()) {
cout << rec.getTitle() << "n";
cout << rec.getShortTitle() << "n";
cout << rec.getNumber() << "n";
}
return 0;
}





share|improve this answer





















  • can i do it like this even my array is in char type?
    – nowifiidie
    Nov 20 at 9:17










  • If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
    – user9335240
    Nov 20 at 9:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388992%2fhow-can-i-display-the-array-without-other-array-with-no-value-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













You are reading the CSV as a text file and just printing it.
So, the solution is to make a record class, read it from stream, then check it whether it is empty or no.



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

class Record {
private:
std::string title;
std::string shortTitle;
int number;
public:
Record(std::string title, std::string shortTitle, int number): title(title),
shortTitle(shortTitle),
number(number){

}
inline std::string getTitle() const { return title; }
inline std::string getShortTitle() const { return shortTitle; }
inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
// an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
std::string title;
std::string shortTitle;
int number;
std::string line;
// Read a line from the stream
getline(str, line);
// Make a new stream from that line
std::stringstream lineStream(line);
// Read until the comma
getline(lineStream, title, ',');
getline(lineStream, shortTitle, ',');
// The last one is an integer, simply read it
lineStream >> number;
return Record(title, shortTitle, number);
}

int main() {
Record rec = readFromStream(cin);
// Check for emptiness, if is NOT empty write it
if (rec.getTitle().size() && rec.getShortTitle().size()) {
cout << rec.getTitle() << "n";
cout << rec.getShortTitle() << "n";
cout << rec.getNumber() << "n";
}
return 0;
}





share|improve this answer





















  • can i do it like this even my array is in char type?
    – nowifiidie
    Nov 20 at 9:17










  • If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
    – user9335240
    Nov 20 at 9:45















up vote
0
down vote













You are reading the CSV as a text file and just printing it.
So, the solution is to make a record class, read it from stream, then check it whether it is empty or no.



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

class Record {
private:
std::string title;
std::string shortTitle;
int number;
public:
Record(std::string title, std::string shortTitle, int number): title(title),
shortTitle(shortTitle),
number(number){

}
inline std::string getTitle() const { return title; }
inline std::string getShortTitle() const { return shortTitle; }
inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
// an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
std::string title;
std::string shortTitle;
int number;
std::string line;
// Read a line from the stream
getline(str, line);
// Make a new stream from that line
std::stringstream lineStream(line);
// Read until the comma
getline(lineStream, title, ',');
getline(lineStream, shortTitle, ',');
// The last one is an integer, simply read it
lineStream >> number;
return Record(title, shortTitle, number);
}

int main() {
Record rec = readFromStream(cin);
// Check for emptiness, if is NOT empty write it
if (rec.getTitle().size() && rec.getShortTitle().size()) {
cout << rec.getTitle() << "n";
cout << rec.getShortTitle() << "n";
cout << rec.getNumber() << "n";
}
return 0;
}





share|improve this answer





















  • can i do it like this even my array is in char type?
    – nowifiidie
    Nov 20 at 9:17










  • If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
    – user9335240
    Nov 20 at 9:45













up vote
0
down vote










up vote
0
down vote









You are reading the CSV as a text file and just printing it.
So, the solution is to make a record class, read it from stream, then check it whether it is empty or no.



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

class Record {
private:
std::string title;
std::string shortTitle;
int number;
public:
Record(std::string title, std::string shortTitle, int number): title(title),
shortTitle(shortTitle),
number(number){

}
inline std::string getTitle() const { return title; }
inline std::string getShortTitle() const { return shortTitle; }
inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
// an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
std::string title;
std::string shortTitle;
int number;
std::string line;
// Read a line from the stream
getline(str, line);
// Make a new stream from that line
std::stringstream lineStream(line);
// Read until the comma
getline(lineStream, title, ',');
getline(lineStream, shortTitle, ',');
// The last one is an integer, simply read it
lineStream >> number;
return Record(title, shortTitle, number);
}

int main() {
Record rec = readFromStream(cin);
// Check for emptiness, if is NOT empty write it
if (rec.getTitle().size() && rec.getShortTitle().size()) {
cout << rec.getTitle() << "n";
cout << rec.getShortTitle() << "n";
cout << rec.getNumber() << "n";
}
return 0;
}





share|improve this answer












You are reading the CSV as a text file and just printing it.
So, the solution is to make a record class, read it from stream, then check it whether it is empty or no.



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

class Record {
private:
std::string title;
std::string shortTitle;
int number;
public:
Record(std::string title, std::string shortTitle, int number): title(title),
shortTitle(shortTitle),
number(number){

}
inline std::string getTitle() const { return title; }
inline std::string getShortTitle() const { return shortTitle; }
inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
// an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
std::string title;
std::string shortTitle;
int number;
std::string line;
// Read a line from the stream
getline(str, line);
// Make a new stream from that line
std::stringstream lineStream(line);
// Read until the comma
getline(lineStream, title, ',');
getline(lineStream, shortTitle, ',');
// The last one is an integer, simply read it
lineStream >> number;
return Record(title, shortTitle, number);
}

int main() {
Record rec = readFromStream(cin);
// Check for emptiness, if is NOT empty write it
if (rec.getTitle().size() && rec.getShortTitle().size()) {
cout << rec.getTitle() << "n";
cout << rec.getShortTitle() << "n";
cout << rec.getNumber() << "n";
}
return 0;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 9:02









user9335240

1,1291210




1,1291210












  • can i do it like this even my array is in char type?
    – nowifiidie
    Nov 20 at 9:17










  • If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
    – user9335240
    Nov 20 at 9:45


















  • can i do it like this even my array is in char type?
    – nowifiidie
    Nov 20 at 9:17










  • If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
    – user9335240
    Nov 20 at 9:45
















can i do it like this even my array is in char type?
– nowifiidie
Nov 20 at 9:17




can i do it like this even my array is in char type?
– nowifiidie
Nov 20 at 9:17












If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
– user9335240
Nov 20 at 9:45




If you want to check whether your char array is empty or not, you can use the C style <string.h>, you can use strlen for example, if the string is empty, it would return 0.
– user9335240
Nov 20 at 9:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388992%2fhow-can-i-display-the-array-without-other-array-with-no-value-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'